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