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.

411 lines
16 KiB

  1. /**
  2. * @file Board.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.function.IntFunction;
  9. /**
  10. * @brief
  11. * This class is the representation of the games's board
  12. *
  13. * The board is the square arrangement of the tiles. This class is also
  14. * the owner of the tile and supply objects.
  15. */
  16. class Board {
  17. /** @name Constructors */
  18. /** @{ */
  19. /**
  20. * The empty constructor for default initialization
  21. */
  22. Board() {
  23. this.N = this.S = this.W = 0;
  24. this.tiles = null;
  25. this.supplies =null;
  26. }
  27. /**
  28. * The main constructor for the application
  29. * @param N The size of each edge of the board
  30. * @param S The number of supplies on the board
  31. * @param W The number of walls on the board
  32. */
  33. Board(int N, int S, int W) {
  34. assert (W>= 4*N-1 && W<=(3*N*N+1)/2 )
  35. : "Boards walls has to be in the range [4N-1, (3N^2+1)/2]";
  36. this.N = Session.boardSize = N;
  37. this.S = S;
  38. this.W = W;
  39. tiles = new Tile[N*N];
  40. supplies = new Supply[S];
  41. }
  42. /**
  43. * Deep copy constructor
  44. * @param b The board to copy
  45. *
  46. * @note
  47. * The lack of value semantics in java is (in author's opinion) one of the greatest
  48. * weakness of the language and one of the reasons why it will never be a language
  49. * to care about. To quote Alexander Stepanof's words in "elements of programming" section 1.5:
  50. * "Assignment is a procedure that takes two objects of the same type and makes the first
  51. * object equal to the second without modifying the second".
  52. * In this class we try to cope with this situation knowing that we can not do anything about
  53. * assignment operator. We just add value semantics to the copy constructor and go on with our lifes...
  54. */
  55. Board(Board b) {
  56. // Copy primitives
  57. this.N = b.N;
  58. this.S = b.S;
  59. this.W = b.W;
  60. // Clone arrays
  61. this.tiles = b.tiles.clone();
  62. this.supplies = b.supplies.clone();
  63. }
  64. /** @} */
  65. /** @name Board's main application interface */
  66. /** @{ */
  67. /**
  68. * Creates the board with all the requested walls and supplies.
  69. *
  70. * @param theseusTile
  71. * @param minotaurTile
  72. */
  73. void createBoard(int theseusTile, int minotaurTile) throws Exception {
  74. createTiles();
  75. createSupplies(theseusTile, minotaurTile);
  76. }
  77. /**
  78. * Returns a 2-D array with the string representation of the board.
  79. *
  80. * The rows of the array represent the Y-coordinate and the columns the X-coordinate.
  81. * The only difference is that between each row there is an extra row with the possible
  82. * walls. This way the number of rows of the returning array are 2N+1 and the number of
  83. * columns N+1.\n
  84. * So each tile of the board is represented by 3 strings. One for the north wall, one for
  85. * the body and one for the south wall.
  86. *
  87. * @param theseusTile The current Theseus tile
  88. * @param minotaurTile The current Minotaur tile
  89. * @return The string representation of the board
  90. */
  91. String[][] getStringRepresentation(int theseusTile, int minotaurTile) {
  92. String[][] frame = new String[2*N+1][N];
  93. for (int row=0 ; row<N ; ++row) {
  94. int col;
  95. for (col =0 ; col<N-1 ; ++col)
  96. renderTile(frame, row, col, theseusTile, minotaurTile);
  97. renderSentinelTile(frame, row, col, theseusTile, minotaurTile);
  98. }
  99. return frame;
  100. }
  101. /**
  102. * Print board utility.
  103. * @param sBoard Reference to string representation of the board to print.
  104. *
  105. * @note
  106. * As the lower row addresses of the string representation of the board contain
  107. * the south rows, in order to view the board correctly we have to print the rows
  108. * in the opposite order.
  109. */
  110. void printBoard (String[][] sBoard) {
  111. for (int i=sBoard.length-1 ; i>=0 ; --i) {
  112. for (String it : sBoard[i])
  113. System.out.print(it);
  114. System.out.println();
  115. }
  116. }
  117. /** @} */
  118. /**
  119. * @name Accessor/Mutator interface
  120. * @note
  121. * Please consider not to use mutator interface. Its the abstraction killer :(
  122. */
  123. /** @{ */
  124. int getN() { return N; }
  125. int getS() { return S; }
  126. int getW() { return W; }
  127. /**
  128. * @note Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure".
  129. * <a href="https://github.com/sean-parent/sean-parent.github.io/blob/master/better-code/03-data-structures.md"> see also here</a>
  130. * @return Reference to inner tiles array.
  131. */
  132. Tile[] getTiles() { return tiles; }
  133. /**
  134. * @note Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure".
  135. * <a href="https://github.com/sean-parent/sean-parent.github.io/blob/master/better-code/03-data-structures.md"> see also here</a>
  136. * @return Reference to inner supplies array.
  137. */
  138. Supply[] getSupplies() { return supplies; }
  139. void setN(int N) { this.N = N; }
  140. void setS(int S) { this.S = S; }
  141. void setW(int W) { this.W = W; }
  142. /**
  143. * @param tiles Reference to tiles that we want to act as replacement for the inner tiles array.
  144. * @note Use with care.
  145. * Any call to this function will probably add memory for the garbage collector.
  146. */
  147. void setTiles(Tile[] tiles) { this.tiles = tiles; }
  148. /**
  149. * @param supplies Reference to supplies that we want to act as replacement for the inner supplies array.
  150. * @note Use with care.
  151. * Any call to this function will probably add memory for the garbage collector.
  152. */
  153. void setSupplies(Supply[] supplies) { this.supplies= supplies; }
  154. /** @} */
  155. /**
  156. * @name private functionality of the object
  157. */
  158. /** @{ */
  159. /**
  160. * This function creates randomly all the tiles of the board
  161. */
  162. private void createTiles() throws Exception {
  163. int wallPool = W;
  164. wallPool -= createBasicTileWalls (); // First create tiles with outer walls
  165. if (createInnerWalls(wallPool) > 0) // Create inner walls with the rest of the requested walls
  166. throw new Exception("Can not create the requested number of walls");
  167. }
  168. /**
  169. * This function create randomly the board's supplies.
  170. *
  171. * The supplies has to be in separate tiles and in tiles with no player
  172. *
  173. * @param theseusTile The tile of the Theseus
  174. * @param minotaurTile The tile of the Minotaur
  175. */
  176. private void createSupplies(int theseusTile, int minotaurTile) {
  177. ShuffledRange rand = new ShuffledRange(0, N*N); // Make a shuffled range of all tiles
  178. for (int tileId, i=0 ; i<supplies.length ; ++i) {
  179. // Pick a tile as long as there is no player in it
  180. do
  181. tileId = rand.get();
  182. while (tileId == theseusTile || tileId == minotaurTile);
  183. supplies[i] = new Supply(i, tileId);
  184. }
  185. }
  186. /** @name Sentinel predicates */
  187. /** @{ */
  188. private boolean isLeftSentinel (int tileId) { return (Position.toCol(tileId) == 0); }
  189. private boolean isRightSentinel (int tileId) { return (Position.toCol(tileId) == N-1); }
  190. private boolean isUpSentinel (int tileId) { return (Position.toRow(tileId) == N-1); }
  191. private boolean isDownSentinel (int tileId) { return (Position.toRow(tileId) == 0); }
  192. /** @} */
  193. /**
  194. * Predicate to check if a tile direction is `Wallable`.
  195. *
  196. * A `wallable` direction is a tile direction where:
  197. * <ul>
  198. * <li>The wall is not the DOWN wall from tile (0, 0).
  199. * <li>There is not already a wall in the desired direction. (Implies no sentinel tile).
  200. * <li>The neighbor in this direction has at most `Const.maxTileWalls -1` walls.
  201. * </ul>
  202. *
  203. * @note
  204. * A wallable direction automatically implies that the direction in not an outer wall.
  205. *
  206. * @param tileId The tile to check.
  207. * @param direction The direction to check
  208. * @return True if the direction is wallable.
  209. */
  210. private boolean isWallableDir (int tileId, int direction) {
  211. // Check list
  212. if (tileId == 0 && direction == Direction.DOWN)
  213. return false;
  214. if (tiles[tileId].hasWall(direction))
  215. return false;
  216. switch (direction) {
  217. case Direction.UP: return (tiles[upTileId.apply(tileId)].hasWalls() < Const.maxTileWalls);
  218. case Direction.DOWN: return (tiles[downTileId.apply(tileId)].hasWalls() < Const.maxTileWalls);
  219. case Direction.LEFT: return (tiles[leftTileId.apply(tileId)].hasWalls() < Const.maxTileWalls);
  220. case Direction.RIGHT:return (tiles[rightTileId.apply(tileId)].hasWalls() < Const.maxTileWalls);
  221. }
  222. return false;
  223. }
  224. /**
  225. * Predicate to check if a tile is `Wallable`.
  226. *
  227. * A `wallable` tile is a tile where:
  228. * <ul>
  229. * <li>The tile has at most `Const.maxTileWalls -1` walls.
  230. * <li>There is at least one wallable direction on the tile.
  231. * </ul>
  232. * @param tileId The tile to check
  233. * @return True if the tile is wallable.
  234. */
  235. private boolean isWallable (int tileId) {
  236. // Check list
  237. if (tileId == Const.noTileId)
  238. return false;
  239. if (tiles[tileId].hasWalls() >= Const.maxTileWalls)
  240. return false;
  241. Range dirs = new Range(Direction.Begin, Direction.End, Direction.Step);
  242. for (int dir ; (dir = dirs.get()) != Const.noTileId ; )
  243. if (isWallableDir(tileId, dir))
  244. return true;
  245. return false;
  246. }
  247. /**
  248. * This utility function create/allocate the tiles of the board and create
  249. * the outer walls at the same time.
  250. *
  251. * @return The number of walls created from the utility.
  252. */
  253. private int createBasicTileWalls () {
  254. int tileCount =0;
  255. for (int i =0 ; i< tiles.length ; ++i) {
  256. boolean up = isUpSentinel(i);
  257. boolean down = isDownSentinel(i) && (i != 0);
  258. boolean left = isLeftSentinel(i);
  259. boolean right = isRightSentinel(i);
  260. tileCount += ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
  261. tiles[i] = new Tile (i, up, down, left, right);
  262. }
  263. return tileCount;
  264. }
  265. /**
  266. * Create randomly a wall in the wallable selected tile.
  267. * @param tileId The wallable tile to create the wall
  268. */
  269. private void createInnerWall(int tileId) {
  270. // Randomly pick a wallable direction in that tile.
  271. ShuffledRange randDirections = new ShuffledRange(Direction.Begin, Direction.End, Direction.Step);
  272. int dir;
  273. do
  274. dir = randDirections.get();
  275. while (!isWallableDir(tileId, dir));
  276. Position neighbor = new Position(Position.toRow(tileId), Position.toCol(tileId), dir);
  277. tiles[tileId].setWall(dir);
  278. tiles[neighbor.getId()].setWall(Direction.opposite(dir));
  279. }
  280. /**
  281. * This utility creates the inner walls of the board.
  282. *
  283. * @param walls The number of walls to create
  284. * @return The number of walls failed to create.
  285. */
  286. private int createInnerWalls (int walls) {
  287. ShuffledRange randTiles = new ShuffledRange(0, N*N);
  288. for (int tileId, i =0, shuffleMark =0 ; i<walls ; ++i) {
  289. // randomly pick a wallable tile.
  290. do {
  291. if ((tileId = randTiles.get())== Const.noTileId) {
  292. if (i == shuffleMark) // Wallable tiles exhausted.
  293. return walls - i;
  294. else { // Re-shuffle and continue.
  295. randTiles = new ShuffledRange(0, N*N);
  296. shuffleMark =i;
  297. }
  298. }
  299. } while (!isWallable(tileId));
  300. createInnerWall(tileId);
  301. }
  302. return 0;
  303. }
  304. /**
  305. * Utility to get the body (center line) of the string representation of the tile.
  306. *
  307. * @param row What board's row to get.
  308. * @param col What board's column to get.
  309. * @param theseusTile The current tile of the Theseus.
  310. * @param minotaurTile The current tile of the Minotaur.
  311. * @return The body string
  312. */
  313. private String getTileBody (int row, int col, int theseusTile, int minotaurTile) {
  314. int tileId = Position.toID(row, col);
  315. boolean T = (tileId == theseusTile) ? true : false;
  316. boolean M = (tileId == minotaurTile) ? true : false;
  317. int S = tiles[tileId].hasSupply(supplies);
  318. if (T && !M) return " T ";
  319. else if (M && !T) return " M ";
  320. else if (T && M) return "T+M";
  321. else if (S != Const.noSupply)
  322. return String.format("s%02d", S+1);
  323. else return " ";
  324. }
  325. /**
  326. * Utility to render the 3 strings of the tile in the representation frame.
  327. *
  328. * @param frame Reference to the frame to print into.
  329. * @param row The board's row to print.
  330. * @param col The board's column to print.
  331. * @param theseusTile The current tile of the Theseus.
  332. * @param minotaurTile The current tile of the Minotaur.
  333. */
  334. private void renderTile(String[][] frame, int row, int col, int theseusTile, int minotaurTile) {
  335. IntFunction<Integer> toframe = (r)->{ return 2*r+1; };
  336. int tileId = Position.toID(row, col);
  337. frame[toframe.apply(row)+1][col] = tiles[tileId].hasWall(Direction.UP) ? "+---" : "+ ";
  338. frame[toframe.apply(row) ][col] = (tiles[tileId].hasWall(Direction.LEFT)? "|" : " ")
  339. + getTileBody(row, col, theseusTile, minotaurTile);
  340. frame[toframe.apply(row)-1][col] = tiles[tileId].hasWall(Direction.DOWN) ? "+---" : "+ ";
  341. }
  342. /**
  343. * Utility to render the 3 strings of the tile in the representation frame in
  344. * the case the tile lies in the east wall. We call these tiles `sentinel tiles`
  345. *
  346. * @param frame Reference to the frame to print into.
  347. * @param row The board's row to print.
  348. * @param col The board's column to print.
  349. * @param theseusTile The current tile of the Theseus.
  350. * @param minotaurTile The current tile of the Minotaur.
  351. */
  352. private void renderSentinelTile(String[][] frame, int row, int col, int theseusTile, int minotaurTile ) {
  353. IntFunction<Integer> toframe = (r)->{ return 2*r+1; };
  354. int tileId = Position.toID(row, col);
  355. frame[toframe.apply(row)+1][col] = tiles[tileId].hasWall(Direction.UP) ? "+---+" : "+ +";
  356. frame[toframe.apply(row) ][col] = (tiles[tileId].hasWall(Direction.LEFT)? "|" : " ")
  357. + getTileBody(row, col, theseusTile, minotaurTile)
  358. + (tiles[tileId].hasWall(Direction.RIGHT)? "|" : " ");
  359. frame[toframe.apply(row)-1][col] = tiles[tileId].hasWall(Direction.DOWN) ? "+---+" : "+ +";
  360. }
  361. /** @} */
  362. /** @name Neighbor access lambdas */
  363. /** @{ */
  364. private IntFunction<Integer> leftTileId = (id) -> { return Position.toID(Position.toRow(id), Position.toCol(id)-1); };
  365. private IntFunction<Integer> rightTileId = (id) -> { return Position.toID(Position.toRow(id), Position.toCol(id)+1); };
  366. private IntFunction<Integer> upTileId = (id) -> { return Position.toID(Position.toRow(id)+1, Position.toCol(id) ); };
  367. private IntFunction<Integer> downTileId = (id) -> { return Position.toID(Position.toRow(id)-1, Position.toCol(id) ); };
  368. /** @} */
  369. /** @name Class data */
  370. /** @{ */
  371. private int N; /**< The size of each edge of the board */
  372. private int S; /**< The number of the supplies on the board */
  373. private int W; /**< The number of walls on the board */
  374. private Tile[] tiles; /**< Array to hold all the tiles for the board */
  375. private Supply[] supplies; /**< Array to hold all the supplies on the board */
  376. /** @} */
  377. }