Skip to content
Snippets Groups Projects
Commit 8360f26f authored by DamianKomdeur's avatar DamianKomdeur
Browse files

Sudoku Solver

parent 7b9672e4
No related branches found
No related tags found
No related merge requests found
...@@ -14,7 +14,7 @@ namespace PuzzlePlayer_Namespace ...@@ -14,7 +14,7 @@ namespace PuzzlePlayer_Namespace
{ {
private static int boardLength; private static int boardLength;
private static int rootBoardLength; private static int rootBoardLength;
public Sudoku(int boardSize = 16) public Sudoku(int boardSize = 9)
{ {
boardState = GetClearBoard(boardSize, boardSize); boardState = GetClearBoard(boardSize, boardSize);
lastGeneratedBoard = GetClearBoard(boardSize, boardSize); lastGeneratedBoard = GetClearBoard(boardSize, boardSize);
...@@ -75,6 +75,15 @@ namespace PuzzlePlayer_Namespace ...@@ -75,6 +75,15 @@ namespace PuzzlePlayer_Namespace
} }
FillSudoku(0, rootBoardLength); FillSudoku(0, rootBoardLength);
RemoveSpaces(54);
}
public override SOLUTIONS Solve(bool j)
{
FillSudoku(0, rootBoardLength);
return SOLUTIONS.UNIQUE;
} }
private int RandomNumber(int number) private int RandomNumber(int number)
...@@ -169,8 +178,9 @@ namespace PuzzlePlayer_Namespace ...@@ -169,8 +178,9 @@ namespace PuzzlePlayer_Namespace
boardState[row, col] = num; boardState[row, col] = num;
if (FillSudoku(row, col + 1)) if (FillSudoku(row, col + 1))
{
return true; return true;
}
boardState[row, col] = -1; boardState[row, col] = -1;
} }
} }
...@@ -219,7 +229,7 @@ namespace PuzzlePlayer_Namespace ...@@ -219,7 +229,7 @@ namespace PuzzlePlayer_Namespace
{ {
if (p == null) return; if (p == null) return;
int num = (int)k - 48; int num = (int)k - 48;
if (num >= 1 && num <= 9) boardState[((Point)p).X, ((Point)p).Y] = num; if (num >= 1 && num <= boardLength) boardState[((Point)p).X, ((Point)p).Y] = num;
} }
public override void TileClick(Point p, int x) public override void TileClick(Point p, int x)
...@@ -242,11 +252,11 @@ namespace PuzzlePlayer_Namespace ...@@ -242,11 +252,11 @@ namespace PuzzlePlayer_Namespace
} }
else else
{ {
boardState[p.X, p.Y] = 9; boardState[p.X, p.Y] = boardLength;
return; return;
} }
} }
else if (boardState[p.X, p.Y] == 9) else if (boardState[p.X, p.Y] == boardLength)
{ {
if (x == 1) if (x == 1)
{ {
...@@ -279,6 +289,17 @@ namespace PuzzlePlayer_Namespace ...@@ -279,6 +289,17 @@ namespace PuzzlePlayer_Namespace
} }
} }
private void RemoveSpaces(int k)
{
for(int i = 0; i < k; i++)
{
int row = RandomNumber(boardLength) - 1;
int col = RandomNumber(boardLength) - 1;
boardState[row, col] = emptySpace;
}
}
protected override List<Move> GetSolveList(int[,] boardToSolve) protected override List<Move> GetSolveList(int[,] boardToSolve)
{ {
List<Move> result = new List<Move>(); List<Move> result = new List<Move>();
......
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