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.

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