|
|
@@ -0,0 +1,137 @@ |
|
|
|
package SnakePkg;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Player
|
|
|
|
* @brief Represent a Player in the Game
|
|
|
|
*
|
|
|
|
* The players are playing in a round-robin sequence and we keep track
|
|
|
|
* for each one of them their playing order, score and place on the board.
|
|
|
|
*
|
|
|
|
* @author Christos Choutouridis 8997
|
|
|
|
* @email cchoutou@ece.auth.gr
|
|
|
|
*/
|
|
|
|
public class Player {
|
|
|
|
/** @name Constructors */
|
|
|
|
/** @{ */
|
|
|
|
/** Default doing nothing constructor */
|
|
|
|
Player () {
|
|
|
|
playerId = score = tile = turn = 0;
|
|
|
|
name = "";
|
|
|
|
board = null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief The main constructor
|
|
|
|
*
|
|
|
|
* This creates a player for the game
|
|
|
|
* @param playerId The player's to create
|
|
|
|
* @param name The name of the player
|
|
|
|
* @param board Reference to the board the player will play on.
|
|
|
|
*/
|
|
|
|
Player (int playerId, String name, Board board) {
|
|
|
|
this.playerId = playerId;
|
|
|
|
this.name = name;
|
|
|
|
this.board = board;
|
|
|
|
score = 0;
|
|
|
|
tile = 0;
|
|
|
|
turn = 0;
|
|
|
|
}
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/** @name Get/Set interface */
|
|
|
|
/** @{ */
|
|
|
|
int getPlayerId () { return playerId; }
|
|
|
|
void setPlayerId (int playerId) {
|
|
|
|
this.playerId = playerId;
|
|
|
|
}
|
|
|
|
String getName () { return name; }
|
|
|
|
void setName (String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
int getScore () { return score; }
|
|
|
|
void setScore (int score) {
|
|
|
|
this.score = score;
|
|
|
|
}
|
|
|
|
/** Get reference to Board */
|
|
|
|
Board getBoard () { return board; }
|
|
|
|
/** Set Board reference */
|
|
|
|
void setBoard (Board board) {
|
|
|
|
this.board = board;
|
|
|
|
}
|
|
|
|
/** Get tile */
|
|
|
|
int getTile () { return tile; }
|
|
|
|
/** Set tile */
|
|
|
|
void setTile (int tile) {
|
|
|
|
this.tile = tile;
|
|
|
|
}
|
|
|
|
/** Get turn */
|
|
|
|
int getTurn () { return turn; }
|
|
|
|
/** Set turn */
|
|
|
|
void setTurn (int turn) {
|
|
|
|
this.turn = turn;
|
|
|
|
}
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/** @name Exposed API members */
|
|
|
|
/** @{ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Move functionality
|
|
|
|
* This function prints to stdout various logs about user interaction with elements
|
|
|
|
*
|
|
|
|
* @param tile The initial tile of the player
|
|
|
|
* @param die The die to play
|
|
|
|
* @return
|
|
|
|
* int[0] tile after move
|
|
|
|
* int[1] number of snake bites
|
|
|
|
* int[2] number of ladder used
|
|
|
|
* int[3] number of apples eaten
|
|
|
|
*/
|
|
|
|
int [] move (int tile, int die) {
|
|
|
|
int [] ret = new int[4];
|
|
|
|
int t;
|
|
|
|
|
|
|
|
tile += die; // Initial move
|
|
|
|
//System.out.println(name + " +" + die + "->tile: " + tile); //XXX: Debug only
|
|
|
|
boolean keepGoing;
|
|
|
|
do {
|
|
|
|
keepGoing = false;
|
|
|
|
// Check apples
|
|
|
|
t = board.checkApple(tile);
|
|
|
|
if (t != 0) {
|
|
|
|
score += t;
|
|
|
|
++ret[3];
|
|
|
|
System.out.println(name + " Apple @" + tile + " " + t + " points");
|
|
|
|
}
|
|
|
|
// Check ladder
|
|
|
|
t = board.checkLadder(tile);
|
|
|
|
if (t != tile) {
|
|
|
|
System.out.println(name + " Ladder @" + tile + " new position " + t);
|
|
|
|
tile = t;
|
|
|
|
++ret[2];
|
|
|
|
keepGoing = true;
|
|
|
|
}
|
|
|
|
// Check snakes
|
|
|
|
t = board.checkSnake(tile);
|
|
|
|
if (t != tile) {
|
|
|
|
System.out.println(name + " Ouch!! Snake @" + tile + " new position " + t);
|
|
|
|
tile = t;
|
|
|
|
++ret[1];
|
|
|
|
keepGoing = true;
|
|
|
|
}
|
|
|
|
} while (keepGoing);
|
|
|
|
ret[0] = this.tile = tile;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/**@} */
|
|
|
|
|
|
|
|
|
|
|
|
/** @name Data members (private) */
|
|
|
|
/** @{ */
|
|
|
|
private int playerId; /**< Player's ID */
|
|
|
|
private String name; /**< Player's name */
|
|
|
|
private int score; /**< Player's score */
|
|
|
|
private Board board; /**< Reference to current board */
|
|
|
|
private int tile; /**< Player's tile location */
|
|
|
|
private int turn; /**< Player's turn of playing */
|
|
|
|
/** @} */
|
|
|
|
}
|