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.

205 lines
6.5 KiB

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