Skip to content
Snippets Groups Projects
Commit 0e381e41 authored by bionic85's avatar bionic85
Browse files

Implemented Maze draw Function

parent 55b0a16b
No related branches found
No related tags found
No related merge requests found
...@@ -69,19 +69,7 @@ namespace PuzzlePlayer_Namespace ...@@ -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 // generates a random solvable board
public override void Generate() public override void Generate()
......
...@@ -44,6 +44,19 @@ namespace PuzzlePlayer_Namespace ...@@ -44,6 +44,19 @@ namespace PuzzlePlayer_Namespace
public int[,] boardState; public int[,] boardState;
public int[,] lastGeneratedBoard; 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. // checks if the board is valid and solvable before setting the variable.
......
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();
}
}
}
...@@ -79,9 +79,6 @@ namespace PuzzlePlayer_Namespace ...@@ -79,9 +79,6 @@ namespace PuzzlePlayer_Namespace
Board = b; Board = b;
CreateUI(); CreateUI();
Board.boardState[1, 1] = 1;
Board.boardState[2, 2] = 0;
} }
private void CreateUI() //sets up ui elements private void CreateUI() //sets up ui elements
{ {
......
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