/** * @file Game.java * * @author Christos Choutouridis AEM:8997 * @email cchoutou@ece.auth.gr */ package net.hoo2.auth.labyrinth; import java.util.Scanner; /** * Main application class. This class is the only public interface of * the entire game. */ public class Game { /**< An empty constructor */ Game() { scan = new Scanner(System.in); } /** @name Player main application interface */ /** @{ */ /** Utility to get current round of the game */ int round () { return round; } /** Utility to increase and get the increased round of the game */ int nextRound() { return ++round; } /** * Utility to hold the execution of the program waiting for user input. * This is true only if the user passed the interactive flag. */ void waitUser () { if(Session.interactive) { System.out.println("Press enter to continue..."); scan.nextLine(); } } /** @{ */ /** * @name Accessor/Mutator interface * @note * Please consider not to use mutator interface. Its the abstraction killer :( * We have added a bit of logic however, in order to make it a bit more safe. */ /** @{ */ int getRound() { return round; } void setRound (int round) { this.round = round; } /** @} */ /** @name Game's data */ /** @{ */ private int round; /**< Holds the round of the game */ private Scanner scan; /**< Input handle used in interactive mode */ /** @} */ /** * Command line argument handler */ static boolean getArguments (String[] args) { for (int i =0 ; i] [-w|--walls ] [-s|--supplies ]"); System.out.println("or"); System.out.println("labyrinth -h|--help"); System.out.println(""); System.out.println("\t-b | --board: Sets the size of board's edge."); System.out.println("\t-w | --walls: Sets the number of walls on the board."); System.out.println("\t-s | --supplies: Sets the number of supplies on the board."); System.out.println("\t-h | --help: Print this and exit"); break; } } return true; } /** * Main game loop */ public static void main(String[] args) { try { // Get command line options if (!Game.getArguments(args)) throw new Exception(""); // Create a game, a board and 2 players. Game game = new Game(); Board board = new Board(Session.boardSize, Session.supplySize, Session.wallSize); Player T = new Player(1, "Theseus", board, 0); Player M = new Player(2, "Minotaur", board, Position.toID(Session.boardSize/2, Session.boardSize/2)); // Populate data to the board board.createBoard(T.playerTileId(), M.playerTileId()); // Initial board printout System.out.println("Initial board: " + (game.round())); board.printBoard( board.getStringRepresentation(T.playerTileId(), M.playerTileId()) ); game.waitUser (); // Main game loop while (true) { int[] m; System.out.println(); System.out.println("State after round: " + (game.round()+1)); // Player moves m = T.move(T.playerTileId()); System.out.println(T.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")"); m = M.move(M.playerTileId()); System.out.println(M.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")"); board.printBoard( board.getStringRepresentation(T.playerTileId(), M.playerTileId()) ); // Loop termination cases if (T.getScore() == 4) { System.out.println(T.getName() + " Wins!!! Score =" + T.getScore()); System.exit(0); } if (M.getScore() == 4 || M.playerTileId() == T.playerTileId()) { System.out.println(M.getName() + " Wins!!! Score =" + M.getScore()); System.exit(0); } if (!(game.nextRound() < Session.maxRounds)) { System.out.println("New day has come... Tie!!!"); System.exit(0); } game.waitUser (); } } catch (Exception e) { // We don't handle exceptions. Print error and exit with error status. System.out.println(e.getMessage()); System.exit(1); } } }