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.

176 lines
5.8 KiB

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