A java snake game for A.U.TH. Data structures class
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.3 KiB

  1. package net.hoo2.auth.dsproject.snake;
  2. import java.util.Arrays;
  3. /**
  4. * @class Player
  5. * @brief Represent a Player in the Game
  6. *
  7. * The players are playing in a round-robin sequence and we keep track
  8. * for each one of them their playing order, score and place on the board.
  9. *
  10. * @author Christos Choutouridis AEM:8997
  11. * @email cchoutou@ece.auth.gr
  12. */
  13. public class Player {
  14. static final int MOVE_DATA_SIZE = 9;
  15. static final int MOVE_TILE_IDX = 0;
  16. static final int MOVE_INITTILE_IDX = 1;
  17. static final int MOVE_STEPS_IDX = 2;
  18. static final int MOVE_ROLL_IDX = 3;
  19. static final int MOVE_POINTS_IDX = 4;
  20. static final int MOVE_SNAKES_IDX = 5; // Always <= 1
  21. static final int MOVE_LADDERS_IDX = 6; // Always <= 1
  22. static final int MOVE_RED_APPLES_IDX = 7; // Always <= 1
  23. static final int MOVE_BLACK_APPLES_IDX = 8; // Always <= 1
  24. /** @name Constructors */
  25. /** @{ */
  26. /** Default doing nothing constructor */
  27. Player () {
  28. playerId = score = tile = 0;
  29. name = "";
  30. board = null;
  31. lastMove = new int[MOVE_DATA_SIZE];
  32. dryMove = new int[MOVE_DATA_SIZE];
  33. }
  34. /**
  35. * @brief The main constructor
  36. *
  37. * This creates a player for the game
  38. * @param playerId The player's to create
  39. * @param name The name of the player
  40. * @param board Reference to the board the player will play on.
  41. */
  42. Player (int playerId, String name, Board board) {
  43. this.playerId = playerId;
  44. this.name = name;
  45. this.board = board;
  46. score = 0;
  47. tile = 0;
  48. lastMove = new int[MOVE_DATA_SIZE];
  49. dryMove = new int[MOVE_DATA_SIZE];
  50. }
  51. /** @} */
  52. /** @name Get/Set interface */
  53. /** @{ */
  54. int getPlayerId () { return playerId; }
  55. void setPlayerId (int playerId) {
  56. this.playerId = playerId;
  57. }
  58. String getName () { return name; }
  59. void setName (String name) {
  60. this.name = name;
  61. }
  62. int getScore () { return score; }
  63. void setScore (int score) {
  64. this.score = score;
  65. }
  66. /** Get reference to Board */
  67. Board getBoard () { return board; }
  68. /** Set Board reference */
  69. void setBoard (Board board) {
  70. this.board = board;
  71. }
  72. /** Get tile */
  73. int getTile () { return tile; }
  74. /** Set tile */
  75. void setTile (int tile) {
  76. this.tile = tile;
  77. }
  78. int[] getLastMove () { return lastMove; }
  79. void setLastMove (int[] lastMove) {
  80. this.lastMove = lastMove;
  81. }
  82. /** @} */
  83. /** @name Exposed API members */
  84. /** @{ */
  85. /**
  86. * Dice functionality for the players
  87. * @return An integer in the range [1 .. 6]
  88. */
  89. int dice () {
  90. return (int)(1 + Math.random()*5);
  91. }
  92. // Liskov substitution principle
  93. int getNextMove (int tile) {
  94. return move (tile, dice(), true)[MOVE_TILE_IDX];
  95. }
  96. /**
  97. * @brief Move functionality
  98. * This function prints to stdout various logs about user interaction with elements
  99. *
  100. * @param tile The initial tile of the player
  101. * @param die The die to play
  102. * @return
  103. * int[MOVE_TILE_IDX (0)] tile after move
  104. * int[MOVE_INITTILE_IDX (1)] tile before move
  105. * int[MOVE_STEPS_IDX (2)] number of total steps for the move
  106. * int[MOVE_ROLL_IDX (3)] the roll of the dice
  107. * int[MOVE_POINTS_IDX (4)] the points of the move
  108. * int[MOVE_SNAKES_IDX (5)] number of snake bites
  109. * int[MOVE_LADDERS_IDX (6)] number of ladders used
  110. * int[MOVE_RED_APPLES_IDX (7)] number of red apples eaten
  111. * int[MOVE_BLACK_APPLES_IDX (8)] number of black apples eaten
  112. */
  113. int [] move (int tile, int roll, boolean run) {
  114. int t;
  115. Arrays.fill(dryMove, 0);
  116. dryMove[MOVE_INITTILE_IDX] = tile;
  117. dryMove[MOVE_ROLL_IDX] = roll;
  118. tile += roll; // Initial move
  119. boolean keepGoing;
  120. do {
  121. keepGoing = false;
  122. // Check apples
  123. if ((t = board.checkApple(tile, run)) != 0) {
  124. dryMove[MOVE_POINTS_IDX] += t;
  125. if (t > 0)
  126. ++dryMove[MOVE_RED_APPLES_IDX];
  127. else
  128. ++dryMove[MOVE_BLACK_APPLES_IDX];
  129. }
  130. // Check ladder
  131. if ((t = board.checkLadder(tile, run)) != tile) {
  132. tile = t;
  133. ++dryMove[MOVE_LADDERS_IDX];
  134. keepGoing = true;
  135. }
  136. // Check snakes
  137. if ((t = board.checkSnake(tile)) != tile) {
  138. tile = t;
  139. ++dryMove[MOVE_SNAKES_IDX];
  140. keepGoing = true;
  141. }
  142. } while (keepGoing);
  143. dryMove[MOVE_TILE_IDX] = tile;
  144. dryMove[MOVE_STEPS_IDX]= tile - dryMove[MOVE_INITTILE_IDX];
  145. if (run) {
  146. lastMove = dryMove.clone();
  147. this.tile = lastMove[MOVE_TILE_IDX];
  148. score += lastMove[MOVE_POINTS_IDX];
  149. }
  150. return dryMove;
  151. }
  152. //Liskov substitution principle
  153. void statistics (boolean verbose, boolean sum) {
  154. int begin = lastMove[MOVE_INITTILE_IDX];
  155. int roll = lastMove[MOVE_ROLL_IDX];
  156. int last = lastMove[MOVE_TILE_IDX];
  157. if (verbose)
  158. System.out.println(name + " +" + roll + "->tile: " + (begin + roll));
  159. if (lastMove[MOVE_RED_APPLES_IDX] > 0)
  160. System.out.println(name + " Apple " + lastMove[MOVE_POINTS_IDX] + " points");
  161. if (lastMove[MOVE_BLACK_APPLES_IDX] > 0)
  162. System.out.println(name + " Apple " + lastMove[MOVE_POINTS_IDX] + " points");
  163. if (lastMove[MOVE_LADDERS_IDX] > 0)
  164. System.out.println(name + " Ladder @" + (begin + roll) + " new position " + last);
  165. if (lastMove[MOVE_SNAKES_IDX] > 0)
  166. System.out.println(name + " Ouch!! Snake @" + (begin + roll) + " new position " + last);
  167. // No use of sum here
  168. }
  169. /**@} */
  170. /** @name Data members package access only */
  171. /** @{ */
  172. int playerId; /**< Player's ID */
  173. String name; /**< Player's name */
  174. int score; /**< Player's score */
  175. Board board; /**< Reference to current board */
  176. int tile; /**< Player's tile location */
  177. int[] lastMove;
  178. private int [] dryMove;
  179. /** @} */
  180. }