From 0e381e410a87d7ed4b06ad152108394ebe6ba1fb Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Mon, 2 Dec 2024 23:34:15 +0100 Subject: [PATCH] Implemented Maze draw Function --- PuzzlePlayer/Binary.cs | 14 +--- PuzzlePlayer/Board.cs | 13 ++++ PuzzlePlayer/Maze.cs | 146 +++++++++++++++++++++++++++++++++++++ PuzzlePlayer/PuzzleForm.cs | 3 - 4 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 PuzzlePlayer/Maze.cs diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index a716217..9a07818 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 85245dd..7b38b2b 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 0000000..7ff7d1d --- /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 04842aa..3cddac9 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 { -- GitLab