2
0

Grid.cs 5.2 KB

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