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.

134 lines
4.9 KiB

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