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.

184 lines
6.2 KiB

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