Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

164 lines
6.0 KiB

  1. /**
  2. * @file Player.java
  3. *
  4. * @author
  5. * Anastasia Foti AEM:8959
  6. * <anastaskf@ece.auth.gr>
  7. *
  8. * @author
  9. * Christos Choutouridis AEM:8997
  10. * <cchoutou@ece.auth.gr>
  11. */
  12. package host.labyrinth;
  13. /**
  14. * @brief
  15. * This class represents the game's player
  16. */
  17. class Player {
  18. /** @name Constructors */
  19. /** @{ */
  20. /**
  21. * Create a new player and put him at the row-column coordinates
  22. * @param id The id of the player
  23. * @param name The name of the player
  24. * @param champion Flag to indicate if a player is a `champion`
  25. * @param board Reference to the board of the game
  26. * @param row The row coordinate of initial player position
  27. * @param column The column coordinate of initial player's position
  28. */
  29. Player(String name, boolean champion, Board board, int row, int column) {
  30. this.playerId = board.generatePlayerId();
  31. this.name = name;
  32. this.board = board;
  33. this.score = 0;
  34. this.x = column;
  35. this.y = row;
  36. this.champion = champion;
  37. int[] m = {Position.toID(row, column), row, column, Const.noSupply};
  38. board.updateMove(m, playerId);
  39. }
  40. /**
  41. * Create a new player and put him at the row-column coordinates
  42. * @param id The id of the player
  43. * @param name The name of the player
  44. * @param champion Flag to indicate if a player is a `champion`
  45. * @param board Reference to the board of the game
  46. * @param tileId The tileId coordinate of player's initial position
  47. */
  48. Player(String name, boolean champion, Board board, int tileId) {
  49. this.playerId = board.generatePlayerId();
  50. this.name = name;
  51. this.board = board;
  52. this.score = 0;
  53. this.x = Position.toCol(tileId);
  54. this.y = Position.toRow(tileId);
  55. this.champion = champion;
  56. int[] m = {tileId, Position.toRow(tileId), Position.toCol(tileId), Const.noSupply};
  57. board.updateMove(m, playerId);
  58. }
  59. /** @} */
  60. /** @name Player main application interface */
  61. /** @{ */
  62. /**
  63. * Player's move.
  64. *
  65. * A player first throws a dice to get a random direction. Then checks if the direction
  66. * is walkable. If it is, then goes to that tile and update player's data.
  67. * If the player is a champion then he also picks up a possible supply from the tile.
  68. *
  69. * @param id The id of the starting tile.
  70. * @return An array containing player's final position and possible supply of that position.
  71. * The array format is:
  72. * <ul>
  73. * <li> int[0]: The tileId of the final player's position.
  74. * <li> int[1]: The row of the final player's position.
  75. * <li> int[2]: The column of the final player's position.
  76. * <li> int[1]: The supplyId in case player picked one (Const.noSupply otherwise).
  77. * </ul>
  78. */
  79. int[] move(int id) {
  80. // Initialize return array with the current data
  81. int[] ret = new int[Const.moveItems];
  82. ret[0] = id;
  83. ret[1] = Position.toRow(id);
  84. ret[2] = Position.toCol(id);
  85. ret[3] = Const.noSupply;
  86. int diceDirection = board.dice(); // throw the dice
  87. if (board.isWalkable(id, diceDirection)) { // The result is walkable
  88. // Get next tile
  89. Position next = new Position(Position.toRow(id), Position.toCol(id), diceDirection);
  90. ret[0] = next.getId(); // Update player's and return data
  91. ret[1] = y = next.getRow();
  92. ret[2] = x = next.getCol();
  93. // In case of a champion player, try also to pick a supply
  94. if (champion && (ret[3] = board.tryPickSupply(next.getId())) != Const.noSupply) {
  95. ++score; // keep score
  96. System.out.println(name + ":\t*Found a supply. [score: " + score + "]");
  97. }
  98. board.updateMove(ret, playerId);
  99. }
  100. else
  101. System.out.println(name + ":\t*Can not move.");
  102. return ret;
  103. }
  104. /** Utility to access player's tileID */
  105. int playerTileId() { return Position.toID(y, x); }
  106. /** Utility to access player's row position (row coordinate) */
  107. int playerRow() { return y; }
  108. /** Utility to access player's column position (column coordinate) */
  109. int playerCol() { return x; }
  110. /** @} */
  111. /**
  112. * @name Accessor/Mutator interface
  113. * @note
  114. * Please consider not to use mutator interface. Its the abstraction killer :(
  115. * We have added a bit of logic however, in order to make it a bit more safe.
  116. */
  117. /** @{ */
  118. int getPlayerId () { return playerId; }
  119. String getName() { return name; }
  120. Board getBoard () { return board; }
  121. int getScore () { return score; }
  122. int getX() { return x; }
  123. int getY() { return y; }
  124. boolean getChampion(){ return champion; }
  125. void setPlayerId(int id) { playerId = id; }
  126. void setName(String name) { this.name = name; }
  127. void setBoard (Board board){ this.board = board; }
  128. void setScore(int score) { this.score = score; }
  129. void setX(int x) {
  130. assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
  131. this.x = x;
  132. }
  133. void setY(int y) {
  134. assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
  135. this.y = y;
  136. }
  137. void setChampion (boolean champion) {
  138. this.champion = champion;
  139. }
  140. /** @} */
  141. /** @name Class data */
  142. /** @{ */
  143. protected int playerId; /**< The unique identifier of the player */
  144. protected String name; /**< The name of the player */
  145. protected Board board; /**< Reference to the session's boards */
  146. protected int score; /**< The current score of the player */
  147. protected int x; /**< The column coordinate of the player on the board */
  148. protected int y; /**< The row coordinate of the player on the board */
  149. protected boolean champion; /**< Champion indicate a player who plays against the Minotaur */
  150. /** @} */
  151. }