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 (x,y)
  21. *
  22. * @param id The Id of the created supply
  23. * @param x The x coordinate to place the supply
  24. * @param y The y coordinate to place the supply
  25. */
  26. protected Supply(int id, int x, int y) {
  27. // Boundary checks
  28. assert (x >= 0 && x< Defaults.boardSize) : "X coordinate must be in the range [0, Defaults.boardSize)";
  29. assert (x >= 0 && x< Defaults.boardSize) : "Y coordinate must be in the range [0, Defaults.boardSize)";
  30. // Initialization
  31. this.supplyId =id;
  32. this.x =x;
  33. this.y =y;
  34. this.supplyTileId = Position.toID(x, y);
  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 (x, y)
  41. */
  42. protected Supply(int id, int tileId) {
  43. // Boundary check
  44. assert (id >= 0 && id <= Position.toID(Defaults.boardSize-1, Defaults.boardSize-1))
  45. : "TileId must be in the range of [0, Defaults.boardSize^2)";
  46. // Initialization
  47. this.supplyId =id;
  48. this.supplyTileId = tileId;
  49. this.x = Position.toX(tileId);
  50. this.y = Position.toY(tileId);
  51. }
  52. /**
  53. * A deep copy constructor.
  54. */
  55. protected 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. protected 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. protected int supplyId (int sID) { return supplyId = sID; }
  74. /**
  75. * @return the position of the supply as a Position object
  76. * @see Position
  77. */
  78. protected Position position () { return new Position (x, y); }
  79. /**
  80. * Set the position of the supply from a (x, y) pair
  81. * @param x The x coordinate of the tile
  82. * @param y The y coordinate of the tile
  83. * @return the position of the supply as a Position object
  84. * @note This function also returns the supplyId to help in chained expressions.
  85. * @see Position
  86. */
  87. protected Position position (int x, int y) {
  88. // Boundary checks
  89. assert (x >= 0 && x< Defaults.boardSize) : "X coordinate must be in the range [0, Defaults.boardSize)";
  90. assert (x >= 0 && x< Defaults.boardSize) : "Y coordinate must be in the range [0, Defaults.boardSize)";
  91. Position p = new Position (x, y);
  92. this.x = x;
  93. this.y = y;
  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 supplyId to help in chained expressions.
  102. * @see Position
  103. */
  104. protected int position (int tileId) {
  105. // Boundary check
  106. assert (tileId >= 0 && tileId <= Position.toID(Defaults.boardSize-1, Defaults.boardSize-1))
  107. : "TileId must be in the range of [0, Defaults.boardSize^2)";
  108. this.x = Position.toX(tileId);
  109. this.y = Position.toY(tileId);
  110. this.supplyTileId = tileId;
  111. return supplyTileId;
  112. }
  113. /** @return The supplyTileId */
  114. protected int positionId () { return supplyTileId; }
  115. /** @} */
  116. /**
  117. * @name Accessor/Mutator interface
  118. * @note
  119. * Please consider not to use mutator interface. Its the abstraction killer :(
  120. * We have added a bit of logic however, in order to make it a bit more safe.
  121. */
  122. /** @{ */
  123. protected int getSupplyId () { return supplyId; }
  124. protected int getX() { return x; }
  125. protected int getY() { return y; }
  126. protected int getSupplyTileId(){ return supplyTileId; }
  127. protected void setSupplyId(int Id) { supplyId = Id; }
  128. protected void setX(int x) {
  129. assert (x >= 0 && x< Defaults.boardSize) : "X coordinate must be in the range [0, Defaults.boardSize)";
  130. this.x = x;
  131. this.supplyTileId = Position.toID(this.x, this.y);
  132. }
  133. protected void setY(int y) {
  134. assert (y >= 0 && y< Defaults.boardSize) : "X coordinate must be in the range [0, Defaults.boardSize)";
  135. this.y = y;
  136. this.supplyTileId = Position.toID(this.x, this.y);
  137. }
  138. protected void setSupplyTileId(int tileId) {
  139. assert (tileId >= 0 && tileId <= Position.toID(Defaults.boardSize-1, Defaults.boardSize-1))
  140. : "TileId must be in the range of [0, Defaults.boardSize^2)";
  141. this.supplyTileId = tileId;
  142. this.x = Position.toX(tileId);
  143. this.y = Position.toY(tileId);
  144. }
  145. /** @} */
  146. /** @name Class data */
  147. /** @{ */
  148. private int supplyId; /**< The unique identifier of the tile. This must not be confused with TileID */
  149. private int x; /**< The x coordinate of the tile as if the board lies in the 1st quadrant */
  150. private int y; /**< The y coordinate of the tile as if the board lies in the 1st quadrant */
  151. private int supplyTileId; /**< The Id of the tile on the board, in witch the supply is located */
  152. /**
  153. * @warning
  154. * We can calculate tileId from (x,y) so having both (x,y) and tile ID is error
  155. * prone and not a good practice. Its easy to get them out of sync (code smell).
  156. * We implement it just because its in the requirements of the assignment.
  157. * @see Tile.tileId
  158. */
  159. /** @} */
  160. }