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