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

solver and tile remover fix

parent 8360f26f
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ namespace PuzzlePlayer_Namespace ...@@ -14,6 +14,7 @@ namespace PuzzlePlayer_Namespace
{ {
private static int boardLength; private static int boardLength;
private static int rootBoardLength; private static int rootBoardLength;
private static Random random = new Random();
public Sudoku(int boardSize = 9) public Sudoku(int boardSize = 9)
{ {
boardState = GetClearBoard(boardSize, boardSize); boardState = GetClearBoard(boardSize, boardSize);
...@@ -22,11 +23,16 @@ namespace PuzzlePlayer_Namespace ...@@ -22,11 +23,16 @@ namespace PuzzlePlayer_Namespace
rootBoardLength = (int)Math.Sqrt(boardLength); rootBoardLength = (int)Math.Sqrt(boardLength);
description = "SUDOKU !!!!! !!!!"; description = "SUDOKU !!!!! !!!!";
drawFactor = 8; drawFactor = 1;
} }
public override void Draw(Graphics gr, Rectangle r) 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)); Size tilesize = new Size(r.Width / boardState.GetLength(0), r.Height / boardState.GetLength(1));
Pen border = new Pen(Color.Black, 2); 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)); 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 ...@@ -47,8 +53,9 @@ namespace PuzzlePlayer_Namespace
(boardState[i, j]).ToString(), (boardState[i, j]).ToString(),
new Font("Arial", tilesize.Width / 2), new Font("Arial", tilesize.Width / 2),
Brushes.Black, Brushes.Black,
(float)(r.X + i * tilesize.Width + tilesize.Width / 4), (float)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4),
(float)(r.Y + j * tilesize.Height + tilesize.Height / 4) (float)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4),
format
); );
} }
...@@ -74,21 +81,22 @@ namespace PuzzlePlayer_Namespace ...@@ -74,21 +81,22 @@ namespace PuzzlePlayer_Namespace
FillBox(i, i); 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); if (SolveSudoku())
{
return SOLUTIONS.UNIQUE; return SOLUTIONS.UNIQUE;
}
return SOLUTIONS.NONE;
} }
private int RandomNumber(int number) private int RandomNumber(int number)
{ {
Random random = new Random();
return (int)Math.Floor((double)(random.NextDouble() * number + 1)); return (int)Math.Floor((double)(random.NextDouble() * number + 1));
} }
...@@ -113,7 +121,7 @@ namespace PuzzlePlayer_Namespace ...@@ -113,7 +121,7 @@ namespace PuzzlePlayer_Namespace
private bool DoAllChecks(int i, int j, int num) 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) private bool BoxFlag(int row, int col, int num)
...@@ -131,7 +139,7 @@ namespace PuzzlePlayer_Namespace ...@@ -131,7 +139,7 @@ namespace PuzzlePlayer_Namespace
return false; return false;
} }
private bool RowFlag(int i, int num) private bool ColFlag(int i, int num)
{ {
for (int j = 0; j < boardLength; j++) for (int j = 0; j < boardLength; j++)
{ {
...@@ -143,7 +151,7 @@ namespace PuzzlePlayer_Namespace ...@@ -143,7 +151,7 @@ namespace PuzzlePlayer_Namespace
return false; return false;
} }
private bool ColFlag(int j, int num) private bool RowFlag(int j, int num)
{ {
for (int i = 0; i < boardLength; i++) for (int i = 0; i < boardLength; i++)
{ {
...@@ -155,36 +163,34 @@ namespace PuzzlePlayer_Namespace ...@@ -155,36 +163,34 @@ namespace PuzzlePlayer_Namespace
return false; return false;
} }
private bool FillSudoku(int row, int col) // Heilige piramide
private bool SolveSudoku()
{ {
if (row == boardLength - 1 && col == boardLength) for (int row = 0; row < boardLength; row++)
return true;
if (col == boardLength)
{ {
row++; for (int col = 0; col < boardLength; col++)
col = 0;
}
if (boardState[row, col] != -1)
{
return FillSudoku(row, col + 1);
}
for (int num = 1; num <= boardLength; num++)
{
if (!DoAllChecks(row, col, num))
{ {
boardState[row, col] = num; if (boardState[row, col] == emptySpace)
if (FillSudoku(row, col + 1))
{ {
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) private static string BoardToString(int[,] board)
...@@ -293,10 +299,20 @@ namespace PuzzlePlayer_Namespace ...@@ -293,10 +299,20 @@ namespace PuzzlePlayer_Namespace
{ {
for(int i = 0; i < k; i++) for(int i = 0; i < k; i++)
{ {
int row = RandomNumber(boardLength) - 1; int row;
int col = RandomNumber(boardLength) - 1; 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--;
}
} }
} }
......
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