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.

152 lines
5.5 KiB

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