From 8360f26f4cea9b4e81221a0958eba6554e3fb4e7 Mon Sep 17 00:00:00 2001 From: DamianKomdeur <77113617+DamianKomdeur@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:09:19 +0100 Subject: [PATCH] Sudoku Solver --- PuzzlePlayer/Sudoku.cs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/PuzzlePlayer/Sudoku.cs b/PuzzlePlayer/Sudoku.cs index 30c870d..5423716 100644 --- a/PuzzlePlayer/Sudoku.cs +++ b/PuzzlePlayer/Sudoku.cs @@ -14,7 +14,7 @@ namespace PuzzlePlayer_Namespace { private static int boardLength; private static int rootBoardLength; - public Sudoku(int boardSize = 16) + public Sudoku(int boardSize = 9) { boardState = GetClearBoard(boardSize, boardSize); lastGeneratedBoard = GetClearBoard(boardSize, boardSize); @@ -75,6 +75,15 @@ namespace PuzzlePlayer_Namespace } FillSudoku(0, rootBoardLength); + + RemoveSpaces(54); + } + + public override SOLUTIONS Solve(bool j) + { + FillSudoku(0, rootBoardLength); + + return SOLUTIONS.UNIQUE; } private int RandomNumber(int number) @@ -169,8 +178,9 @@ namespace PuzzlePlayer_Namespace boardState[row, col] = num; if (FillSudoku(row, col + 1)) + { return true; - + } boardState[row, col] = -1; } } @@ -219,7 +229,7 @@ namespace PuzzlePlayer_Namespace { if (p == null) return; 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) @@ -242,11 +252,11 @@ namespace PuzzlePlayer_Namespace } else { - boardState[p.X, p.Y] = 9; + boardState[p.X, p.Y] = boardLength; return; } } - else if (boardState[p.X, p.Y] == 9) + else if (boardState[p.X, p.Y] == boardLength) { if (x == 1) { @@ -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) { List<Move> result = new List<Move>(); -- GitLab