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.

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