Labyrinth
A labyrinth game assignment
Game.java
Go to the documentation of this file.
1 
42 package host.labyrinth;
43 
44 import java.util.Scanner;
45 
50 public class Game {
51 
53  Game() {
54  scan = new Scanner(System.in);
55  }
56 
60  int round () { return round; }
62  int nextRound() { return ++round; }
63 
68  void waitUser () {
69  if(Session.interactive) {
70  System.out.println("Press enter to continue...");
71  scan.nextLine();
72  }
73  }
83  int getRound() { return round; }
84  void setRound (int round) { this.round = round; }
89  private int round;
90  private Scanner scan;
96  static boolean getArguments (String[] args) {
97  for (int i =0 ; i<args.length ; ++i) {
98  switch (args[i]) {
99  case "-b":
100  case "--board":
101  if (i+1 < args.length)
102  Session.boardSize = Integer.parseInt(args[++i]);
103  break;
104 
105  case "-s":
106  case "--supplies":
107  if (i+1 < args.length)
108  Session.supplySize = Integer.parseInt(args[++i]);
109  break;
110 
111  case "-r":
112  case "--rounds":
113  if (i+1 < args.length)
114  Session.maxRounds = Integer.parseInt(args[++i]);
115  break;
116 
117  case "--norooms":
118  Session.loopGuard = true;
119  break;
120 
121  case "-i":
122  case "--interactive":
123  Session.interactive = true;
124  break;
125 
126  default:
127  case "-h":
128  case "--help":
129  System.out.println("Labyrinth Game");
130  System.out.println("");
131  System.out.println("Usage:");
132  System.out.println("labyrinth [-b|--board <Num>] [-s|--supplies <Num>] [-r|--rounds <Num>] [--norooms] [-i|--interactive]");
133  System.out.println("or");
134  System.out.println("labyrinth -h|--help");
135  System.out.println("\nOptions\n");
136  System.out.println("-b | --board:\n Sets the size of board's edge.\n");
137  System.out.println("-s | --supplies:\n Sets the number of supplies on the board.\n");
138  System.out.println("-r | --rounds:\n Sets the maximum number of rounds of the game.\n");
139  System.out.println("--norooms:\n Prevents the creation of closed rooms inside the board.\n");
140  System.out.println("-i | --interactive:\n Each round requires user input in order to continue.\n");
141  System.out.println("-h | --help:\n Print this and exits.");
142  return false;
143  }
144  }
145  return true;
146  }
147 
148 
152  public static void main(String[] args) {
153  try {
154  // Get command line options
155  if (!Game.getArguments(args)) throw new Exception("");
156 
157  // Create a game, a board and 2 players.
158  Game game = new Game();
160  Player T = new MinMaxPlayer("Theseus", true, board, 0);
161  Player M = new Player("Minotaur", false, board, Position.toID(Session.boardSize/2, Session.boardSize/2));
162  Player players [] = {M, T};
163 
164  // Populate data to the board
165  board.createBoard(T.playerTileId(), M.playerTileId());
166 
167  // Initial board printout
168  System.out.println("Initial board: " + (game.round()));
169  board.printBoard(
170  board.getStringRepresentation(T.playerTileId(), M.playerTileId())
171  );
172  game.waitUser ();
173  // Main game loop
174  while (true) {
175  System.out.println();
176  System.out.println("Round: " + (game.round()+1));
177 
178  // Players moves
179  for (Player p : players) {
180  p.move(p.playerTileId());
181  p.statistics();
182  }
183  board.printBoard(
184  board.getStringRepresentation(T.playerTileId(), M.playerTileId())
185  );
186 
187  // Loop termination cases
188  if (T.getScore() == Session.supplySize) {
189  System.out.println("**** " + T.getName() + " Wins!!! Score =" + T.getScore() + " ****");
190  break;
191  }
192  if (M.playerTileId() == T.playerTileId()) {
193  System.out.println("**** " + M.getName() + " Wins!!!");
194  break;
195  }
196  if (!(game.nextRound() < Session.maxRounds)) {
197  System.out.println("**** New day has come... Tie! ****");
198  break;
199  }
200  game.waitUser ();
201  }
202  for (Player p : players)
203  p.final_statistics();
204  System.exit(0);
205  }
206  catch (Exception e) {
207  // We don't handle exceptions. Print error and exit with error status.
208  System.out.println(e.getMessage());
209  System.exit(1);
210  }
211  }
212 }
void createBoard(int theseusTile, int minotaurTile)
Creates the board with all the requested walls and supplies.
Definition: Board.java:110
static void main(String[] args)
Main game loop.
Definition: Game.java:152
void setRound(int round)
Definition: Game.java:84
This class is the representation of the games&#39;s board.
Definition: Board.java:25
This class represents the game&#39;s minimax player.
int round()
Utility to get current round of the game.
Definition: Game.java:60
int playerTileId()
Utility to access player&#39;s tileID.
Definition: Player.java:165
String [][] getStringRepresentation(int theseusTile, int minotaurTile)
Returns a 2-D array with the string representation of the board.
Definition: Board.java:129
static int supplySize
Default board&#39;s supply size (if no one set it via command line)
Definition: Common.java:45
This class represents the game&#39;s player.
Definition: Player.java:21
int nextRound()
Utility to increase and get the increased round of the game.
Definition: Game.java:62
static boolean getArguments(String[] args)
Command line argument handler.
Definition: Game.java:96
static boolean interactive
When true each round of the game requires user input.
Definition: Common.java:48
Main application class.
Definition: Game.java:50
Scanner scan
Input handle used in interactive mode.
Definition: Game.java:90
An Application wide board position implementation holding just the id coordinate. ...
Definition: Common.java:112
static boolean loopGuard
When true a wall creation guard is added to prevent closed rooms inside the board.
Definition: Common.java:47
static int toID(int row, int col)
Takes row and column coordinates and return the calculated Id coordinate.
Definition: Common.java:165
Game()
< An empty constructor
Definition: Game.java:53
Application wide object to hold settings like values for the session.
Definition: Common.java:43
static int boardSize
Default board&#39;s size (if no one set it via command line)
Definition: Common.java:44
static int maxRounds
Default number of rounds per game (if no one set it via command line)
Definition: Common.java:46
int round
Holds the round of the game.
Definition: Game.java:89
void printBoard(String[][] sBoard)
Print board utility.
Definition: Board.java:150
void waitUser()
Utility to hold the execution of the program waiting for user input.
Definition: Game.java:68