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.

189 lines
6.3 KiB

  1. /**
  2. * @file Supply.java
  3. *
  4. * @author
  5. * Anastasia Foti AEM:8959
  6. * <anastaskf@ece.auth.gr>
  7. *
  8. * @author
  9. * Christos Choutouridis AEM:8997
  10. * <cchoutou@ece.auth.gr>
  11. */
  12. package host.labyrinth;
  13. /**
  14. * @brief
  15. * This class is the representation of the supplies in the game
  16. *
  17. * Supplies are the game "goodies". They placed randomly in the board.
  18. * In each tile there can be only one supply. The player has to collect all
  19. * of them in order to complete the game successfully.
  20. */
  21. class Supply {
  22. /** @name Constructors */
  23. /** @{ */
  24. /**
  25. * The main constructor of the Supply constructed from (row,column)
  26. *
  27. * @param id The Id of the created supply
  28. * @param row The row coordinate to place the supply
  29. * @param col The column coordinate to place the supply
  30. */
  31. Supply(int id, int row, int col) {
  32. // Boundary checks
  33. assert (row >= 0 && row< Session.boardSize) : "Row coordinate must be in the range [0, Session.boardSize)";
  34. assert (col >= 0 && col< Session.boardSize) : "Column coordinate must be in the range [0, Session.boardSize)";
  35. // Initialization
  36. this.supplyId =id;
  37. this.x =col;
  38. this.y =row;
  39. this.supplyTileId = Position.toID(row, col);
  40. }
  41. /**
  42. * A second constructor of the Supply constructed from supplyTileId
  43. *
  44. * @param id The Id of the created supply
  45. * @param tileId The linear combination of (row, column)
  46. */
  47. Supply(int id, int tileId) {
  48. // Boundary check
  49. assert (id >= 0 && id <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  50. : "TileId must be in the range of [0, Session.boardSize^2)";
  51. // Initialization
  52. this.supplyId = id;
  53. this.supplyTileId = tileId;
  54. this.x = Position.toCol(tileId);
  55. this.y = Position.toRow(tileId);
  56. }
  57. /**
  58. * A deep copy constructor.
  59. */
  60. Supply (Supply s) {
  61. this.supplyId = s.supplyId;
  62. this.x = s.x;
  63. this.y = s.y;
  64. this.supplyTileId = s.supplyTileId;
  65. // We achieve deep copy as the members are all primitives.
  66. }
  67. /** @} */
  68. /** @name Supply's main application interface */
  69. /** @{ */
  70. /** @return the supplyId */
  71. int supplyId () { return supplyId; }
  72. /**
  73. * Set the supplyId
  74. * @param sID The Id to set
  75. * @return The supplyId
  76. * @note This function also returns the supplyId to help in chained expressions.
  77. */
  78. int supplyId (int sID) { return supplyId = sID; }
  79. /**
  80. * @return the position of the supply as a Position object
  81. * @see Position
  82. */
  83. Position position () { return new Position (supplyTileId); }
  84. /**
  85. * Set the position of the supply 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 Position 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.supplyTileId = p.getId();
  100. return p;
  101. }
  102. /**
  103. * Set the position of the supply 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 Position 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.supplyTileId = p.getId(); // =tileId;
  117. return p;
  118. }
  119. /**
  120. * Marks the supply removed. This usually mean the supply is picked up by a user.
  121. */
  122. void removeSupply () { this.supplyId = Const.noSupply; }
  123. /** @} */
  124. /**
  125. * @name Accessor/Mutator interface
  126. * @note
  127. * Please consider not to use mutator interface. Its the abstraction killer :(
  128. * We have added a bit of logic however, in order to make it a bit more safe.
  129. */
  130. /** @{ */
  131. int getSupplyId () { return supplyId; }
  132. int getX() { return x; }
  133. int getY() { return y; }
  134. int getSupplyTileId(){ return supplyTileId; }
  135. void setSupplyId(int Id) { supplyId = Id; }
  136. void setX(int x) {
  137. assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
  138. this.x = x;
  139. this.supplyTileId = Position.toID(this.x, this.y);
  140. }
  141. void setY(int y) {
  142. assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
  143. this.y = y;
  144. this.supplyTileId = Position.toID(this.x, this.y);
  145. }
  146. void setSupplyTileId(int tileId) {
  147. assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  148. : "TileId must be in the range of [0, Session.boardSize^2)";
  149. this.supplyTileId = tileId;
  150. this.x = Position.toCol(tileId);
  151. this.y = Position.toRow(tileId);
  152. }
  153. /** @} */
  154. /** @name Class data */
  155. /** @{ */
  156. private int supplyId; /**< The unique identifier of the tile. This must not be confused with TileID */
  157. private int x; /**< The x coordinate of the tile as if the board lies in the 1st quadrant */
  158. private int y; /**< The y coordinate of the tile as if the board lies in the 1st quadrant */
  159. private int supplyTileId; /**< The Id of the tile on the board, in witch the supply is located */
  160. /**
  161. * @warning
  162. * We can calculate tileId from (x,y) so having both (x,y) and tile ID is error
  163. * prone and not a good practice. Its easy to get them out of sync (code smell).
  164. * We implement it just because its in the requirements of the assignment.
  165. * @see Tile.tileId
  166. */
  167. /** @} */
  168. }