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.

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