Grid.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.Xna.Framework;
  5. namespace PacMan
  6. {
  7. /// <summary>
  8. /// By who the tile can be traversed
  9. /// </summary>
  10. public enum TileTypes
  11. {
  12. /// <summary>
  13. /// Everyone can go through
  14. /// </summary>
  15. Open,
  16. /// <summary>
  17. /// No one can go through
  18. /// </summary>
  19. Closed,
  20. /// <summary>
  21. /// Under special circumstances ghosts can go there
  22. /// </summary>
  23. Home
  24. }
  25. /// <summary>
  26. /// Represents the maze in terms of tiles. Initializes itself from txt file.
  27. /// </summary>
  28. public static class Grid
  29. {
  30. /// <summary>
  31. /// Creates a new Grid object
  32. /// </summary>
  33. static Grid()
  34. {
  35. initializeFromFile();
  36. }
  37. /// <summary>
  38. /// Reads Grid.txt to get an object grid from the numbers.
  39. /// </summary>
  40. static void initializeFromFile()
  41. {
  42. using (var stream = TitleContainer.OpenStream("Content/Grid.txt"))
  43. using (var reader = new StreamReader(stream))
  44. {
  45. string line = reader.ReadLine();
  46. int lineIndex = 0;
  47. int charIndex = 0;
  48. while (line != null)
  49. {
  50. foreach (char c in line)
  51. {
  52. if (c == '1')
  53. {
  54. TileGrid[charIndex, lineIndex] = new Tile(TileTypes.Open, true, false, new Point(charIndex, lineIndex));
  55. }
  56. else if (c == '0')
  57. {
  58. TileGrid[charIndex, lineIndex] = new Tile(TileTypes.Closed, false, false, new Point(charIndex, lineIndex));
  59. }
  60. else if (c == '2')
  61. {
  62. TileGrid[charIndex, lineIndex] = new Tile(TileTypes.Home, false, false, new Point(charIndex, lineIndex));
  63. }
  64. else if (c == '3')
  65. {
  66. TileGrid[charIndex, lineIndex] = new Tile(TileTypes.Open, true, true, new Point(charIndex, lineIndex));
  67. }
  68. if (c != ' ')
  69. {
  70. charIndex++;
  71. }
  72. }
  73. charIndex = 0;
  74. lineIndex++;
  75. line = reader.ReadLine();
  76. }
  77. reader.Close();
  78. // Now, actually a few open tiles do not contain a crump; such as
  79. // the tunnels and around the ghosts' home.
  80. for (int i = 0; i < 28; i++)
  81. {
  82. if (i != 6 && i != 21)
  83. {
  84. TileGrid[i, 14].HasCrump = false;
  85. }
  86. }
  87. for (int j = 11; j < 20; j++)
  88. {
  89. TileGrid[9, j].HasCrump = false;
  90. TileGrid[18, j].HasCrump = false;
  91. }
  92. for (int i = 10; i < 18; i++)
  93. {
  94. TileGrid[i, 11].HasCrump = false;
  95. TileGrid[i, 17].HasCrump = false;
  96. }
  97. TileGrid[12, 9].HasCrump = false;
  98. TileGrid[15, 9].HasCrump = false;
  99. TileGrid[12, 10].HasCrump = false;
  100. TileGrid[15, 10].HasCrump = false;
  101. TileGrid[13, 23].HasCrump = false;
  102. TileGrid[14, 23].HasCrump = false;
  103. }
  104. }
  105. static Tile[,] tileGrid_ = new Tile[28, 31];
  106. public static Tile[,] TileGrid
  107. {
  108. get { return tileGrid_; }
  109. }
  110. public static int Width
  111. {
  112. get { return 28; }
  113. }
  114. public static int Height
  115. {
  116. get { return 31; }
  117. }
  118. public static int NumCrumps { get; set; }
  119. public static void Reset()
  120. {
  121. NumCrumps = 0;
  122. initializeFromFile();
  123. }
  124. }
  125. /// <summary>
  126. /// A square of the maze
  127. /// </summary>
  128. public struct Tile
  129. {
  130. TileTypes type_;
  131. /// <summary>
  132. /// The type of the tile
  133. /// </summary>
  134. public TileTypes Type
  135. {
  136. get { return type_; }
  137. set { type_ = value; }
  138. }
  139. bool hasCrump_;
  140. /// <summary>
  141. /// Whether the tile has a crump
  142. /// </summary>
  143. public bool HasCrump
  144. {
  145. get { return hasCrump_; }
  146. set
  147. {
  148. if (value != hasCrump_)
  149. {
  150. Grid.NumCrumps += value ? 1 : -1;
  151. }
  152. hasCrump_ = value;
  153. }
  154. }
  155. bool hasPowerPill_;
  156. /// <summary>
  157. /// Whether the tile has a power pill
  158. /// </summary>
  159. public bool HasPowerPill
  160. {
  161. get { return hasPowerPill_; }
  162. set { hasPowerPill_ = value; }
  163. }
  164. public bool IsOpen
  165. {
  166. get { return type_ == TileTypes.Open; }
  167. }
  168. Point position_;
  169. public Point ToPoint { get { return position_; } }
  170. /// <summary>
  171. /// Sets the different attributes
  172. /// </summary>
  173. /// <param name="type">The type of tile</param>
  174. /// <param name="hasCrump">Whether the tile has a crump</param>
  175. /// <param name="hasPowerPill">Whether the tile has a power pill</param>
  176. public Tile(TileTypes type, bool hasCrump, bool hasPowerPill, Point position)
  177. {
  178. type_ = type;
  179. hasCrump_ = hasCrump;
  180. if (hasCrump)
  181. {
  182. Grid.NumCrumps++;
  183. }
  184. hasPowerPill_ = hasPowerPill;
  185. position_ = position;
  186. }
  187. }
  188. }