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.

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