Skip to content
Snippets Groups Projects
Commit e1cea94b authored by Floris's avatar Floris
Browse files

general updates

- TileClick and TileInput are now bool that return True if board is solved after input, false otherwise (so invalid or incomplete)
- virtual IsBoardSolved is called by TileClick and TileInput to verify board.
- removed some unused Board methods and added virtual hint method
- fixed impossible 4x4 sudoku generation
- improved solvability of sudoku
- changed draw method of sudoku to draw color givens and inputs differently
- hidden updatebutton
- added win method in puzzleform that runs when board is solved
parent 9425199b
No related branches found
No related tags found
No related merge requests found
...@@ -97,7 +97,8 @@ namespace PuzzlePlayer_Namespace ...@@ -97,7 +97,8 @@ namespace PuzzlePlayer_Namespace
//MessageBox.Show($"Found board in {counter} tries"); //MessageBox.Show($"Found board in {counter} tries");
boardState = (int[,])generatedBoard.Clone(); boardState = (int[,])generatedBoard.Clone();
solution = (int[,])generatedBoard.Clone();
// generate a list with all positions // generate a list with all positions
List<(int,int)> allPositions = new List<(int,int)> (); List<(int,int)> allPositions = new List<(int,int)> ();
for(int i = 0; i < boardState.GetLength(0); i++) for(int i = 0; i < boardState.GetLength(0); i++)
...@@ -441,29 +442,36 @@ namespace PuzzlePlayer_Namespace ...@@ -441,29 +442,36 @@ namespace PuzzlePlayer_Namespace
return false; return false;
} }
public override void TileInput(Point? p, Keys k) public override bool TileInput(Point? p, Keys k)
{ {
if (p == null) return; if (p == null) return false;
int num = (int)k - 48; int num = (int)k - 48;
if (num==0 || num==1 || num==2) boardState[((Point)p).X, ((Point)p).Y] = num%2; if (num == 0 || num == 1 || num == 2)
{
boardState[((Point)p).X, ((Point)p).Y] = num % 2;
return IsBoardSolved();
}
return false;
} }
public override void TileClick(Point p, int x) public override bool TileClick(Point p, int x)
{ {
if (boardState[p.X, p.Y] == emptySpace) if (boardState[p.X, p.Y] == emptySpace)
{ {
boardState[p.X, p.Y] = x; boardState[p.X, p.Y] = x;
return; return IsBoardSolved();
} }
if (boardState[p.X, p.Y] == x) if (boardState[p.X, p.Y] == x)
{ {
boardState[p.X, p.Y] = 1 - boardState[p.X, p.Y]; boardState[p.X, p.Y] = 1 - boardState[p.X, p.Y];
return; return IsBoardSolved();
} }
if (boardState[p.X, p.Y] == 1 - x) if (boardState[p.X, p.Y] == 1 - x)
{ {
boardState[p.X, p.Y] = emptySpace; boardState[p.X, p.Y] = emptySpace;
return false;
} }
} return false;
}
} }
} }
\ No newline at end of file
...@@ -44,6 +44,7 @@ namespace PuzzlePlayer_Namespace ...@@ -44,6 +44,7 @@ namespace PuzzlePlayer_Namespace
// variables to keep track of the board state and the last generated board // variables to keep track of the board state and the last generated board
public int[,] boardState; public int[,] boardState;
public int[,] lastGeneratedBoard; public int[,] lastGeneratedBoard;
public int[,] solution;
// static meathode for filling a int[,] with -1 // static meathode for filling a int[,] with -1
public static int[,] GetClearBoard(int boardSizeX, int boardSizeY) public static int[,] GetClearBoard(int boardSizeX, int boardSizeY)
...@@ -80,7 +81,7 @@ namespace PuzzlePlayer_Namespace ...@@ -80,7 +81,7 @@ namespace PuzzlePlayer_Namespace
result[m.x, m.y] = m.changeTo; result[m.x, m.y] = m.changeTo;
} }
} }
// check if the whole board is filled // check if the whole board is filled
// if not then the methode will return null because it is imposible to solve the board // if not then the methode will return null because it is imposible to solve the board
for (int i = 0; i < result.GetLength(0); i++) for (int i = 0; i < result.GetLength(0); i++)
...@@ -105,13 +106,22 @@ namespace PuzzlePlayer_Namespace ...@@ -105,13 +106,22 @@ namespace PuzzlePlayer_Namespace
public abstract void Generate(); public abstract void Generate();
// abstract methode for checking if a imputed boardstate is valid // abstract methode for checking if a imputed boardstate is valid
public virtual bool IsBoardValid(int[,] boardToCheck) { return true; } public virtual bool IsBoardSolved()
{
for (int i = 0; i < boardState.GetLength(0); i++) for (int j = 0;j < boardState.GetLength(1); j++)
{
if (boardState[i,j] != solution[i,j]) return false;
}
return true;
//MessageBox.Show($"{boardState[0, 0]},{boardState[0, 1]},{boardState[1, 0]},{boardState[1, 1]} hjjkjkhj {solution[0, 0]},{solution[0, 1]},{solution[1, 0]},{solution[1, 1]} ");
//MessageBox.Show($"{boardState == solution}");
}
// changes tile P to value X // changes tile P to value X
public abstract void TileInput(Point? p, Keys k); public abstract bool TileInput(Point? p, Keys k);
// performs a left/right (X) click on tile P, changing its value // performs a left/right (X) click on tile P, changing its value
public virtual void TileClick(Point p, int x) { } public virtual bool TileClick(Point p, int x) { return false; }
// is called by restartbutton on PuzzleForm // is called by restartbutton on PuzzleForm
public virtual void Restart() public virtual void Restart()
......
...@@ -40,7 +40,7 @@ namespace PuzzlePlayer_Namespace ...@@ -40,7 +40,7 @@ namespace PuzzlePlayer_Namespace
Point playerPos; Point playerPos;
List<Point> shortestPath; List<Point> shortestPath;
public Maze(int sizeX = 30, int sizeY = 20) public Maze(int sizeX = 6, int sizeY = 4)
{ {
drawFactor = 1; drawFactor = 1;
// init all 2D array's // init all 2D array's
...@@ -102,7 +102,7 @@ namespace PuzzlePlayer_Namespace ...@@ -102,7 +102,7 @@ namespace PuzzlePlayer_Namespace
Pen wall = new Pen(Color.Black, tilesize.Width / 5); Pen wall = new Pen(Color.Black, tilesize.Width / 5);
// draw an indication of where the end of the maze is // draw an indication of where the end of the maze is
gr.FillRectangle(Brushes.Red, r.X + tilesize.Width * (boardState.GetLength(0) - 1), r.Y + tilesize.Height * (boardState.GetLength(1) - 1), tilesize.Width, tilesize.Height); gr.FillRectangle(Brushes.Red, r.X + tilesize.Width * (boardState.GetLength(0) - 1), r.Y + tilesize.Height * (boardState.GetLength(1) - 1), tilesize.Width + 1, tilesize.Height + 1);
//debug colors //debug colors
//Pen wall1 = new Pen(Color.Black, tilesize.Width / 5); //Pen wall1 = new Pen(Color.Black, tilesize.Width / 5);
...@@ -117,7 +117,7 @@ namespace PuzzlePlayer_Namespace ...@@ -117,7 +117,7 @@ namespace PuzzlePlayer_Namespace
for(int j = 0; j < boardState.GetLength(1); j++) for(int j = 0; j < boardState.GetLength(1); j++)
{ {
Rectangle currentRect = Rectangle currentRect =
new Rectangle(r.X + i * tilesize.Width, r.Y + j * tilesize.Height, tilesize.Width, tilesize.Height); new Rectangle(r.X + i * tilesize.Width, r.Y + j * tilesize.Height, tilesize.Width + 1, tilesize.Height + 1);
// draw the space blue if the player has visited it // draw the space blue if the player has visited it
if (boardState[i, j] == 1) if (boardState[i, j] == 1)
...@@ -366,7 +366,7 @@ namespace PuzzlePlayer_Namespace ...@@ -366,7 +366,7 @@ namespace PuzzlePlayer_Namespace
return false; return false;
} }
public override void TileInput(Point? p, Keys key) public override bool TileInput(Point? p, Keys key)
{ {
boardState[playerPos.X, playerPos.Y] = 1; //dw boardState[playerPos.X, playerPos.Y] = 1; //dw
(bool top, bool right, bool bottom, bool left) = getWallsFromNumber(mazeState[playerPos.X,playerPos.Y]); (bool top, bool right, bool bottom, bool left) = getWallsFromNumber(mazeState[playerPos.X,playerPos.Y]);
...@@ -394,9 +394,10 @@ namespace PuzzlePlayer_Namespace ...@@ -394,9 +394,10 @@ namespace PuzzlePlayer_Namespace
playerPos.X--; playerPos.X--;
break; break;
} }
// upadte the spaces where the player has walked // upadte the spaces where the player has walked
boardState[playerPos.X, playerPos.Y] = 1; boardState[playerPos.X, playerPos.Y] = 1;
return IsBoardSolved();
} }
public override void Restart() public override void Restart()
...@@ -410,5 +411,11 @@ namespace PuzzlePlayer_Namespace ...@@ -410,5 +411,11 @@ namespace PuzzlePlayer_Namespace
{ {
return null; return null;
} }
public override bool IsBoardSolved()
{
//MessageBox.Show($"{Convert.ToString(playerPos)} moet bij {Convert.ToString(new Point(boardState.GetLength(0) - 1, boardState.GetLength(1)))}");
return playerPos == new Point(boardState.GetLength(0) - 1, boardState.GetLength(1) - 1);
}
} }
} }
...@@ -242,7 +242,7 @@ namespace PuzzlePlayer_Namespace ...@@ -242,7 +242,7 @@ namespace PuzzlePlayer_Namespace
case Keys.A: case Keys.A:
case Keys.S: case Keys.S:
case Keys.D: case Keys.D:
Board.TileInput(null, k); if (Board.TileInput(null, k)) Win();
this.Invalidate(); this.Invalidate();
return; return;
} }
...@@ -257,19 +257,25 @@ namespace PuzzlePlayer_Namespace ...@@ -257,19 +257,25 @@ namespace PuzzlePlayer_Namespace
return; return;
case Keys.LButton: case Keys.LButton:
case Keys.RButton: case Keys.RButton:
Board.TileClick(tile, (int)k - 1); if (Board.TileInput(null, k)) Win();
this.Invalidate(); this.Invalidate();
return; return;
case Keys.OemOpenBrackets: case Keys.OemOpenBrackets:
case Keys.OemCloseBrackets: case Keys.OemCloseBrackets:
Board.TileClick(tile, ((int)k - 219)/2); if (Board.TileClick(tile, ((int)k - 219)/2)) Win();
this.Invalidate(); this.Invalidate();
return; return;
default: default:
Board.TileInput(tile, k); if (Board.TileInput(tile, k)) Win();
this.Invalidate(); this.Invalidate();
return; return;
} }
} }
public void Win()
{
this.Invalidate();
MessageBox.Show("oi oi oi");
}
} }
} }
...@@ -20,8 +20,8 @@ namespace PuzzlePlayer_Namespace ...@@ -20,8 +20,8 @@ namespace PuzzlePlayer_Namespace
public string[,] candidateState; public string[,] candidateState;
public Skyscrapers(int boardSize = 6) public Skyscrapers(int boardSize = 6)
{ {
boardState = GetClearBoard(boardSize + 2); boardState = GetClearBoard(boardSize + 2, boardSize + 2);
lastGeneratedBoard = GetClearBoard(boardSize + 2); lastGeneratedBoard = GetClearBoard(boardSize + 2, boardSize + 2);
candidateState = new string[boardSize + 2, boardSize + 2]; candidateState = new string[boardSize + 2, boardSize + 2];
description = "lol"; description = "lol";
drawFactor = 1; drawFactor = 1;
...@@ -101,13 +101,14 @@ namespace PuzzlePlayer_Namespace ...@@ -101,13 +101,14 @@ namespace PuzzlePlayer_Namespace
} }
public override void TileInput(Point? p, Keys k) public override bool TileInput(Point? p, Keys k)
{ {
if (p == null) return; if (p == null) return false;
double center = ((double)boardState.GetLength(0) - 1) / 2; double center = ((double)boardState.GetLength(0) - 1) / 2;
if (Math.Abs(((Point)p).X - center) == center && Math.Abs(((Point)p).Y - center) == center) return; if (Math.Abs(((Point)p).X - center) == center && Math.Abs(((Point)p).Y - center) == center) return false;
int num = (int)k - 48; int num = (int)k - 48;
if (num > 0 && num <= boardState.GetLength(0) - 2) boardState[((Point)p).X, ((Point)p).Y] = num; if (num > 0 && num <= boardState.GetLength(0) - 2) boardState[((Point)p).X, ((Point)p).Y] = num;
return false;
} }
protected override List<Move> GetSolveList(int[,] boardToSolve) protected override List<Move> GetSolveList(int[,] boardToSolve)
......
...@@ -15,7 +15,7 @@ namespace PuzzlePlayer_Namespace ...@@ -15,7 +15,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(); private static Random random = new Random();
public Sudoku(int boardSize = 9) public Sudoku(int boardSize = 4)
{ {
boardState = GetClearBoard(boardSize, boardSize); boardState = GetClearBoard(boardSize, boardSize);
lastGeneratedBoard = GetClearBoard(boardSize, boardSize); lastGeneratedBoard = GetClearBoard(boardSize, boardSize);
...@@ -49,23 +49,26 @@ namespace PuzzlePlayer_Namespace ...@@ -49,23 +49,26 @@ namespace PuzzlePlayer_Namespace
if (boardState[i,j] != -1) if (boardState[i,j] != -1)
{ {
gr.DrawString( if (lastGeneratedBoard[i, j] != Board.emptySpace)
(boardState[i, j]).ToString(), {
new Font("Arial", tilesize.Width / 2), gr.DrawString(
Brushes.Black, (boardState[i, j]).ToString(),
(float)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4), new Font("Arial", tilesize.Width / 2),
(float)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4), Brushes.Black,
format (int)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4),
); (int)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4),
} format);
}
if (lastGeneratedBoard[i, j] != Board.emptySpace) else
{ {
gr.FillRectangle(Brushes.LightGray, gr.DrawString(
(int)(r.X + ((double)i + 0.4375) * tilesize.Width), (boardState[i, j]).ToString(),
(int)(r.Y + ((double)j + 0.4375) * tilesize.Height), new Font("Arial", tilesize.Width / 2),
tilesize.Width / 8, Brushes.DarkBlue,
tilesize.Height / 8); (int)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4),
(int)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4),
format);
}
} }
} }
} }
...@@ -89,14 +92,25 @@ namespace PuzzlePlayer_Namespace ...@@ -89,14 +92,25 @@ namespace PuzzlePlayer_Namespace
boardState = GetClearBoard(boardLength, boardLength); boardState = GetClearBoard(boardLength, boardLength);
for (int i = 0; i < boardLength; i += rootBoardLength) if (boardLength == 4)
{
FillBox(0, 0);
}
else
{ {
FillBox(i, i); for (int i = 0; i < boardLength; i += rootBoardLength)
{
FillBox(i, i);
}
} }
SolveSudoku(); SolveSudoku();
RemoveSpaces((int)(boardLength * boardLength - 30)); solution = (int[,])boardState.Clone();
RemoveSpaces((int)(boardLength * boardLength * 0.5));
lastGeneratedBoard = (int[,])boardState.Clone();
} }
public override SOLUTIONS Solve(bool b) public override SOLUTIONS Solve(bool b)
...@@ -244,35 +258,28 @@ namespace PuzzlePlayer_Namespace ...@@ -244,35 +258,28 @@ namespace PuzzlePlayer_Namespace
return result; return result;
} }
public override void TileInput(Point? p, Keys k) public override bool TileInput(Point? p, Keys k)
{ {
if (p == null) return; if (p == null) return false;
int num = (int)k - 48; int num = (int)k - 48;
if (num >= 1 && num <= boardLength) boardState[((Point)p).X, ((Point)p).Y] = num; if (num >= 1 && num <= boardLength) boardState[((Point)p).X, ((Point)p).Y] = num;
return false;
} }
public override void TileClick(Point p, int x) public override bool TileClick(Point p, int x)
{ {
if (x == 1) x = 1 - 2 * x;
{
x = -1;
}
if (x == 0)
{
x = 1;
}
if (boardState[p.X, p.Y] == emptySpace) if (boardState[p.X, p.Y] == emptySpace)
{ {
if (x == 1) if (x == 1)
{ {
boardState[p.X, p.Y] = 1; boardState[p.X, p.Y] = 1;
return; return IsBoardSolved();
} }
else else
{ {
boardState[p.X, p.Y] = boardLength; boardState[p.X, p.Y] = boardLength;
return; return IsBoardSolved();
} }
} }
else if (boardState[p.X, p.Y] == boardLength) else if (boardState[p.X, p.Y] == boardLength)
...@@ -280,12 +287,12 @@ namespace PuzzlePlayer_Namespace ...@@ -280,12 +287,12 @@ namespace PuzzlePlayer_Namespace
if (x == 1) if (x == 1)
{ {
boardState[p.X, p.Y] = emptySpace; boardState[p.X, p.Y] = emptySpace;
return; return false;
} }
else else
{ {
boardState[p.X, p.Y] += x; boardState[p.X, p.Y] += x;
return; return IsBoardSolved();
} }
} }
else if (boardState[p.X, p.Y] == 1) else if (boardState[p.X, p.Y] == 1)
...@@ -293,18 +300,18 @@ namespace PuzzlePlayer_Namespace ...@@ -293,18 +300,18 @@ namespace PuzzlePlayer_Namespace
if (x == -1) if (x == -1)
{ {
boardState[p.X, p.Y] = emptySpace; boardState[p.X, p.Y] = emptySpace;
return; return false;
} }
else else
{ {
boardState[p.X, p.Y] += x; boardState[p.X, p.Y] += x;
return; return IsBoardSolved();
} }
} }
else else
{ {
boardState[p.X, p.Y] += x; boardState[p.X, p.Y] += x;
return; return IsBoardSolved();
} }
} }
......
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