You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
8.5 KiB

  1. /**
  2. * @file Tile.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.labyrinth;
  8. /**
  9. * @brief
  10. * This class is the representation of the board's tile
  11. *
  12. * Tiles are arranged on the board in square shape and they're identified by
  13. * an ID. This ID is the linear combination of x and y coordinate of the tile.
  14. */
  15. class Tile {
  16. /** @name Constructors */
  17. /** @{ */
  18. /**
  19. * The main constructor of the Tile constructed from (row,column)
  20. *
  21. * @param row The row coordinate to place the Tile
  22. * @param col The column coordinate to place the Tile
  23. * @param up The existence of wall north of the tile
  24. * @param down The existence of wall south of the tile
  25. * @param left The existence of wall left of the tile
  26. * @param right The existence of wall right of the tile
  27. */
  28. Tile(int row, int col, boolean up, boolean down, boolean left, boolean right) {
  29. // Boundary checks
  30. assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
  31. assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
  32. // Initialization
  33. this.tileId = Position.toID(row, col);
  34. this.x =col;
  35. this.y =row;
  36. this.up = up;
  37. this.down = down;
  38. this.left = left;
  39. this.right = right;
  40. }
  41. /**
  42. * The main constructor of the Tile constructed from tileId
  43. *
  44. * @param id The tileId to place the Tile
  45. * @param up The existence of wall north of the tile
  46. * @param down The existence of wall south of the tile
  47. * @param left The existence of wall left of the tile
  48. * @param right The existence of wall right of the tile
  49. */
  50. Tile(int id, boolean up, boolean down, boolean left, boolean right) {
  51. // Boundary check
  52. assert (id >= 0 && id <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  53. : "TileId must be in the range of [0, Session.boardSize^2)";
  54. // Initialization
  55. this.tileId = id;
  56. this.x =Position.toCol(id);
  57. this.y =Position.toRow(id);
  58. this.up = up;
  59. this.down = down;
  60. this.left = left;
  61. this.right = right;
  62. }
  63. /**
  64. * A deep copy constructor.
  65. */
  66. Tile (Tile t) {
  67. this.tileId = t.tileId;
  68. this.x = t.x;
  69. this.y = t.y;
  70. this.up = t.up;
  71. this.down = t.down;
  72. this.left = t.left;
  73. this.right = t.right;
  74. // We achieve deep copy as the members are all primitives.
  75. }
  76. /** @} */
  77. /** @name Supply's main application interface */
  78. /** @{ */
  79. /**
  80. * @return the position of the tile as a Position object
  81. * @see Position
  82. */
  83. Position position () { return new Position (tileId); }
  84. /**
  85. * Set the position of the tile from a (row, column) pair
  86. * @param row The row coordinate of the tile
  87. * @param col The column coordinate of the tile
  88. * @return the position of the supply as a Position object
  89. * @note This function also returns the supplyId to help in chained expressions.
  90. * @see Position
  91. */
  92. Position position (int row, int col) {
  93. // Boundary checks
  94. assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
  95. assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
  96. Position p = new Position (row, col);
  97. this.x = p.getCol(); // =col;
  98. this.y = p.getRow(); // =row;
  99. this.tileId = p.getId();
  100. return p;
  101. }
  102. /**
  103. * Set the position of the tile from a tileId
  104. * @param tileId The tileId position
  105. * @return The position of the supply as Position object
  106. * @note This function also returns the supplyId to help in chained expressions.
  107. * @see Position
  108. */
  109. Position position (int tileId) {
  110. // Boundary check
  111. assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  112. : "TileId must be in the range of [0, Session.boardSize^2)";
  113. Position p = new Position (tileId);
  114. this.x = p.getCol();
  115. this.y = p.getRow();
  116. this.tileId = p.getId(); // =tileId;
  117. return p;
  118. }
  119. /**
  120. * Sets the tile's wall in the requested direction.
  121. * @param up The direction for the wall.
  122. */
  123. void setWall (int direction) {
  124. switch (direction) {
  125. case Direction.UP: this.up = true; break;
  126. case Direction.DOWN: this.down = true; break;
  127. case Direction.LEFT: this.left = true; break;
  128. case Direction.RIGHT:this.right = true; break;
  129. }
  130. }
  131. /**
  132. * Clears the tile's wall in the requested direction.
  133. * @param up The direction for the wall
  134. */
  135. void clearWall (int direction) {
  136. switch (direction) {
  137. case Direction.UP: this.up = false; break;
  138. case Direction.DOWN: this.down = false; break;
  139. case Direction.LEFT: this.left = false; break;
  140. case Direction.RIGHT:this.right = false; break;
  141. }
  142. }
  143. /**
  144. * Checks if the tile has wall in the requested direction
  145. * @param direction The direction to check
  146. * @return True if there is a wall
  147. */
  148. boolean hasWall (int direction) {
  149. switch (direction) {
  150. case Direction.UP: return up;
  151. case Direction.RIGHT: return right;
  152. case Direction.DOWN: return down;
  153. case Direction.LEFT: return left;
  154. }
  155. return false;
  156. }
  157. /**
  158. * Checks if the tile has walls and return the number of them
  159. * @return The number of walls
  160. */
  161. int hasWalls () {
  162. return ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
  163. }
  164. int hasSupply (Supply[] supplies) {
  165. for (Supply s: supplies)
  166. if (s.position().getId() == position().getId())
  167. return s.getSupplyId();
  168. return Const.noSupply;
  169. }
  170. /** @} */
  171. /**
  172. * @name Accessor/Mutator interface
  173. * @note
  174. * Please consider not to use mutator interface. Its the abstraction killer :(
  175. * We have added a bit of logic however, in order to make it a bit more safe.
  176. */
  177. /** @{ */
  178. int getTileId () { return tileId; }
  179. int getX () { return x; }
  180. int getY () { return y; }
  181. boolean getUp () { return up; }
  182. boolean getDown () { return down; }
  183. boolean getLeft () { return left; }
  184. boolean getRight () { return right; }
  185. void setTileId(int tileId) {
  186. assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  187. : "TileId must be in the range of [0, Session.boardSize^2)";
  188. this.tileId = tileId;
  189. this.x = Position.toCol(tileId);
  190. this.y = Position.toRow(tileId);
  191. }
  192. void setX(int x) {
  193. assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
  194. this.x = x;
  195. this.tileId = Position.toID(this.x, this.y);
  196. }
  197. void setY(int y) {
  198. assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
  199. this.y = y;
  200. this.tileId = Position.toID(this.x, this.y);
  201. }
  202. void setUp(boolean up) { this.up = up; }
  203. void setDown(boolean down) { this.down = down; }
  204. void setRight(boolean right) { this.right = right; }
  205. void setLeft(boolean left) { this.left = left; }
  206. /** @} */
  207. /** @name Class data */
  208. /** @{ */
  209. private int tileId; /**<
  210. * The unique identifier of the tile. This is the linear combination of
  211. * x and y coordinates of the tile
  212. */
  213. private int x; /**< The x coordinate(column) of the tile as if the board lies in the 1st quadrant */
  214. private int y; /**< The y coordinate(row) of the tile as if the board lies in the 1st quadrant */
  215. private boolean up; /**< Indicator of a wall in the north side of the tile */
  216. private boolean down; /**< Indicator of a wall in the south side of the tile */
  217. private boolean left; /**< Indicator of a wall in the left side of the tile */
  218. private boolean right; /**< Indicator of a wall in the right side of the tile */
  219. /**
  220. * @warning
  221. * We can calculate tileId from (x,y) so having both (x,y) and tile ID is error
  222. * prone and not a good practice. Its easy to get them out of sync(code smell).
  223. * We implement it just because its in the requirements of the assignment.
  224. * @see Supply.supplyTileId
  225. */
  226. }