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.

210 lines
6.2 KiB

  1. /**
  2. * @file Common.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.labyrinth;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. /**
  11. * Class to hold constant values for entire application
  12. */
  13. class Const {
  14. static final int maxTileWalls = 2; /**< Number of maximum walls for each tile on the board */
  15. static final int noSupply =-1; /**< Number to indicate the absent of supply */
  16. static final int noTileId =-1; /**< Number to indicate wrong tileId */
  17. }
  18. /**
  19. * Application wide object to hold settings like values for the session.
  20. */
  21. class Session {
  22. static int boardSize = 15; /**< Default board's size (if no one set it via command line) */
  23. }
  24. /**
  25. * Helper C++-like enumerator class to hold direction
  26. */
  27. class Direction {
  28. static final int UP =1; /**< North direction */
  29. static final int RIGHT =3; /**< East direction */
  30. static final int DOWN =5; /**< South direction */
  31. static final int LEFT =7; /**< West direction */
  32. static final int Begin =1; /**< Iterator style begin of range direction (starting north) */
  33. static final int End =8; /**< Iterator style end of range direction (one place after the last) */
  34. static final int Step =2; /**< Step for iterator style direction */
  35. /**
  36. * Utility to get the opposite
  37. * @param direction Input direction
  38. * @return The opposite direction
  39. */
  40. static int opposite (int direction) { return (direction+4)%End; }
  41. }
  42. /**
  43. * @brief
  44. * An Application wide board position implementation holding just the id coordinate.
  45. *
  46. * Position is a helper class to enable us cope with the redundant position data (id and coordinates).
  47. * This class provide both static conversion functionalities between id and coordinates
  48. * and data representation in the coordinates system.
  49. * For clarity we adopt a row-column naming convention.
  50. */
  51. class Position {
  52. /**
  53. * Basic constructor from row-column coordinates
  54. * @param row The row coordinate
  55. * @param col The column coordinate
  56. */
  57. Position(int row, int col) {
  58. this.id = toID(row, col);
  59. }
  60. /**
  61. * Basic constructor from Id
  62. * @param tileId The id of tile
  63. */
  64. Position(int tileId) {
  65. this.id = tileId;
  66. }
  67. /**
  68. * Constructor from row-column coordinates and a direction.
  69. *
  70. * This constructor creates a position relative to coordinates.
  71. *
  72. * @param row The row coordinate
  73. * @param col The column coordinate
  74. * @param direction The direction
  75. */
  76. Position(int row, int col, int direction) {
  77. switch (direction) {
  78. case Direction.UP: this.id = toID(row+1, col); break;
  79. case Direction.DOWN: this.id = toID(row-1, col); break;
  80. case Direction.LEFT: this.id = toID(row, col-1); break;
  81. case Direction.RIGHT:this.id = toID(row, col+1); break;
  82. }
  83. }
  84. /** @name non-static API */
  85. /** @{ */
  86. int getRow() { return toRow(id); } /**< Read access to virtual row coordinate */
  87. int getCol() { return toCol(id); } /**< Read access to virtual column coordinate */
  88. int getId() { return id; } /**< Read access to id coordinate */
  89. /** @} */
  90. /** @name Static convention utilities */
  91. /** @{ */
  92. /**
  93. * Takes row and column coordinates and return the calculated Id coordinate
  94. * @param row The row coordinate
  95. * @param col The column coordinate
  96. * @return The converted value
  97. */
  98. static int toID(int row, int col) {
  99. return row * Session.boardSize + col;
  100. }
  101. /**
  102. * Takes Id coordinate and return the corresponding row coordinate
  103. * @param id The id coordinate
  104. * @return The row coordinate
  105. */
  106. static int toRow(int id){
  107. return id / Session.boardSize;
  108. }
  109. /**
  110. * Takes Id coordinate and return the corresponding column coordinate
  111. * @param id The id coordinate
  112. * @return The column coordinate
  113. */
  114. static int toCol(int id) {
  115. return id % Session.boardSize;
  116. }
  117. /** @} */
  118. /** @name private data types */
  119. /** @{ */
  120. private int id; /**< The id coordinate of the constructed Position object */
  121. /** @} */
  122. }
  123. /**
  124. * Class to create ranges of numbers
  125. */
  126. class Range {
  127. /**
  128. * Create the range [begin, end)
  129. * @param begin The first item on the range
  130. * @param end The item after the last on the range
  131. */
  132. Range (int begin, int end) {
  133. numbers = new ArrayList<Integer>();
  134. init (begin, end, 1);
  135. }
  136. /**
  137. * Create the range [begin, end) using step as interval between items.
  138. * @param begin The first item on the range
  139. * @param end The item after the last on the range
  140. * @param step The interval between items
  141. */
  142. Range(int begin, int end, int step) {
  143. numbers = new ArrayList<Integer>();
  144. init (begin, end, step);
  145. }
  146. /**
  147. * Common utility to create the range for all constructors
  148. */
  149. private void init (int begin, int end, int step) {
  150. numbers.clear();
  151. for (int i=begin ; i<end ; i+=step)
  152. numbers.add(i);
  153. }
  154. /**
  155. * Extract and return the first item from the range.
  156. * @return The first item of the range or Const.noTileId if there is none.
  157. */
  158. int get () {
  159. try {
  160. return numbers.remove(0);
  161. }
  162. catch (IndexOutOfBoundsException e) {
  163. return Const.noTileId;
  164. }
  165. }
  166. protected ArrayList<Integer> numbers; /**< handle to range */
  167. }
  168. /**
  169. * Class to create shuffled ranges of numbers
  170. */
  171. class ShuffledRange extends Range {
  172. /**
  173. * Create a shuffled version of range [begin, end)
  174. * @param begin The first item on the range
  175. * @param end The item after the last on the range
  176. */
  177. ShuffledRange(int begin, int end) {
  178. super(begin, end); // Delegate
  179. Collections.shuffle(numbers);
  180. }
  181. /**
  182. * Create a shuffled version of the range [begin, end)
  183. * using step as interval between items.
  184. *
  185. * @param begin The first item on the range
  186. * @param end The item after the last on the range
  187. * @param step The interval between items
  188. */
  189. ShuffledRange(int begin, int end, int step) {
  190. super(begin, end, step); // Delegate
  191. Collections.shuffle(numbers);
  192. }
  193. }