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.

186 lines
6.2 KiB

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