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.

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