Guest User

Cell.cs

a guest
Dec 1st, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Drawing;
  4.  
  5. namespace MazeSolution
  6. {
  7. /// <summary>
  8. /// Summary description for Cell.
  9. /// </summary>
  10. public class Cell
  11. {
  12. public enum CellState
  13. {
  14. FREE = 0,
  15. WALLA = 1,
  16. WALLB = 2,
  17. WALLC = 3,
  18. WALLD = 4,
  19. WALLE = 5,
  20. WALLF = 6,
  21. WALLG = 7,
  22. WALLH = 8,
  23. WALLI = 9,
  24. WALLJ = 10,
  25. WALLK = 11,
  26. WALLL = 12,
  27. WALLM = 13,
  28. WALLN = 14,
  29. WALLO = 15,
  30. WALLP = 16
  31. }
  32.  
  33. public const int PADDING = 20;
  34. public static int kCellSize = 20;
  35. public int[] Walls = new int[4]{1, 1, 1, 1};
  36. public CellState cellState = CellState.FREE;
  37. public int Row;
  38. public int Column;
  39. private long Seed = DateTime.Now.Ticks;
  40. public static Random TheRandom;
  41.  
  42.  
  43. public Cell()
  44. {
  45. TheRandom = new Random((int)Seed);
  46. }
  47.  
  48. public bool HasAllWalls()
  49. {
  50. for (int i = 0; i < 4; i++)
  51. {
  52. if (Walls[i] == 0)
  53. return false;
  54. }
  55.  
  56. return true;
  57. }
  58.  
  59. public void KnockDownWall(int theWall)
  60. {
  61. Walls[theWall] = 0;
  62. }
  63.  
  64. public void KnockDownWall(Cell theCell)
  65. {
  66. // find adjacent wall
  67. int theWallToKnockDown = FindAdjacentWall(theCell);
  68. Walls[theWallToKnockDown] = 0;
  69. int oppositeWall = (theWallToKnockDown + 2) % 4;
  70. theCell.Walls[oppositeWall] = 0;
  71. }
  72.  
  73.  
  74. public int FindAdjacentWall(Cell theCell)
  75. {
  76. if (theCell.Row == Row)
  77. {
  78. if (theCell.Column < Column)
  79. return 0;
  80. else
  81. return 2;
  82. }
  83. else // columns are the same
  84. {
  85. if (theCell.Row < Row)
  86. return 1;
  87. else
  88. return 3;
  89. }
  90. }
  91.  
  92. public int GetRandomWall()
  93. {
  94. int nextWall = TheRandom.Next(0, 3);
  95. while ( (Walls[nextWall] == 0)
  96. || ((Row == 0) && (nextWall == 0)) ||
  97. ((Row == Maze.kDimension - 1) && (nextWall == 2)) ||
  98. ((Column == 0) && (nextWall == 1)) ||
  99. ((Column == Maze.kDimension - 1) && (nextWall == 3))
  100. )
  101. {
  102. nextWall = TheRandom.Next(0, 3);
  103. }
  104.  
  105. return nextWall;
  106. }
  107.  
  108. public void Draw(Graphics g)
  109. {
  110. Pen fillPen = null;
  111. Brush fillBrush = null;
  112.  
  113. // Each wall type is drawn in a different color.
  114. switch( cellState )
  115. {
  116. case CellState.WALLA:
  117. fillPen = new Pen( Color.BlueViolet );
  118. fillBrush = Brushes.BlueViolet;
  119. break;
  120. case CellState.WALLB:
  121. fillPen = new Pen( Color.DarkViolet );
  122. fillBrush = Brushes.DarkViolet;
  123. break;
  124. case CellState.WALLC:
  125. fillPen = new Pen( Color.SlateBlue );
  126. fillBrush = Brushes.SlateBlue;
  127. break;
  128. case CellState.WALLD:
  129. fillPen = new Pen( Color.MediumPurple );
  130. fillBrush = Brushes.MediumPurple;
  131. break;
  132. case CellState.WALLE:
  133. fillPen = new Pen( Color.Green );
  134. fillBrush = Brushes.Green;
  135. break;
  136. case CellState.WALLF:
  137. fillPen = new Pen( Color.DarkGreen );
  138. fillBrush = Brushes.DarkGreen;
  139. break;
  140. case CellState.WALLG:
  141. fillPen = new Pen( Color.MidnightBlue );
  142. fillBrush = Brushes.MidnightBlue;
  143. break;
  144. case CellState.WALLH:
  145. fillPen = new Pen( Color.Indigo );
  146. fillBrush = Brushes.Indigo;
  147. break;
  148. case CellState.WALLI:
  149. fillPen = new Pen( Color.Navy );
  150. fillBrush = Brushes.Navy;
  151. break;
  152. case CellState.WALLJ:
  153. fillPen = new Pen( Color.RoyalBlue );
  154. fillBrush = Brushes.RoyalBlue;
  155. break;
  156. case CellState.WALLK:
  157. fillPen = new Pen( Color.SlateGray );
  158. fillBrush = Brushes.SlateGray;
  159. break;
  160. case CellState.WALLL:
  161. fillPen = new Pen( Color.DarkOliveGreen );
  162. fillBrush = Brushes.DarkOliveGreen;
  163. break;
  164. case CellState.WALLM:
  165. fillPen = new Pen( Color.DarkSlateBlue );
  166. fillBrush = Brushes.DarkSlateBlue;
  167. break;
  168. case CellState.WALLN:
  169. fillPen = new Pen( Color.DodgerBlue );
  170. fillBrush = Brushes.DodgerBlue;
  171. break;
  172. case CellState.WALLO:
  173. fillPen = new Pen( Color.ForestGreen );
  174. fillBrush = Brushes.ForestGreen;
  175. break;
  176. case CellState.WALLP:
  177. fillPen = new Pen( Color.SkyBlue );
  178. fillBrush = Brushes.SkyBlue;
  179. break;
  180. }
  181.  
  182. // First fill in all cells that are not free...
  183. if ( (int)cellState > (int)CellState.FREE )
  184. {
  185. g.FillRectangle(fillBrush, Row*kCellSize + PADDING + 1, Column*kCellSize + PADDING + 1, kCellSize - 1, kCellSize - 1);
  186.  
  187. if (Walls[0] == 0)
  188. g.DrawLine(fillPen, Row*kCellSize + PADDING, Column*kCellSize + PADDING, (Row+1) * kCellSize + PADDING, Column*kCellSize + + PADDING);
  189. if (Walls[1] == 0)
  190. g.DrawLine(fillPen, Row*kCellSize + PADDING, Column*kCellSize + PADDING, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
  191. if (Walls[2] == 0)
  192. g.DrawLine(fillPen, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
  193. if (Walls[3] == 0)
  194. g.DrawLine(fillPen, (Row+1)*kCellSize + PADDING , Column*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
  195. }
  196.  
  197. // Then draw the cell walls.
  198. if (Walls[0] == 1)
  199. g.DrawLine(Pens.Blue, Row*kCellSize + PADDING, Column*kCellSize + PADDING, (Row+1) * kCellSize + PADDING, Column*kCellSize + + PADDING);
  200.  
  201. if (Walls[1] == 1)
  202. g.DrawLine(Pens.Blue, Row*kCellSize + PADDING, Column*kCellSize + PADDING, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
  203.  
  204. if (Walls[2] == 1)
  205. g.DrawLine(Pens.Blue, Row*kCellSize + PADDING, (Column+1)*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
  206.  
  207. if (Walls[3] == 1)
  208. g.DrawLine(Pens.Blue,(Row+1)*kCellSize + PADDING , Column*kCellSize + PADDING, (Row+1)*kCellSize + PADDING, (Column+1)*kCellSize + PADDING);
  209. }
  210.  
  211.  
  212. }
  213. }
Add Comment
Please, Sign In to add comment