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.

447 lines
14 KiB

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