diff --git a/PuzzlePlayer/Sudoku.cs b/PuzzlePlayer/Sudoku.cs index 447c2d723129b2d7bb78aa8f42b4e652276ad8af..e537c7f81f0bd604c2c519d8aaf2b20038957779 100644 --- a/PuzzlePlayer/Sudoku.cs +++ b/PuzzlePlayer/Sudoku.cs @@ -14,6 +14,7 @@ namespace PuzzlePlayer_Namespace { private static int boardLength; private static int rootBoardLength; + private static Random random = new Random(); public Sudoku(int boardSize = 9) { boardState = GetClearBoard(boardSize, boardSize); @@ -27,6 +28,11 @@ namespace PuzzlePlayer_Namespace public override void Draw(Graphics gr, Rectangle r) { + StringFormat format = new StringFormat + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center, + }; Size tilesize = new Size(r.Width / boardState.GetLength(0), r.Height / boardState.GetLength(1)); Pen border = new Pen(Color.Black, 2); gr.FillRectangle(Brushes.Beige, r.X, r.Y, tilesize.Width * boardState.GetLength(0), tilesize.Height * boardState.GetLength(1)); @@ -47,8 +53,9 @@ namespace PuzzlePlayer_Namespace (boardState[i, j]).ToString(), new Font("Arial", tilesize.Width / 2), Brushes.Black, - (float)(r.X + i * tilesize.Width + tilesize.Width / 4), - (float)(r.Y + j * tilesize.Height + tilesize.Height / 4) + (float)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4), + (float)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4), + format ); } @@ -74,21 +81,22 @@ namespace PuzzlePlayer_Namespace FillBox(i, i); } - FillSudoku(0, rootBoardLength); + SolveSudoku(); - RemoveSpaces(54); + RemoveSpaces((int)(boardLength * boardLength - 30)); } - public override SOLUTIONS Solve(bool j) + public override SOLUTIONS Solve(bool b) { - FillSudoku(0, rootBoardLength); - - return SOLUTIONS.UNIQUE; + if (SolveSudoku()) + { + return SOLUTIONS.UNIQUE; + } + return SOLUTIONS.NONE; } private int RandomNumber(int number) { - Random random = new Random(); return (int)Math.Floor((double)(random.NextDouble() * number + 1)); } @@ -113,7 +121,7 @@ namespace PuzzlePlayer_Namespace private bool DoAllChecks(int i, int j, int num) { - return (BoxFlag(i - i % rootBoardLength, j - j % rootBoardLength, num) || RowFlag(i, num) || ColFlag(j, num)); + return (BoxFlag(i - i % rootBoardLength, j - j % rootBoardLength, num) || ColFlag(i, num) || RowFlag(j, num)); } private bool BoxFlag(int row, int col, int num) @@ -131,7 +139,7 @@ namespace PuzzlePlayer_Namespace return false; } - private bool RowFlag(int i, int num) + private bool ColFlag(int i, int num) { for (int j = 0; j < boardLength; j++) { @@ -143,7 +151,7 @@ namespace PuzzlePlayer_Namespace return false; } - private bool ColFlag(int j, int num) + private bool RowFlag(int j, int num) { for (int i = 0; i < boardLength; i++) { @@ -155,36 +163,34 @@ namespace PuzzlePlayer_Namespace return false; } - private bool FillSudoku(int row, int col) + // Heilige piramide + private bool SolveSudoku() { - if (row == boardLength - 1 && col == boardLength) - return true; - - if (col == boardLength) + for (int row = 0; row < boardLength; row++) { - row++; - col = 0; - } - - if (boardState[row, col] != -1) - { - return FillSudoku(row, col + 1); - } - - for (int num = 1; num <= boardLength; num++) - { - if (!DoAllChecks(row, col, num)) + for (int col = 0; col < boardLength; col++) { - boardState[row, col] = num; - - if (FillSudoku(row, col + 1)) + if (boardState[row, col] == emptySpace) { - return true; + for (int num = 1; num <= boardLength; num++) + { + if (!DoAllChecks(row, col, num)) + { + boardState[row, col] = num; + + if (SolveSudoku()) + { + return true; + } + + boardState[row, col] = emptySpace; + } + } + return false; } - boardState[row, col] = -1; } } - return false; + return true; } private static string BoardToString(int[,] board) @@ -293,10 +299,20 @@ namespace PuzzlePlayer_Namespace { for(int i = 0; i < k; i++) { - int row = RandomNumber(boardLength) - 1; - int col = RandomNumber(boardLength) - 1; + int row; + int col; - boardState[row, col] = emptySpace; + row = RandomNumber(boardLength) - 1; + col = RandomNumber(boardLength) - 1; + + if (boardState[row, col] != emptySpace) + { + boardState[row, col] = emptySpace; + } + else + { + i--; + } } }