@@ -395,8 +395,11 @@ class Board { | |||||
int S = tiles[tileId].hasSupply(supplies); | int S = tiles[tileId].hasSupply(supplies); | ||||
if (T && !M) return " T "; | if (T && !M) return " T "; | ||||
else if (M && !T) return " M "; | |||||
else if (T && M) return "T+M"; | else if (T && M) return "T+M"; | ||||
else if (M) { | |||||
if (S == Const.noSupply) return " M "; | |||||
else return "M+s"; | |||||
} | |||||
else if (S != Const.noSupply) | else if (S != Const.noSupply) | ||||
return String.format("s%02d", S+1); | return String.format("s%02d", S+1); | ||||
else return " "; | else return " "; | ||||
@@ -7,17 +7,38 @@ | |||||
package net.hoo2.auth.labyrinth; | package net.hoo2.auth.labyrinth; | ||||
import java.util.Scanner; | |||||
/** | /** | ||||
* Main application class. This class is the only public interface of | * Main application class. This class is the only public interface of | ||||
* the entire game. | * the entire game. | ||||
*/ | */ | ||||
public class Game { | public class Game { | ||||
Game() {} /**< An empty constructor */ | |||||
/**< 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; } | int round () { return round; } | ||||
/** Utility to increase and get the increased round of the game */ | |||||
int nextRound() { return ++round; } | 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 | * @name Accessor/Mutator interface | ||||
* @note | * @note | ||||
@@ -31,15 +52,14 @@ public class Game { | |||||
/** @name Game's data */ | /** @name Game's data */ | ||||
/** @{ */ | /** @{ */ | ||||
private int round; | |||||
private int round; /**< Holds the round of the game */ | |||||
private Scanner scan; /**< Input handle used in interactive mode */ | |||||
/** @} */ | /** @} */ | ||||
/** | /** | ||||
* Main game loop | |||||
* Command line argument handler | |||||
*/ | */ | ||||
static boolean getArguments (String[] args) { | static boolean getArguments (String[] args) { | ||||
boolean ret = true; | |||||
for (int i =0 ; i<args.length ; ++i) { | for (int i =0 ; i<args.length ; ++i) { | ||||
switch (args[i]) { | switch (args[i]) { | ||||
case "-b": | case "-b": | ||||
@@ -61,7 +81,6 @@ public class Game { | |||||
break; | break; | ||||
default: | default: | ||||
ret = false; | |||||
case "-h": | case "-h": | ||||
case "--help": | case "--help": | ||||
System.out.println("Labyrinth Game"); | System.out.println("Labyrinth Game"); | ||||
@@ -78,13 +97,17 @@ public class Game { | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
return ret; | |||||
return true; | |||||
} | } | ||||
/** | |||||
* Main game loop | |||||
*/ | |||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
try { | try { | ||||
// Get command line options | // Get command line options | ||||
Game.getArguments(args); | |||||
if (!Game.getArguments(args)) throw new Exception(""); | |||||
// Create a game, a board and 2 players. | // Create a game, a board and 2 players. | ||||
Game game = new Game(); | Game game = new Game(); | ||||
@@ -95,12 +118,19 @@ public class Game { | |||||
// Populate data to the board | // Populate data to the board | ||||
board.createBoard(T.playerTileId(), M.playerTileId()); | board.createBoard(T.playerTileId(), M.playerTileId()); | ||||
// The game | |||||
// 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) { | while (true) { | ||||
int[] m; | int[] m; | ||||
System.out.println(); | System.out.println(); | ||||
System.out.println("Round: " + (game.round()+1)); | |||||
System.out.println("State after round: " + (game.round()+1)); | |||||
// Player moves | |||||
m = T.move(T.playerTileId()); | m = T.move(T.playerTileId()); | ||||
System.out.println(T.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")"); | System.out.println(T.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")"); | ||||
m = M.move(M.playerTileId()); | m = M.move(M.playerTileId()); | ||||
@@ -109,7 +139,7 @@ public class Game { | |||||
board.getStringRepresentation(T.playerTileId(), M.playerTileId()) | board.getStringRepresentation(T.playerTileId(), M.playerTileId()) | ||||
); | ); | ||||
// Termination cases | |||||
// Loop termination cases | |||||
if (T.getScore() == 4) { | if (T.getScore() == 4) { | ||||
System.out.println(T.getName() + " Wins!!! Score =" + T.getScore()); | System.out.println(T.getName() + " Wins!!! Score =" + T.getScore()); | ||||
System.exit(0); | System.exit(0); | ||||
@@ -118,13 +148,15 @@ public class Game { | |||||
System.out.println(M.getName() + " Wins!!! Score =" + M.getScore()); | System.out.println(M.getName() + " Wins!!! Score =" + M.getScore()); | ||||
System.exit(0); | System.exit(0); | ||||
} | } | ||||
if (!(game.nextRound() < 100)) { | |||||
if (!(game.nextRound() < Session.maxRounds)) { | |||||
System.out.println("New day has come... Tie!!!"); | System.out.println("New day has come... Tie!!!"); | ||||
System.exit(0); | System.exit(0); | ||||
} | } | ||||
game.waitUser (); | |||||
} | } | ||||
} | } | ||||
catch (Exception e) { | catch (Exception e) { | ||||
// We don't handle exceptions. Print error and exit with error status. | |||||
System.out.println(e.getMessage()); | System.out.println(e.getMessage()); | ||||
System.exit(1); | System.exit(1); | ||||
} | } | ||||
@@ -6,7 +6,7 @@ package net.hoo2.auth.labyrinth; | |||||
* This class represents the game's player | * This class represents the game's player | ||||
*/ | */ | ||||
class Player { | class Player { | ||||
/** @name Contructors */ | |||||
/** @name Constructors */ | |||||
/** @{ */ | /** @{ */ | ||||
/** | /** | ||||
@@ -15,13 +15,14 @@ class Player { | |||||
* @param name The name of the player | * @param name The name of the player | ||||
* @param board Reference to the board of the game | * @param board Reference to the board of the game | ||||
*/ | */ | ||||
Player(int id, String name, Board board, int row, int column) { | |||||
Player(int id, String name, boolean champion, Board board, int row, int column) { | |||||
this.playerId = id; | this.playerId = id; | ||||
this.name = name; | this.name = name; | ||||
this.board = board; | this.board = board; | ||||
this.score = 0; | this.score = 0; | ||||
this.x = column; | this.x = column; | ||||
this.y = row; | this.y = row; | ||||
this.champion = champion; | |||||
} | } | ||||
/** | /** | ||||
@@ -30,13 +31,14 @@ class Player { | |||||
* @param name The name of the player | * @param name The name of the player | ||||
* @param board Reference to the board of the game | * @param board Reference to the board of the game | ||||
*/ | */ | ||||
Player(int id, String name, Board board, int tileId) { | |||||
Player(int id, String name, boolean champion, Board board, int tileId) { | |||||
this.playerId = id; | this.playerId = id; | ||||
this.name = name; | this.name = name; | ||||
this.board = board; | this.board = board; | ||||
this.score = 0; | this.score = 0; | ||||
this.x = Position.toCol(tileId); | this.x = Position.toCol(tileId); | ||||
this.y = Position.toRow(tileId); | this.y = Position.toRow(tileId); | ||||
this.champion = champion; | |||||
} | } | ||||
/** @} */ | /** @} */ | ||||
@@ -46,10 +48,10 @@ class Player { | |||||
/** | /** | ||||
* Player's move. | * Player's move. | ||||
* | * | ||||
* A player first throw a dice to get a random direction. Then checks if the direction | |||||
* is walkable. If it is, then goes to that tile, pick up a possible supply on the tile | |||||
* and update player's data. | |||||
* | |||||
* A player first throws a dice to get a random direction. Then checks if the direction | |||||
* is walkable. If it is, then goes to that tile and update player's data. | |||||
* If the player is a champion then he also picks up a possible supply from the tile. | |||||
* | |||||
* @param id The id of the starting tile. | * @param id The id of the starting tile. | ||||
* @return An array containing player's final position and possible supply of that position. | * @return An array containing player's final position and possible supply of that position. | ||||
* The array format is: | * The array format is: | ||||
@@ -61,7 +63,7 @@ class Player { | |||||
* </ul> | * </ul> | ||||
*/ | */ | ||||
int[] move(int id) { | int[] move(int id) { | ||||
// Init return array with the current data | |||||
// Initialize return array with the current data | |||||
int[] ret = {id, Position.toRow(id), Position.toCol(id), Const.noSupply}; | int[] ret = {id, Position.toRow(id), Position.toCol(id), Const.noSupply}; | ||||
int diceDirection = board.dice(); // throw the dice | int diceDirection = board.dice(); // throw the dice | ||||
@@ -71,8 +73,9 @@ class Player { | |||||
ret[0] = next.getId(); // Update player's and return data | ret[0] = next.getId(); // Update player's and return data | ||||
ret[1] = y = next.getRow(); | ret[1] = y = next.getRow(); | ||||
ret[2] = x = next.getCol(); | ret[2] = x = next.getCol(); | ||||
if ((ret[3] = board.tryPickSupply(next.getId())) != Const.noSupply) { | |||||
++score; // keep score | |||||
// In case of a champion player, try also to pick a supply | |||||
if (champion && (ret[3] = board.tryPickSupply(next.getId())) != Const.noSupply) { | |||||
++score; // keep score | |||||
System.out.println(name + ":\t*Found a supply. [score: " + score + "]"); | System.out.println(name + ":\t*Found a supply. [score: " + score + "]"); | ||||
} | } | ||||
} | } | ||||
@@ -81,8 +84,11 @@ class Player { | |||||
return ret; | return ret; | ||||
} | } | ||||
/** Utility to access player's tileID */ | |||||
int playerTileId() { return Position.toID(y, x); } | int playerTileId() { return Position.toID(y, x); } | ||||
/** Utility to access player's row position (row coordinate) */ | |||||
int playerRow() { return y; } | int playerRow() { return y; } | ||||
/** Utility to access player's column position (column coordinate) */ | |||||
int playerCol() { return x; } | int playerCol() { return x; } | ||||
/** @} */ | /** @} */ | ||||
@@ -122,5 +128,6 @@ class Player { | |||||
private int score; /**< The current score of the player */ | private int score; /**< The current score of the player */ | ||||
private int x; /**< The column coordinate of the player on the board */ | private int x; /**< The column coordinate of the player on the board */ | ||||
private int y; /**< The row coordinate of the player on the board */ | private int y; /**< The row coordinate of the player on the board */ | ||||
private boolean champion; /**< Champion indicate a player who plays against the Minotaur */ | |||||
/** @} */ | /** @} */ | ||||
} | } |