diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index a7162177b9471c6332253ea1eac8375ecd05c68c..9a07818a78db858a17d5baa682ba71e0469f1cf0 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -69,19 +69,7 @@ namespace PuzzlePlayer_Namespace } } - // static meathode for filling a int[,] with -1 - public static int[,] GetClearBoard(int boardSize) - { - int[,] result = new int[boardSize, boardSize]; - // fill the board with empty spaces (-1) - for (int i = 0; i < boardSize; i++) - { - for (int j = 0; j < boardSize; j++) - result[i, j] = emptySpace; - } - - return result; - } + // generates a random solvable board public override void Generate() diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs index 85245dde2bb7215873d54cf055a769d0d28f6a22..7b38b2bd37c9e71306f965bc96dff8e2dd461e8f 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -44,6 +44,19 @@ namespace PuzzlePlayer_Namespace public int[,] boardState; public int[,] lastGeneratedBoard; + // static meathode for filling a int[,] with -1 + public static int[,] GetClearBoard(int boardSize) + { + int[,] result = new int[boardSize, boardSize]; + // fill the board with empty spaces (-1) + for (int i = 0; i < boardSize; i++) + { + for (int j = 0; j < boardSize; j++) + result[i, j] = emptySpace; + } + + return result; + } // checks if the board is valid and solvable before setting the variable. diff --git a/PuzzlePlayer/Maze.cs b/PuzzlePlayer/Maze.cs new file mode 100644 index 0000000000000000000000000000000000000000..7ff7d1d2de5eeed3f3f2000055972c0a0ba7d22d --- /dev/null +++ b/PuzzlePlayer/Maze.cs @@ -0,0 +1,146 @@ +using System; +using System.Buffers.Binary; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace PuzzlePlayer_Namespace +{ + /* + * all the info about the maze is stored in the mazeState int[,] + * this way the boardState can be used to keep track off the spaces that the player has visited + * + * if the player has not yet visited a space then it is equal to the emptyspace constant + * if the player did visit the space then it is equal to 1 + * + * The maze board consists of cells with a number between 1 and 15 + * the number represents where the walls are + * 1111 is 15 in binary the first bit is the top wall, second bit the right wall, etc. continuing clockwise + * _ + * | | fully surounded is 15, so 1111 in binary + * - + * _ + * | right side open is 11, so 1011 in binary + * - + * + * -1 means that the cell is empty so there are no walls anywhere + * + * the topleft corner is always the starting point (left wall), and the bottom right corner is always the end point (right wall) + * + */ + + class Maze : Board + { + int[,] mazeState; + + public Maze(int size = 3) + { + boardState = GetClearBoard(size); + mazeState = GetClearBoard(size); + + // example thingy + mazeState[0, 0] = 5; + mazeState[1, 0] = 3; + mazeState[2, 0] = 11; + mazeState[0, 1] = 11; + mazeState[1, 1] = 8; + mazeState[2, 1] = 6; + mazeState[0, 2] = 12; + mazeState[1, 2] = 4; + mazeState[2, 2] = 5; + + boardState[0, 2] = 1; + } + + // two funcions to go from number to walls and back + private int getNumberFromWalls(bool top, bool right, bool bottom, bool left) + { + int result = 0; + + if (top) + result++; + if (right) + result += 2; + if (bottom) + result += 4; + if (left) + result += 8; + + if (result == 0) + return emptySpace; //if there are no walls then the space is empty + + return result; + } + + private (bool,bool,bool,bool) getWallsFromNumber(int number) + { + if(number == emptySpace) + return (false,false,false,false); //if the place is empty then there are no walls + + // bitwise and opperations to check each bit + bool top = (number & 1) != 0; + bool right = (number & 2) != 0; + bool bottom = (number & 4) != 0; + bool left = (number & 8) != 0; + + return (top, right, bottom, left); + } + + public override void Draw(Graphics gr, Rectangle r) + { + Size tilesize = new Size(r.Width / boardState.GetLength(0), r.Height / boardState.GetLength(1)); + Pen wall = new Pen(Color.Black, tilesize.Width / 5); + + for(int i = 0; i < boardState.GetLength(0); i++) + 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); + + // draw the space blue if the player has visited it + if (boardState[i, j] == 1) + gr.FillRectangle(Brushes.Blue, currentRect); + + // draw board outline + gr.DrawRectangle(Pens.LightGray,currentRect); + + + // drawing walls + (bool top,bool right,bool bottom,bool left) = getWallsFromNumber(mazeState[i,j]); + + if (top) + gr.DrawLine(wall, currentRect.Left, currentRect.Top, currentRect.Right, currentRect.Top); + if (right) + gr.DrawLine(wall, currentRect.Right, currentRect.Top, currentRect.Right, currentRect.Bottom); + if (bottom) + gr.DrawLine(wall, currentRect.Left, currentRect.Bottom, currentRect.Right, currentRect.Bottom); + if (left) + gr.DrawLine(wall, currentRect.Left, currentRect.Bottom, currentRect.Left, currentRect.Top); + } + + // draw an indication of where the start and end from the maze are + gr.DrawString("START", SettingForm.mainFont, Brushes.Red, r.Left, r.Top + tilesize.Height/2); + gr.DrawString("END", SettingForm.mainFont, Brushes.Red, r.Right - tilesize.Width / 4, r.Bottom - tilesize.Height / 2); + } + + protected override List<Move> GetSolveList(int[,] boardToSolve) + { + throw new NotImplementedException(); + } + + // https://en.wikipedia.org/wiki/Maze_generation_algorithm#Randomized_depth-first_search + public override void Generate() + { + throw new NotImplementedException(); + } + + public override void TileInput(Point p, int x) + { + throw new NotImplementedException(); + } + } +} diff --git a/PuzzlePlayer/PuzzleForm.cs b/PuzzlePlayer/PuzzleForm.cs index 04842aaedff2b8b9a34a6c34b62f77c2821d1ff9..3cddac961bdbbae82e70f62b8f802666f755aff3 100644 --- a/PuzzlePlayer/PuzzleForm.cs +++ b/PuzzlePlayer/PuzzleForm.cs @@ -79,9 +79,6 @@ namespace PuzzlePlayer_Namespace Board = b; CreateUI(); - - Board.boardState[1, 1] = 1; - Board.boardState[2, 2] = 0; } private void CreateUI() //sets up ui elements {