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.

240 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. protected 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. protected 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. protected 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. protected 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. protected 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. protected 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. * Set the tile's walls
  121. * @param up Request for north wall (winter is coming)
  122. * @param down Request for south wall
  123. * @param left Request for west wall
  124. * @param right Request for east wall
  125. */
  126. protected void setWalls (boolean up, boolean down, boolean left, boolean right) {
  127. assert (
  128. ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0)) <=2
  129. ) : "A tile can have at most 2 walls";
  130. this.up = (up) ? true : this.up;
  131. this.down = (down) ? true : this.down;
  132. this.left = (left) ? true : this.left;
  133. this.right = (right)? true : this.right;
  134. }
  135. /**
  136. * Checks if the tile has wall in the requested direction
  137. * @param direction The direction to check
  138. * @return True if there is a wall
  139. */
  140. protected boolean hasWall (int direction) {
  141. switch (direction) {
  142. case Direction.UP: return up;
  143. case Direction.RIGHT: return right;
  144. case Direction.DOWN: return down;
  145. case Direction.LEFT: return left;
  146. }
  147. return false;
  148. }
  149. /**
  150. * Checks if the tile has walls and return the number of them
  151. * @return The number of walls
  152. */
  153. protected int hasWalls () {
  154. return ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
  155. }
  156. protected int hasSupply (Supply[] supplies) {
  157. for (Supply s: supplies)
  158. if (s.position().getId() == position().getId())
  159. return s.getSupplyId();
  160. return Const.noSupply;
  161. }
  162. /** @} */
  163. /**
  164. * @name Accessor/Mutator interface
  165. * @note
  166. * Please consider not to use mutator interface. Its the abstraction killer :(
  167. * We have added a bit of logic however, in order to make it a bit more safe.
  168. */
  169. /** @{ */
  170. protected int getTileId () { return tileId; }
  171. protected int getX () { return x; }
  172. protected int getY () { return y; }
  173. protected boolean getUp () { return up; }
  174. protected boolean getDown () { return down; }
  175. protected boolean getLeft () { return left; }
  176. protected boolean getRight () { return right; }
  177. protected void setTileId(int tileId) {
  178. assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  179. : "TileId must be in the range of [0, Session.boardSize^2)";
  180. this.tileId = tileId;
  181. this.x = Position.toCol(tileId);
  182. this.y = Position.toRow(tileId);
  183. }
  184. protected void setX(int x) {
  185. assert (x >= 0 && x< Session.boardSize) : "X coordinate must be in the range [0, Session.boardSize)";
  186. this.x = x;
  187. this.tileId = Position.toID(this.x, this.y);
  188. }
  189. protected void setY(int y) {
  190. assert (y >= 0 && y< Session.boardSize) : "Y coordinate must be in the range [0, Session.boardSize)";
  191. this.y = y;
  192. this.tileId = Position.toID(this.x, this.y);
  193. }
  194. protected void setUp(boolean up) { this.up = up; }
  195. protected void setDown(boolean down) { this.down = down; }
  196. protected void setRight(boolean right) { this.right = right; }
  197. protected void setLeft(boolean left) { this.left = left; }
  198. /** @} */
  199. /** @name Class data */
  200. /** @{ */
  201. private int tileId; /**<
  202. * The unique identifier of the tile. This is the linear combination of
  203. * x and y coordinates of the tile
  204. */
  205. private int x; /**< The x coordinate(column) of the tile as if the board lies in the 1st quadrant */
  206. private int y; /**< The y coordinate(row) of the tile as if the board lies in the 1st quadrant */
  207. private boolean up; /**< Indicator of a wall in the north side of the tile */
  208. private boolean down; /**< Indicator of a wall in the south side of the tile */
  209. private boolean left; /**< Indicator of a wall in the left side of the tile */
  210. private boolean right; /**< Indicator of a wall in the right side of the tile */
  211. /**
  212. * @warning
  213. * We can calculate tileId from (x,y) so having both (x,y) and tile ID is error
  214. * prone and not a good practice. Its easy to get them out of sync(code smell).
  215. * We implement it just because its in the requirements of the assignment.
  216. * @see Supply.supplyTileId
  217. */
  218. }