Skip to content
Snippets Groups Projects
Commit 8700e758 authored by bionic85's avatar bionic85
Browse files

started working on optimisation

parent 218ec63c
No related branches found
No related tags found
No related merge requests found
......@@ -82,6 +82,19 @@ namespace PuzzlePlayer_Namespace
boardState = generatedBoard;
}
private static int recursionDepth = 0;
private static int maxDepth = 500000;
private static void PrintBoard(int[,] board)
{
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
Debug.Write(board[i, j] == emptySpace ? "." : board[i, j].ToString());
Debug.WriteLine("");
}
Debug.WriteLine(new string('-', 20));
}
// generates a random board with a backtracking algorithm
// After searching online about what the best way is to make a random puzzle generator a lot of people pointed towards a backtracking algorithm
......@@ -89,6 +102,15 @@ namespace PuzzlePlayer_Namespace
// But i wrote all the code myself
private static int[,] BackTrackAlgorithm(int[,] board)
{
//recursionDepth++;
if (recursionDepth > maxDepth)
{
Console.WriteLine("Max recursion depth reached.");
//return null;
}
PrintBoard(board);
// check if the board is complete. if so then we can return the result
if (IsBoardCompletlyFilledIn(board))
return board;
......@@ -104,7 +126,11 @@ namespace PuzzlePlayer_Namespace
return null; // backtrack
}
/*
// check if the current board is already imposible to complete even tho there are still choices left
if (BoardIsImposible(board, choices))
return null;
///*
// shuffle the choices in the list around to get different results every time
// First answer from: https://stackoverflow.com/questions/273313/randomize-a-listt
Random random = new Random();
......@@ -123,7 +149,8 @@ namespace PuzzlePlayer_Namespace
// do the algorithm for every move
foreach (Move m in choices)
{
int[,] newBoard = board;
int[,] newBoard = (int[,])board.Clone();
newBoard[m.x,m.y] = m.changeTo;
int[,] result = BackTrackAlgorithm(newBoard); // recursion for every move
......@@ -133,11 +160,38 @@ namespace PuzzlePlayer_Namespace
}
//Debug.WriteLine("backtrack want moves empty");
recursionDepth--;
// if all choices fail then we should also return null
return null;
}
private static bool BoardIsImposible(int[,] board, List<Move> choices)
{
throw new NotImplementedException();
bool isImposible = false;
List<int[]> locations = new List<int[]>();
for (int i = 0; i < board.GetLength(0); i++)
for (int j = 0; j < board.GetLength(1); j++)
{
if(board[i,j] == emptySpace)
locations.Add([i,j]);
}
foreach (Move m in choices)
{
foreach (int[] i in locations)
{
if (m.x == i[0] && m.y == i[1])
continue;
}
}
return isImposible;
}
// get all the possible choices
private static List<Move> GetChoices(int[,] board)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment