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.

212 lines
6.6 KiB

  1. /**
  2. * @file Game.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. /**
  13. * @mainpage A labyrinth board game
  14. *
  15. * This is a console game, played by 2 players. The Theseus and Minotaur.
  16. * The Minotaur goal is to capture Theseus. The Theseus's goal is to collect
  17. * all the supplies of the board before Minotaur catches him and before the
  18. * game ends.
  19. *
  20. * In this 2nd assignment we deal with the creation of a new heuristic player
  21. * who can cheat and manipulate the dice. Documented classes:
  22. * - Tile
  23. * - Supply
  24. * - Board
  25. * - Player
  26. * - HeuristicPlayer
  27. * - Game
  28. *
  29. * Which are the requested classes. We also provide some extra functionalities in:
  30. * - Const
  31. * - Session
  32. * - Direction
  33. * - DirRange
  34. * - Edge
  35. * - Graph
  36. * - Position
  37. * - Range
  38. * - ShuffledRange
  39. */
  40. package host.labyrinth;
  41. import java.util.Scanner;
  42. /**
  43. * Main application class. This class is the only public interface of
  44. * the entire game.
  45. */
  46. public class Game {
  47. /**< An empty constructor */
  48. Game() {
  49. scan = new Scanner(System.in);
  50. }
  51. /** @name Player main application interface */
  52. /** @{ */
  53. /** Utility to get current round of the game */
  54. int round () { return round; }
  55. /** Utility to increase and get the increased round of the game */
  56. int nextRound() { return ++round; }
  57. /**
  58. * Utility to hold the execution of the program waiting for user input.
  59. * This is true only if the user passed the interactive flag.
  60. */
  61. void waitUser () {
  62. if(Session.interactive) {
  63. System.out.println("Press enter to continue...");
  64. scan.nextLine();
  65. }
  66. }
  67. /** @{ */
  68. /**
  69. * @name Accessor/Mutator interface
  70. * @note
  71. * Please consider not to use mutator interface. Its the abstraction killer :(
  72. * We have added a bit of logic however, in order to make it a bit more safe.
  73. */
  74. /** @{ */
  75. int getRound() { return round; }
  76. void setRound (int round) { this.round = round; }
  77. /** @} */
  78. /** @name Game's data */
  79. /** @{ */
  80. private int round; /**< Holds the round of the game */
  81. private Scanner scan; /**< Input handle used in interactive mode */
  82. /** @} */
  83. /**
  84. * Command line argument handler
  85. */
  86. static boolean getArguments (String[] args) {
  87. for (int i =0 ; i<args.length ; ++i) {
  88. switch (args[i]) {
  89. case "-b":
  90. case "--board":
  91. if (i+1 < args.length)
  92. Session.boardSize = Integer.parseInt(args[++i]);
  93. break;
  94. case "-s":
  95. case "--supplies":
  96. if (i+1 < args.length)
  97. Session.supplySize = Integer.parseInt(args[++i]);
  98. break;
  99. case "-r":
  100. case "--rounds":
  101. if (i+1 < args.length)
  102. Session.maxRounds = Integer.parseInt(args[++i]);
  103. break;
  104. case "--norooms":
  105. Session.loopGuard = true;
  106. break;
  107. case "-i":
  108. case "--interactive":
  109. Session.interactive = true;
  110. break;
  111. default:
  112. case "-h":
  113. case "--help":
  114. System.out.println("Labyrinth Game");
  115. System.out.println("");
  116. System.out.println("Usage:");
  117. System.out.println("labyrinth [-b|--board <Num>] [-s|--supplies <Num>] [-r|--rounds <Num>] [--norooms] [-i|--interactive]");
  118. System.out.println("or");
  119. System.out.println("labyrinth -h|--help");
  120. System.out.println("\nOptions\n");
  121. System.out.println("-b | --board:\n Sets the size of board's edge.\n");
  122. System.out.println("-s | --supplies:\n Sets the number of supplies on the board.\n");
  123. System.out.println("-r | --rounds:\n Sets the maximum number of rounds of the game.\n");
  124. System.out.println("--norooms:\n Prevents the creation of closed rooms inside the board.\n");
  125. System.out.println("-i | --interactive:\n Each round requires user input in order to continue.\n");
  126. System.out.println("-h | --help:\n Print this and exits.");
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. /**
  133. * Main game loop
  134. */
  135. public static void main(String[] args) {
  136. try {
  137. // Get command line options
  138. if (!Game.getArguments(args)) throw new Exception("");
  139. // Create a game, a board and 2 players.
  140. Game game = new Game();
  141. Board board = new Board(Session.boardSize, Session.supplySize);
  142. Player T = new HeuristicPlayer("Theseus", true, board, 0);
  143. Player M = new Player("Minotaur", false, board, Position.toID(Session.boardSize/2, Session.boardSize/2));
  144. Player players [] = {T, M};
  145. // Populate data to the board
  146. board.createBoard(T.playerTileId(), M.playerTileId());
  147. // Initial board printout
  148. System.out.println("Initial board: " + (game.round()));
  149. board.printBoard(
  150. board.getStringRepresentation(T.playerTileId(), M.playerTileId())
  151. );
  152. game.waitUser ();
  153. // Main game loop
  154. while (true) {
  155. System.out.println();
  156. System.out.println("Round: " + (game.round()+1));
  157. // Players moves
  158. for (Player p : players) {
  159. p.move(p.playerTileId());
  160. p.statistics();
  161. }
  162. board.printBoard(
  163. board.getStringRepresentation(T.playerTileId(), M.playerTileId())
  164. );
  165. // Loop termination cases
  166. if (T.getScore() == 4) {
  167. System.out.println("**** " + T.getName() + " Wins!!! Score =" + T.getScore() + " ****");
  168. break;
  169. }
  170. if (M.getScore() == 4 || M.playerTileId() == T.playerTileId()) {
  171. System.out.println("**** " + M.getName() + " Wins!!! Score =" + M.getScore());
  172. break;
  173. }
  174. if (!(game.nextRound() < Session.maxRounds)) {
  175. System.out.println("**** New day has come... Tie! ****");
  176. break;
  177. }
  178. game.waitUser ();
  179. }
  180. for (Player p : players)
  181. p.final_statistics();
  182. System.exit(0);
  183. }
  184. catch (Exception e) {
  185. // We don't handle exceptions. Print error and exit with error status.
  186. System.out.println(e.getMessage());
  187. System.exit(1);
  188. }
  189. }
  190. }