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.

413 lines
13 KiB

  1. /**
  2. * @file Common.java
  3. *
  4. * @author
  5. * Christos Choutouridis
  6. * <cchoutou@ece.auth.gr>
  7. * AEM:8997
  8. */
  9. package host.labyrinth;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. /**
  13. * Class to hold constant values for entire application
  14. */
  15. class Const {
  16. static final int maxTileWalls = 2; /**< Number of maximum walls for each tile on the board */
  17. static final int noSupply =-1; /**< Number to indicate the absent of supply */
  18. static final int noTileId =-1; /**< Number to indicate wrong tileId */
  19. }
  20. /**
  21. * Application wide object to hold settings like values for the session.
  22. */
  23. class Session {
  24. static int boardSize = 15; /**< Default board's size (if no one set it via command line) */
  25. static int supplySize = 4; /**< Default board's supply size (if no one set it via command line) */
  26. static int maxRounds = 100; /**< Default number of rounds per game (if no one set it via command line) */
  27. static boolean loopGuard = false; /**< When true a wall creation guard is added to prevent closed rooms inside the board */
  28. static boolean interactive = false; /**< When true each round of the game requires user input */
  29. }
  30. /**
  31. * Helper C++-like enumerator class to hold direction
  32. */
  33. class Direction {
  34. static final int UP =1; /**< North direction */
  35. static final int RIGHT =3; /**< East direction */
  36. static final int DOWN =5; /**< South direction */
  37. static final int LEFT =7; /**< West direction */
  38. /**
  39. * Utility to get the opposite direction.
  40. * @param direction Input direction
  41. * @return The opposite direction
  42. */
  43. static int opposite (int direction) { return (direction+4)%DirRange.End; }
  44. }
  45. /**
  46. * Helper C++ like enumerator class for direction ranged loops.
  47. *
  48. * We can make use of this in loops like:
  49. * <pre>
  50. * for (int i=DirRange.Begin ; i<DirRange.End ; i += DirRange.Step) { }
  51. *
  52. * or
  53. *
  54. * Range directions = new Range(DirRange.Begin, DirRange.End, DirRange.Step);
  55. * </pre>
  56. */
  57. class DirRange {
  58. static final int Begin =1; /**< Iterator style begin of range direction (starting north) */
  59. static final int End =8; /**< Iterator style end of range direction (one place after the last) */
  60. static final int Step =2; /**< Step for iterator style direction */
  61. }
  62. /**
  63. * @brief
  64. * An Application wide board position implementation holding just the id coordinate.
  65. *
  66. * Position is a helper class to enable us cope with the redundant position data (id and coordinates).
  67. * This class provide both static conversion functionalities between id and coordinates
  68. * and data representation in the coordinates system.
  69. * For clarity we adopt a tileId convention.
  70. */
  71. class Position {
  72. /**
  73. * Basic constructor from row-column coordinates
  74. * @param row The row coordinate
  75. * @param col The column coordinate
  76. */
  77. Position(int row, int col) {
  78. this.id = toID(row, col);
  79. }
  80. /**
  81. * Basic constructor from Id
  82. * @param tileId The id of tile
  83. */
  84. Position(int tileId) {
  85. this.id = tileId;
  86. }
  87. /**
  88. * Constructor from row-column coordinates and a direction.
  89. *
  90. * This constructor creates a position relative to coordinates.
  91. *
  92. * @param row The row coordinate
  93. * @param col The column coordinate
  94. * @param direction The direction
  95. */
  96. Position(int row, int col, int direction) {
  97. switch (direction) {
  98. case Direction.UP: this.id = toID(row+1, col); break;
  99. case Direction.DOWN: this.id = toID(row-1, col); break;
  100. case Direction.LEFT: this.id = toID(row, col-1); break;
  101. case Direction.RIGHT:this.id = toID(row, col+1); break;
  102. }
  103. }
  104. /** @name non-static API */
  105. /** @{ */
  106. int getRow() { return toRow(id); } /**< Read access to virtual row coordinate */
  107. int getCol() { return toCol(id); } /**< Read access to virtual column coordinate */
  108. int getId() { return id; } /**< Read access to id coordinate */
  109. /** @} */
  110. /** @name Static convention utilities */
  111. /** @{ */
  112. /**
  113. * Takes row and column coordinates and return the calculated Id coordinate
  114. * @param row The row coordinate
  115. * @param col The column coordinate
  116. * @return The converted value
  117. */
  118. static int toID(int row, int col) {
  119. return row * Session.boardSize + col;
  120. }
  121. /**
  122. * Takes Id coordinate and return the corresponding row coordinate
  123. * @param id The id coordinate
  124. * @return The row coordinate
  125. */
  126. static int toRow(int id){
  127. return id / Session.boardSize;
  128. }
  129. /**
  130. * Takes Id coordinate and return the corresponding column coordinate
  131. * @param id The id coordinate
  132. * @return The column coordinate
  133. */
  134. static int toCol(int id) {
  135. return id % Session.boardSize;
  136. }
  137. /** @} */
  138. /** @name private data types */
  139. /** @{ */
  140. private int id; /**< The id coordinate of the constructed Position object */
  141. /** @} */
  142. }
  143. /**
  144. * Class to create ranges of numbers
  145. */
  146. class Range {
  147. /**
  148. * Create the range [begin, end)
  149. * @param begin The first item on the range
  150. * @param end The item after the last on the range
  151. */
  152. Range (int begin, int end) {
  153. numbers = new ArrayList<Integer>();
  154. init (begin, end, 1);
  155. }
  156. /**
  157. * Create the range [begin, end) using step as interval between items.
  158. * @param begin The first item on the range
  159. * @param end The item after the last on the range
  160. * @param step The interval between items
  161. */
  162. Range(int begin, int end, int step) {
  163. numbers = new ArrayList<Integer>();
  164. init (begin, end, step);
  165. }
  166. /**
  167. * Common utility to create the range for all constructors
  168. */
  169. private void init (int begin, int end, int step) {
  170. numbers.clear();
  171. for (int i=begin ; i<end ; i+=step)
  172. numbers.add(i);
  173. }
  174. /**
  175. * Extract and return the first item from the range.
  176. * @return The first item of the range or Const.noTileId if there is none.
  177. */
  178. int get () {
  179. if (!numbers.isEmpty())
  180. return numbers.remove(0);
  181. return Const.noTileId;
  182. }
  183. /** @name protected data types */
  184. /** @{ */
  185. protected ArrayList<Integer> numbers; /**< handle to range */
  186. /** @} */
  187. }
  188. /**
  189. * Class to create shuffled ranges of numbers
  190. */
  191. class ShuffledRange extends Range {
  192. /**
  193. * Create a shuffled version of range [begin, end)
  194. * @param begin The first item on the range
  195. * @param end The item after the last on the range
  196. */
  197. ShuffledRange(int begin, int end) {
  198. super(begin, end); // Delegate
  199. Collections.shuffle(numbers);
  200. }
  201. /**
  202. * Create a shuffled version of the range [begin, end)
  203. * using step as interval between items.
  204. *
  205. * @param begin The first item on the range
  206. * @param end The item after the last on the range
  207. * @param step The interval between items
  208. */
  209. ShuffledRange(int begin, int end, int step) {
  210. super(begin, end, step); // Delegate
  211. Collections.shuffle(numbers);
  212. }
  213. }
  214. /**
  215. * @brief
  216. * A utility class used for room prevent algorithm.
  217. *
  218. * This class is the wall representation we use in the room preventing algorithm.
  219. * In this algorithm we represent the crosses between tiles as nodes (V) of a graph and the
  220. * walls as edges. So for example:
  221. * <pre>
  222. * 12--13--14---15
  223. * | |
  224. * 8 9--10 11
  225. * | | |
  226. * 4 5 6 7
  227. * | | |
  228. * 0 1---2---3
  229. * </pre>
  230. * In this example we have a 4x4=16 vertices board(nodes) and 14 edges(walls).
  231. * To represent the vertices on the board we use the same trick as the tileId
  232. *
  233. * V = Row*(N+1) + Column, where N is the board's tile size.
  234. *
  235. * The edges are represented as vertices pairs. For example (0, 4) or (13, 14).
  236. *
  237. * @note
  238. * Beside the fact that we prefer this kind of representation of the walls in
  239. * the application we use the one that is suggested from the assignment. This is
  240. * used only in room preventing algorithm.
  241. * @note
  242. * Using this kind of representation we don't have any more the "problem"
  243. * of setting the wall in both neighbor tiles.
  244. */
  245. class Edge {
  246. /**
  247. * This constructor acts as the interface between the application's wall
  248. * representation and the one based on graph.
  249. * @param tileId The tile id of the wall.
  250. * @param direction The direction of the tile where the wall should be.
  251. */
  252. Edge(int tileId, int direction) {
  253. int N = Session.boardSize +1;
  254. switch (direction) {
  255. case Direction.UP:
  256. v1= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
  257. v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) + 1;
  258. break;
  259. case Direction.DOWN:
  260. v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
  261. v2= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
  262. break;
  263. case Direction.LEFT:
  264. v1= (Position.toRow(tileId))*N + Position.toCol(tileId);
  265. v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId);
  266. break;
  267. case Direction.RIGHT:
  268. v1= (Position.toRow(tileId))*N + Position.toCol(tileId) + 1;
  269. v2= (Position.toRow(tileId) + 1)*N + Position.toCol(tileId) +1;
  270. break;
  271. }
  272. }
  273. /** A deep copy contructor */
  274. Edge(Edge e) {
  275. v1 = e.getV1();
  276. v2 = e.getV2();
  277. }
  278. /** Access of the first node of the edge */
  279. int getV1() { return v1; }
  280. /** Access of the second node of the edge */
  281. int getV2() { return v2; }
  282. private int v1; /**< First vertex of the edge */
  283. private int v2; /**< Second vertex of the edge */
  284. }
  285. /**
  286. * @brief
  287. * Provides a graph functionality for the room preventing algorithm.
  288. * We use a graph to represent the wall structure of the walls. This way
  289. * its easy to find any closed loops. Using graph we transform the problem
  290. * of the closed room into the problem of finding a non simple graph.
  291. *
  292. * If the board has non connected wall structure then we would need more than
  293. * one graph to represent it.
  294. *
  295. * An example graph we can create from the board bellow, starting from V=1 is:
  296. * <pre>
  297. * 6---7 8 (1)
  298. * | | / \
  299. * 3 4 5 (4) (2)
  300. * | | | \
  301. * 0 1---2 (5)--(8)
  302. * </pre>
  303. */
  304. class Graph {
  305. /**
  306. * Constructs a node of the graph using the value of a vertex(node).
  307. * @param v The verteg to attach.
  308. */
  309. Graph (int v) {
  310. V = v;
  311. E = new ArrayList<Graph>();
  312. }
  313. /**
  314. * Constructor that transform an edge into graph.
  315. * @param e The edge to transform.
  316. */
  317. Graph (Edge e) {
  318. V = e.getV1();
  319. E = new ArrayList<Graph>();
  320. E.add(new Graph(e.getV2()));
  321. }
  322. /** Access to the current vertex */
  323. int getV() { return V; }
  324. /** Access to the links of the current vertex */
  325. ArrayList<Graph> getE() { return E; }
  326. /**
  327. * Attach an edge into a graph IFF the graph already has a vertex
  328. * with the same value as one of the vertices of the edge.
  329. * @param e The edge to attach.
  330. * @return The status of the operation.
  331. * @arg True on success
  332. * @arg False on failure
  333. */
  334. boolean attach (Edge e) {
  335. return tryAttach(e, 0) > 0;
  336. }
  337. /**
  338. * Counts the number of vertices on the graph with the value of `v`
  339. * @param v The vertex to count
  340. * @return The number of vertices with value `v`
  341. */
  342. int count (int v) {
  343. return tryCount (v, 0);
  344. }
  345. /**
  346. * Recursive algorithm that tries to attach an edge into a graph
  347. * IFF the graph already has a vertex with the same value as one
  348. * of the vertices of the edge.
  349. *
  350. * @param e The edge to attach.
  351. * @param count An initial count value to feed the algorithm.
  352. * @return The status of the operation.
  353. * @arg True on success
  354. * @arg False on failure
  355. */
  356. private int tryAttach (Edge e, int count) {
  357. for (Graph n: E)
  358. count = n.tryAttach (e, count);
  359. if (V == e.getV1()) {
  360. E.add(new Graph(e.getV2()));
  361. ++count;
  362. }
  363. if (V == e.getV2()) {
  364. E.add(new Graph(e.getV1()));
  365. ++count;
  366. }
  367. return count;
  368. }
  369. /**
  370. * Recursive algorithm that tries to count the number of vertices
  371. * on the graph with the same value as `v`.
  372. *
  373. * @param v The vertex to count
  374. * @param count An initial count value to feed to the algorithm.
  375. * @return The number of vertices with value `v`
  376. */
  377. private int tryCount (int v, int count) {
  378. for (Graph n: E)
  379. count = n.tryCount (v, count);
  380. if (V == v)
  381. return ++count;
  382. return count;
  383. }
  384. private int V; /**< The value of the current vertex/node */
  385. private ArrayList<Graph> E; /**< A list of all the child nodes */
  386. }