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