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.

179 lines
6.1 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. /**
  116. * @name Accessor/Mutator interface
  117. * @note
  118. * Please consider not to use mutator interface. Its the abstraction killer :(
  119. * We have added a bit of logic however, in order to make it a bit more safe.
  120. */
  121. /** @{ */
  122. int getSupplyId () { return supplyId; }
  123. int getX() { return x; }
  124. int getY() { return y; }
  125. int getSupplyTileId(){ return supplyTileId; }
  126. void setSupplyId(int Id) { supplyId = Id; }
  127. void setX(int x) {
  128. assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
  129. this.x = x;
  130. this.supplyTileId = Position.toID(this.x, this.y);
  131. }
  132. void setY(int y) {
  133. assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
  134. this.y = y;
  135. this.supplyTileId = Position.toID(this.x, this.y);
  136. }
  137. void setSupplyTileId(int tileId) {
  138. assert (tileId >= 0 && tileId <= Position.toID(Session.boardSize-1, Session.boardSize-1))
  139. : "TileId must be in the range of [0, Session.boardSize^2)";
  140. this.supplyTileId = tileId;
  141. this.x = Position.toCol(tileId);
  142. this.y = Position.toRow(tileId);
  143. }
  144. /** @} */
  145. /** @name Class data */
  146. /** @{ */
  147. private int supplyId; /**< The unique identifier of the tile. This must not be confused with TileID */
  148. private int x; /**< The x coordinate of the tile as if the board lies in the 1st quadrant */
  149. private int y; /**< The y coordinate of the tile as if the board lies in the 1st quadrant */
  150. private int supplyTileId; /**< The Id of the tile on the board, in witch the supply is located */
  151. /**
  152. * @warning
  153. * We can calculate tileId from (x,y) so having both (x,y) and tile ID is error
  154. * prone and not a good practice. Its easy to get them out of sync (code smell).
  155. * We implement it just because its in the requirements of the assignment.
  156. * @see Tile.tileId
  157. */
  158. /** @} */
  159. }