package net.hoo2.auth.dsproject.snake; import java.util.Arrays; /** * @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 AEM:8997 * @email cchoutou@ece.auth.gr */ public class Player { static final int MOVE_DATA_SIZE = 9; static final int MOVE_TILE_IDX = 0; static final int MOVE_INITTILE_IDX = 1; static final int MOVE_STEPS_IDX = 2; static final int MOVE_ROLL_IDX = 3; static final int MOVE_POINTS_IDX = 4; static final int MOVE_SNAKES_IDX = 5; // Always <= 1 static final int MOVE_LADDERS_IDX = 6; // Always <= 1 static final int MOVE_RED_APPLES_IDX = 7; // Always <= 1 static final int MOVE_BLACK_APPLES_IDX = 8; // Always <= 1 /** @name Constructors */ /** @{ */ /** Default doing nothing constructor */ Player () { playerId = score = tile = 0; name = ""; board = null; lastMove = new int[MOVE_DATA_SIZE]; dryMove = new int[MOVE_DATA_SIZE]; } /** * @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; lastMove = new int[MOVE_DATA_SIZE]; dryMove = new int[MOVE_DATA_SIZE]; } /** @} */ /** @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; } int[] getLastMove () { return lastMove; } void setLastMove (int[] lastMove) { this.lastMove = lastMove; } /** @} */ /** @name Exposed API members */ /** @{ */ /** * Dice functionality for the players * @return An integer in the range [1 .. 6] */ int dice () { return (int)(1 + Math.random()*5); } // Liskov substitution principle int getNextMove (int tile) { return move (tile, dice(), true)[MOVE_TILE_IDX]; } /** * @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[MOVE_TILE_IDX (0)] tile after move * int[MOVE_INITTILE_IDX (1)] tile before move * int[MOVE_STEPS_IDX (2)] number of total steps for the move * int[MOVE_ROLL_IDX (3)] the roll of the dice * int[MOVE_POINTS_IDX (4)] the points of the move * int[MOVE_SNAKES_IDX (5)] number of snake bites * int[MOVE_LADDERS_IDX (6)] number of ladders used * int[MOVE_RED_APPLES_IDX (7)] number of red apples eaten * int[MOVE_BLACK_APPLES_IDX (8)] number of black apples eaten */ int [] move (int tile, int roll, boolean run) { int t; Arrays.fill(dryMove, 0); dryMove[MOVE_INITTILE_IDX] = tile; dryMove[MOVE_ROLL_IDX] = roll; tile += roll; // Initial move boolean keepGoing; do { keepGoing = false; // Check apples if ((t = board.checkApple(tile, run)) != 0) { dryMove[MOVE_POINTS_IDX] += t; if (t > 0) ++dryMove[MOVE_RED_APPLES_IDX]; else ++dryMove[MOVE_BLACK_APPLES_IDX]; } // Check ladder if ((t = board.checkLadder(tile, run)) != tile) { tile = t; ++dryMove[MOVE_LADDERS_IDX]; keepGoing = true; } // Check snakes if ((t = board.checkSnake(tile)) != tile) { tile = t; ++dryMove[MOVE_SNAKES_IDX]; keepGoing = true; } } while (keepGoing); dryMove[MOVE_TILE_IDX] = tile; dryMove[MOVE_STEPS_IDX]= tile - dryMove[MOVE_INITTILE_IDX]; if (run) { lastMove = dryMove.clone(); this.tile = lastMove[MOVE_TILE_IDX]; score += lastMove[MOVE_POINTS_IDX]; } return dryMove; } //Liskov substitution principle void statistics (boolean verbose, boolean sum) { int begin = lastMove[MOVE_INITTILE_IDX]; int roll = lastMove[MOVE_ROLL_IDX]; int last = lastMove[MOVE_TILE_IDX]; if (verbose) System.out.println(name + " +" + roll + "->tile: " + (begin + roll)); if (lastMove[MOVE_RED_APPLES_IDX] > 0) System.out.println(name + " Apple " + lastMove[MOVE_POINTS_IDX] + " points"); if (lastMove[MOVE_BLACK_APPLES_IDX] > 0) System.out.println(name + " Apple " + lastMove[MOVE_POINTS_IDX] + " points"); if (lastMove[MOVE_LADDERS_IDX] > 0) System.out.println(name + " Ladder @" + (begin + roll) + " new position " + last); if (lastMove[MOVE_SNAKES_IDX] > 0) System.out.println(name + " Ouch!! Snake @" + (begin + roll) + " new position " + last); // No use of sum here } /**@} */ /** @name Data members package access only */ /** @{ */ int playerId; /**< Player's ID */ String name; /**< Player's name */ int score; /**< Player's score */ Board board; /**< Reference to current board */ int tile; /**< Player's tile location */ int[] lastMove; private int [] dryMove; /** @} */ }