diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index 8f76b5cb65ead5eee454197a656f7805f0648f01..5f3271810fbaeff380489691771fba7be00d33c5 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -97,7 +97,8 @@ namespace PuzzlePlayer_Namespace //MessageBox.Show($"Found board in {counter} tries"); boardState = (int[,])generatedBoard.Clone(); - + solution = (int[,])generatedBoard.Clone(); + // generate a list with all positions List<(int,int)> allPositions = new List<(int,int)> (); for(int i = 0; i < boardState.GetLength(0); i++) @@ -441,29 +442,36 @@ namespace PuzzlePlayer_Namespace 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; - 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) { boardState[p.X, p.Y] = x; - return; + return IsBoardSolved(); } if (boardState[p.X, p.Y] == x) { boardState[p.X, p.Y] = 1 - boardState[p.X, p.Y]; - return; + return IsBoardSolved(); } if (boardState[p.X, p.Y] == 1 - x) { boardState[p.X, p.Y] = emptySpace; + return false; } - } + return false; + } } } \ No newline at end of file diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs index b7a1fe1b43225910b58144b76aeca65e81dbb626..40c30798adda3fe1cb8a02bcee2472497f648cc1 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -44,6 +44,7 @@ namespace PuzzlePlayer_Namespace // variables to keep track of the board state and the last generated board public int[,] boardState; public int[,] lastGeneratedBoard; + public int[,] solution; // static meathode for filling a int[,] with -1 public static int[,] GetClearBoard(int boardSizeX, int boardSizeY) @@ -80,7 +81,7 @@ namespace PuzzlePlayer_Namespace result[m.x, m.y] = m.changeTo; } } - + // check if the whole board is filled // 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++) @@ -105,13 +106,22 @@ namespace PuzzlePlayer_Namespace public abstract void Generate(); // 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 - 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 - public virtual void TileClick(Point p, int x) { } + public virtual bool TileClick(Point p, int x) { return false; } // is called by restartbutton on PuzzleForm public virtual void Restart() diff --git a/PuzzlePlayer/Maze.cs b/PuzzlePlayer/Maze.cs index aa7f924659de002e54f93313b1dcb091a11f38fe..e533f65c0049fa74419d86a46f43c6c261e94e32 100644 --- a/PuzzlePlayer/Maze.cs +++ b/PuzzlePlayer/Maze.cs @@ -40,7 +40,7 @@ namespace PuzzlePlayer_Namespace Point playerPos; List<Point> shortestPath; - public Maze(int sizeX = 30, int sizeY = 20) + public Maze(int sizeX = 6, int sizeY = 4) { drawFactor = 1; // init all 2D array's @@ -102,7 +102,7 @@ namespace PuzzlePlayer_Namespace Pen wall = new Pen(Color.Black, tilesize.Width / 5); // 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 //Pen wall1 = new Pen(Color.Black, tilesize.Width / 5); @@ -117,7 +117,7 @@ namespace PuzzlePlayer_Namespace for(int j = 0; j < boardState.GetLength(1); j++) { 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 if (boardState[i, j] == 1) @@ -366,7 +366,7 @@ namespace PuzzlePlayer_Namespace 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 (bool top, bool right, bool bottom, bool left) = getWallsFromNumber(mazeState[playerPos.X,playerPos.Y]); @@ -394,9 +394,10 @@ namespace PuzzlePlayer_Namespace playerPos.X--; break; } - // upadte the spaces where the player has walked boardState[playerPos.X, playerPos.Y] = 1; + + return IsBoardSolved(); } public override void Restart() @@ -410,5 +411,11 @@ namespace PuzzlePlayer_Namespace { 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); + } } } diff --git a/PuzzlePlayer/PuzzleForm.cs b/PuzzlePlayer/PuzzleForm.cs index ea1fed3cc2ac720c163a5e193028cc727c5a308b..84e300244d8195d5de40a01fb5aee7fe279631ad 100644 --- a/PuzzlePlayer/PuzzleForm.cs +++ b/PuzzlePlayer/PuzzleForm.cs @@ -242,7 +242,7 @@ namespace PuzzlePlayer_Namespace case Keys.A: case Keys.S: case Keys.D: - Board.TileInput(null, k); + if (Board.TileInput(null, k)) Win(); this.Invalidate(); return; } @@ -257,19 +257,25 @@ namespace PuzzlePlayer_Namespace return; case Keys.LButton: case Keys.RButton: - Board.TileClick(tile, (int)k - 1); + if (Board.TileInput(null, k)) Win(); this.Invalidate(); return; case Keys.OemOpenBrackets: case Keys.OemCloseBrackets: - Board.TileClick(tile, ((int)k - 219)/2); + if (Board.TileClick(tile, ((int)k - 219)/2)) Win(); this.Invalidate(); return; default: - Board.TileInput(tile, k); + if (Board.TileInput(tile, k)) Win(); this.Invalidate(); return; } } + + public void Win() + { + this.Invalidate(); + MessageBox.Show("oi oi oi"); + } } } diff --git a/PuzzlePlayer/Skyscrapers.cs b/PuzzlePlayer/Skyscrapers.cs index b38470d632595a3983839ef42280d78a53142aed..a1e07844e71978d40386202fe9ae2feb4bcb9097 100644 --- a/PuzzlePlayer/Skyscrapers.cs +++ b/PuzzlePlayer/Skyscrapers.cs @@ -20,8 +20,8 @@ namespace PuzzlePlayer_Namespace public string[,] candidateState; public Skyscrapers(int boardSize = 6) { - boardState = GetClearBoard(boardSize + 2); - lastGeneratedBoard = GetClearBoard(boardSize + 2); + boardState = GetClearBoard(boardSize + 2, boardSize + 2); + lastGeneratedBoard = GetClearBoard(boardSize + 2, boardSize + 2); candidateState = new string[boardSize + 2, boardSize + 2]; description = "lol"; drawFactor = 1; @@ -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; - 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; 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) diff --git a/PuzzlePlayer/Sudoku.cs b/PuzzlePlayer/Sudoku.cs index aeadcfe5fb12dc03cd395c762541dee4c202c4c6..ebfb05beb9e66cb2db9b4310198b2d88fb8a483f 100644 --- a/PuzzlePlayer/Sudoku.cs +++ b/PuzzlePlayer/Sudoku.cs @@ -15,7 +15,7 @@ namespace PuzzlePlayer_Namespace private static int boardLength; private static int rootBoardLength; private static Random random = new Random(); - public Sudoku(int boardSize = 9) + public Sudoku(int boardSize = 4) { boardState = GetClearBoard(boardSize, boardSize); lastGeneratedBoard = GetClearBoard(boardSize, boardSize); @@ -49,23 +49,26 @@ namespace PuzzlePlayer_Namespace if (boardState[i,j] != -1) { - gr.DrawString( - (boardState[i, j]).ToString(), - new Font("Arial", tilesize.Width / 2), - Brushes.Black, - (float)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4), - (float)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4), - format - ); - } - - if (lastGeneratedBoard[i, j] != Board.emptySpace) - { - gr.FillRectangle(Brushes.LightGray, - (int)(r.X + ((double)i + 0.4375) * tilesize.Width), - (int)(r.Y + ((double)j + 0.4375) * tilesize.Height), - tilesize.Width / 8, - tilesize.Height / 8); + if (lastGeneratedBoard[i, j] != Board.emptySpace) + { + gr.DrawString( + (boardState[i, j]).ToString(), + new Font("Arial", tilesize.Width / 2), + Brushes.Black, + (int)(r.X + (i + 0.27) * tilesize.Width + tilesize.Width / 4), + (int)(r.Y + (j + 0.33) * tilesize.Height + tilesize.Height / 4), + format); + } + else + { + gr.DrawString( + (boardState[i, j]).ToString(), + new Font("Arial", tilesize.Width / 2), + Brushes.DarkBlue, + (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 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(); - 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) @@ -244,35 +258,28 @@ namespace PuzzlePlayer_Namespace 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; 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; - } - if (x == 0) - { - x = 1; - } + x = 1 - 2 * x; if (boardState[p.X, p.Y] == emptySpace) { if (x == 1) { boardState[p.X, p.Y] = 1; - return; + return IsBoardSolved(); } else { boardState[p.X, p.Y] = boardLength; - return; + return IsBoardSolved(); } } else if (boardState[p.X, p.Y] == boardLength) @@ -280,12 +287,12 @@ namespace PuzzlePlayer_Namespace if (x == 1) { boardState[p.X, p.Y] = emptySpace; - return; + return false; } else { boardState[p.X, p.Y] += x; - return; + return IsBoardSolved(); } } else if (boardState[p.X, p.Y] == 1) @@ -293,18 +300,18 @@ namespace PuzzlePlayer_Namespace if (x == -1) { boardState[p.X, p.Y] = emptySpace; - return; + return false; } else { boardState[p.X, p.Y] += x; - return; + return IsBoardSolved(); } } else { boardState[p.X, p.Y] += x; - return; + return IsBoardSolved(); } }