From b13062b7055876a49b13e7b71d92fd19a5f017bd Mon Sep 17 00:00:00 2001
From: DamianKomdeur <77113617+DamianKomdeur@users.noreply.github.com>
Date: Tue, 10 Dec 2024 12:19:11 +0100
Subject: [PATCH] solver and tile remover fix

---
 PuzzlePlayer/Sudoku.cs | 94 ++++++++++++++++++++++++------------------
 1 file changed, 55 insertions(+), 39 deletions(-)

diff --git a/PuzzlePlayer/Sudoku.cs b/PuzzlePlayer/Sudoku.cs
index 5423716..7d4dd3f 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);
@@ -22,11 +23,16 @@ namespace PuzzlePlayer_Namespace
             rootBoardLength = (int)Math.Sqrt(boardLength);
 
             description = "SUDOKU !!!!! !!!!";
-            drawFactor = 8;
+            drawFactor = 1;
         }
 
         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--;
+                }
             }
         }
 
-- 
GitLab