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.

127 lines
4.4 KiB

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