Skip to content
Snippets Groups Projects
Commit 55b0a16b authored by bionic85's avatar bionic85
Browse files

binary generator is nu echt helemaal done

parent 4c71273a
No related branches found
No related tags found
No related merge requests found
...@@ -101,22 +101,29 @@ namespace PuzzlePlayer_Namespace ...@@ -101,22 +101,29 @@ namespace PuzzlePlayer_Namespace
boardState = (int[,])generatedBoard.Clone(); boardState = (int[,])generatedBoard.Clone();
// generate a list with all positions
List<(int,int)> allPositions = new List<(int,int)> ();
for(int i = 0; i < boardState.GetLength(0); i++)
for(int j = 0; j < boardState.GetLength(1); j++)
allPositions.Add(new (i, j));
Random rnd = new Random(); Random rnd = new Random();
//remove spaces until the board is unsolvable, then go one step back //remove spaces until the board is unsolvable, then go one step back
while (true) while (true)
{ {
int[,] copy = (int[,])boardState.Clone(); if(allPositions.Count == 0)
int x = rnd.Next(boardState.GetLength(0)); break;
int y = rnd.Next(boardState.GetLength(1));
if (boardState[x,y] != emptySpace) int i = rnd.Next(allPositions.Count);
boardState[x,y] = emptySpace; (int x, int y) = allPositions[i];
int copy = boardState[x,y]; // create a copy in case it is not possible to solve the puzzle anymore
boardState[x,y] = emptySpace;
allPositions.RemoveAt(i);
if(Solve(true) == SOLUTIONS.NONE) if(Solve(true) == SOLUTIONS.NONE)
{ {
boardState = (int[,])copy.Clone(); boardState[x, y] = copy; // restore the copy if there are no solutions found
break;
} }
} }
// save the generated board for testing the users solution and the use of a reset button // save the generated board for testing the users solution and the use of a reset button
......
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