Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Drawing;
- namespace MazeSolution
- {
- /// <summary>
- /// Summary description for Cell.
- /// </summary>
- public class Cell
- {
- public enum CellState
- {
- FREE = 0,
- WALLA = 1,
- WALLB = 2,
- WALLC = 3,
- WALLD = 4,
- WALLE = 5,
- WALLF = 6,
- WALLG = 7,
- WALLH = 8,
- WALLI = 9,
- WALLJ = 10,
- WALLK = 11,
- WALLL = 12,
- WALLM = 13,
- WALLN = 14,
- WALLO = 15,
- WALLP = 16
- }
- public const int PADDING = 20;
- public static int kCellSize = 20;
- public int[] Walls = new int[4]{1, 1, 1, 1};
- public CellState cellState = CellState.FREE;
- public int Row;
- public int Column;
- private long Seed = DateTime.Now.Ticks;
- public static Random TheRandom;
- public Cell()
- {
- TheRandom = new Random((int)Seed);
- }
- public bool HasAllWalls()
- {
- for (int i = 0; i < 4; i++)
- {
- if (Walls[i] == 0)
- return false;
- }
- return true;
- }
- public void KnockDownWall(int theWall)
- {
- Walls[theWall] = 0;
- }
- public void KnockDownWall(Cell theCell)
- {
- // find adjacent wall
- int theWallToKnockDown = FindAdjacentWall(theCell);
- Walls[theWallToKnockDown] = 0;
- int oppositeWall = (theWallToKnockDown + 2) % 4;
- theCell.Walls[oppositeWall] = 0;
- }
- public int FindAdjacentWall(Cell theCell)
- {
- if (theCell.Row == Row)
- {
- if (theCell.Column < Column)
- return 0;
- else
- return 2;
- }
- else // columns are the same
- {
- if (theCell.Row < Row)
- return 1;
- else
- return 3;
- }
- }
- public int GetRandomWall()
- {
- int nextWall = TheRandom.Next(0, 3);
- while ( (Walls[nextWall] == 0)
- || ((Row == 0) && (nextWall == 0)) ||
- ((Row == Maze.kDimension - 1) && (nextWall == 2)) ||
- ((Column == 0) && (nextWall == 1)) ||
- ((Column == Maze.kDimension - 1) && (nextWall == 3))
- )
- {
- nextWall = TheRandom.Next(0, 3);
- }
- return nextWall;
- }
- public void Draw(Graphics g)
- {
- Pen fillPen = null;
- Brush fillBrush = null;
- // Each wall type is drawn in a different color.
- switch( cellState )
- {
- case CellState.WALLA:
- fillPen = new Pen( Color.BlueViolet );
- fillBrush = Brushes.BlueViolet;
- break;
- case CellState.WALLB:
- fillPen = new Pen( Color.DarkViolet );
- fillBrush = Brushes.DarkViolet;
- break;
- case CellState.WALLC:
- fillPen = new Pen( Color.SlateBlue );
- fillBrush = Brushes.SlateBlue;
- break;
- case CellState.WALLD:
- fillPen = new Pen( Color.MediumPurple );
- fillBrush = Brushes.MediumPurple;
- break;
- case CellState.WALLE:
- fillPen = new Pen( Color.Green );
- fillBrush = Brushes.Green;
- break;
- case CellState.WALLF:
- fillPen = new Pen( Color.DarkGreen );
- fillBrush = Brushes.DarkGreen;
- break;
- case CellState.WALLG:
- fillPen = new Pen( Color.MidnightBlue );
- fillBrush = Brushes.MidnightBlue;
- break;
- case CellState.WALLH:
- fillPen = new Pen( Color.Indigo );
- fillBrush = Brushes.Indigo;
- break;
- case CellState.WALLI:
- fillPen = new Pen( Color.Navy );
- fillBrush = Brushes.Navy;
- break;
- case CellState.WALLJ:
- fillPen = new Pen( Color.RoyalBlue );
- fillBrush = Brushes.RoyalBlue;
- break;
- case CellState.WALLK:
- fillPen = new Pen( Color.SlateGray );
- fillBrush = Brushes.SlateGray;
- break;
- case CellState.WALLL:
- fillPen = new Pen( Color.DarkOliveGreen );
- fillBrush = Brushes.DarkOliveGreen;
- break;
- case CellState.WALLM:
- fillPen = new Pen( Color.DarkSlateBlue );
- fillBrush = Brushes.DarkSlateBlue;
- break;
- case CellState.WALLN:
- fillPen = new Pen( Color.DodgerBlue );
- fillBrush = Brushes.DodgerBlue;
- break;
- case CellState.WALLO:
- fillPen = new Pen( Color.ForestGreen );
- fillBrush = Brushes.ForestGreen;
- break;
- case CellState.WALLP:
- fillPen = new Pen( Color.SkyBlue );
- fillBrush = Brushes.SkyBlue;
- break;
- }
- // First fill in all cells that are not free...
- if ( (int)cellState > (int)CellState.FREE )
- {
- g.FillRectangle(fillBrush, Row*kCellSize + PADDING + 1, Column*kCellSize + PADDING + 1, kCellSize - 1, kCellSize - 1);
- if (Walls[0] == 0)
- g.DrawLine(fillPen, Row*kCellSize + PADDING, Column*kCellSize + PADDING, (Row+1) * kCellSize + PADDING, Column*kCellSize + + PADDING);
- if (Walls[1] == 0)
- g.DrawLine(fillPen, Row*kCellSize + PADDING, Column*kCellSize + PADDING, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
- if (Walls[2] == 0)
- g.DrawLine(fillPen, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
- if (Walls[3] == 0)
- g.DrawLine(fillPen, (Row+1)*kCellSize + PADDING , Column*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
- }
- // Then draw the cell walls.
- if (Walls[0] == 1)
- g.DrawLine(Pens.Blue, Row*kCellSize + PADDING, Column*kCellSize + PADDING, (Row+1) * kCellSize + PADDING, Column*kCellSize + + PADDING);
- if (Walls[1] == 1)
- g.DrawLine(Pens.Blue, Row*kCellSize + PADDING, Column*kCellSize + PADDING, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
- if (Walls[2] == 1)
- g.DrawLine(Pens.Blue, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
- if (Walls[3] == 1)
- g.DrawLine(Pens.Blue,(Row+1)*kCellSize + PADDING , Column*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
- }
- }
- }
Add Comment
Please, Sign In to add comment