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.

133 lines
4.2 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. /**
  9. * Main application class. This class is the only public interface of
  10. * the entire game.
  11. */
  12. public class Game {
  13. Game() {} /**< An empty constructor */
  14. int round () { return round; }
  15. int nextRound() { return ++round; }
  16. /**
  17. * @name Accessor/Mutator interface
  18. * @note
  19. * Please consider not to use mutator interface. Its the abstraction killer :(
  20. * We have added a bit of logic however, in order to make it a bit more safe.
  21. */
  22. /** @{ */
  23. int getRound() { return round; }
  24. void setRound (int round) { this.round = round; }
  25. /** @} */
  26. /** @name Game's data */
  27. /** @{ */
  28. private int round;
  29. /** @} */
  30. /**
  31. * Main game loop
  32. */
  33. static boolean getArguments (String[] args) {
  34. boolean ret = true;
  35. for (int i =0 ; i<args.length ; ++i) {
  36. switch (args[i]) {
  37. case "-b":
  38. case "--board":
  39. if (i+1 < args.length)
  40. Session.boardSize = Integer.parseInt(args[++i]);
  41. break;
  42. case "-w":
  43. case "--walls":
  44. if (i+1 < args.length)
  45. Session.wallSize = Integer.parseInt(args[++i]);
  46. break;
  47. case "-s":
  48. case "--suplies":
  49. if (i+1 < args.length)
  50. Session.supplySize = Integer.parseInt(args[++i]);
  51. break;
  52. default:
  53. ret = false;
  54. case "-h":
  55. case "--help":
  56. System.out.println("Labyrinth Game");
  57. System.out.println("");
  58. System.out.println("Usage:");
  59. System.out.println("labyrinth [-b|--board <Num>] [-w|--walls <Num>] [-s|--supplies <Num>]");
  60. System.out.println("or");
  61. System.out.println("labyrinth -h|--help");
  62. System.out.println("");
  63. System.out.println("\t-b | --board: Sets the size of board's edge.");
  64. System.out.println("\t-w | --walls: Sets the number of walls on the board.");
  65. System.out.println("\t-s | --supplies: Sets the number of supplies on the board.");
  66. System.out.println("\t-h | --help: Print this and exit");
  67. break;
  68. }
  69. }
  70. return ret;
  71. }
  72. public static void main(String[] args) {
  73. try {
  74. // Get command line options
  75. Game.getArguments(args);
  76. // Create a game, a board and 2 players.
  77. Game game = new Game();
  78. Board board = new Board(Session.boardSize, Session.supplySize, Session.wallSize);
  79. Player T = new Player(1, "Theseus", board, 0);
  80. Player M = new Player(2, "Minotaur", board, Position.toID(Session.boardSize/2, Session.boardSize/2));
  81. // Populate data to the board
  82. board.createBoard(T.playerTileId(), M.playerTileId());
  83. // The game
  84. while (true) {
  85. int[] m;
  86. System.out.println();
  87. System.out.println("Round: " + (game.round()+1));
  88. m = T.move(T.playerTileId());
  89. System.out.println(T.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")");
  90. m = M.move(M.playerTileId());
  91. System.out.println(M.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")");
  92. board.printBoard(
  93. board.getStringRepresentation(T.playerTileId(), M.playerTileId())
  94. );
  95. // Termination cases
  96. if (T.getScore() == 4) {
  97. System.out.println(T.getName() + " Wins!!! Score =" + T.getScore());
  98. System.exit(0);
  99. }
  100. if (M.getScore() == 4 || M.playerTileId() == T.playerTileId()) {
  101. System.out.println(M.getName() + " Wins!!! Score =" + M.getScore());
  102. System.exit(0);
  103. }
  104. if (!(game.nextRound() < 100)) {
  105. System.out.println("New day has come... Tie!!!");
  106. System.exit(0);
  107. }
  108. }
  109. }
  110. catch (Exception e) {
  111. System.out.println(e.getMessage());
  112. System.exit(1);
  113. }
  114. }
  115. }