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.

165 lines
5.3 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 "-w":
  62. case "--walls":
  63. if (i+1 < args.length)
  64. Session.wallSize = Integer.parseInt(args[++i]);
  65. break;
  66. case "-s":
  67. case "--suplies":
  68. if (i+1 < args.length)
  69. Session.supplySize = Integer.parseInt(args[++i]);
  70. break;
  71. default:
  72. case "-h":
  73. case "--help":
  74. System.out.println("Labyrinth Game");
  75. System.out.println("");
  76. System.out.println("Usage:");
  77. System.out.println("labyrinth [-b|--board <Num>] [-w|--walls <Num>] [-s|--supplies <Num>]");
  78. System.out.println("or");
  79. System.out.println("labyrinth -h|--help");
  80. System.out.println("");
  81. System.out.println("\t-b | --board: Sets the size of board's edge.");
  82. System.out.println("\t-w | --walls: Sets the number of walls on the board.");
  83. System.out.println("\t-s | --supplies: Sets the number of supplies on the board.");
  84. System.out.println("\t-h | --help: Print this and exit");
  85. break;
  86. }
  87. }
  88. return true;
  89. }
  90. /**
  91. * Main game loop
  92. */
  93. public static void main(String[] args) {
  94. try {
  95. // Get command line options
  96. if (!Game.getArguments(args)) throw new Exception("");
  97. // Create a game, a board and 2 players.
  98. Game game = new Game();
  99. Board board = new Board(Session.boardSize, Session.supplySize, Session.wallSize);
  100. Player T = new Player(1, "Theseus", board, 0);
  101. Player M = new Player(2, "Minotaur", board, Position.toID(Session.boardSize/2, Session.boardSize/2));
  102. // Populate data to the board
  103. board.createBoard(T.playerTileId(), M.playerTileId());
  104. // Initial board printout
  105. System.out.println("Initial board: " + (game.round()));
  106. board.printBoard(
  107. board.getStringRepresentation(T.playerTileId(), M.playerTileId())
  108. );
  109. game.waitUser ();
  110. // Main game loop
  111. while (true) {
  112. int[] m;
  113. System.out.println();
  114. System.out.println("State after round: " + (game.round()+1));
  115. // Player moves
  116. m = T.move(T.playerTileId());
  117. System.out.println(T.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")");
  118. m = M.move(M.playerTileId());
  119. System.out.println(M.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")");
  120. board.printBoard(
  121. board.getStringRepresentation(T.playerTileId(), M.playerTileId())
  122. );
  123. // Loop termination cases
  124. if (T.getScore() == 4) {
  125. System.out.println(T.getName() + " Wins!!! Score =" + T.getScore());
  126. System.exit(0);
  127. }
  128. if (M.getScore() == 4 || M.playerTileId() == T.playerTileId()) {
  129. System.out.println(M.getName() + " Wins!!! Score =" + M.getScore());
  130. System.exit(0);
  131. }
  132. if (!(game.nextRound() < Session.maxRounds)) {
  133. System.out.println("New day has come... Tie!!!");
  134. System.exit(0);
  135. }
  136. game.waitUser ();
  137. }
  138. }
  139. catch (Exception e) {
  140. // We don't handle exceptions. Print error and exit with error status.
  141. System.out.println(e.getMessage());
  142. System.exit(1);
  143. }
  144. }
  145. }