A java snake game for A.U.TH. Data structures class
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.

138 lines
3.9 KiB

  1. package net.hoo2.auth.dsproject.snake;
  2. /**
  3. * @class Player
  4. * @brief Represent a Player in the Game
  5. *
  6. * The players are playing in a round-robin sequence and we keep track
  7. * for each one of them their playing order, score and place on the board.
  8. *
  9. * @author Christos Choutouridis 8997
  10. * @email cchoutou@ece.auth.gr
  11. */
  12. public class Player {
  13. /** @name Constructors */
  14. /** @{ */
  15. /** Default doing nothing constructor */
  16. Player () {
  17. playerId = score = tile = turn = 0;
  18. name = "";
  19. board = null;
  20. }
  21. /**
  22. * @brief The main constructor
  23. *
  24. * This creates a player for the game
  25. * @param playerId The player's to create
  26. * @param name The name of the player
  27. * @param board Reference to the board the player will play on.
  28. */
  29. Player (int playerId, String name, Board board) {
  30. this.playerId = playerId;
  31. this.name = name;
  32. this.board = board;
  33. score = 0;
  34. tile = 0;
  35. turn = 0;
  36. }
  37. /** @} */
  38. /** @name Get/Set interface */
  39. /** @{ */
  40. int getPlayerId () { return playerId; }
  41. void setPlayerId (int playerId) {
  42. this.playerId = playerId;
  43. }
  44. String getName () { return name; }
  45. void setName (String name) {
  46. this.name = name;
  47. }
  48. int getScore () { return score; }
  49. void setScore (int score) {
  50. this.score = score;
  51. }
  52. /** Get reference to Board */
  53. Board getBoard () { return board; }
  54. /** Set Board reference */
  55. void setBoard (Board board) {
  56. this.board = board;
  57. }
  58. /** Get tile */
  59. int getTile () { return tile; }
  60. /** Set tile */
  61. void setTile (int tile) {
  62. this.tile = tile;
  63. }
  64. /** Get turn */
  65. int getTurn () { return turn; }
  66. /** Set turn */
  67. void setTurn (int turn) {
  68. this.turn = turn;
  69. }
  70. /** @} */
  71. /** @name Exposed API members */
  72. /** @{ */
  73. /**
  74. * @brief Move functionality
  75. * This function prints to stdout various logs about user interaction with elements
  76. *
  77. * @param tile The initial tile of the player
  78. * @param die The die to play
  79. * @return
  80. * int[0] tile after move
  81. * int[1] number of snake bites
  82. * int[2] number of ladder used
  83. * int[3] number of apples eaten
  84. */
  85. int [] move (int tile, int die) {
  86. int [] ret = new int[4];
  87. int t;
  88. tile += die; // Initial move
  89. //System.out.println(name + " +" + die + "->tile: " + tile); //XXX: Debug only
  90. boolean keepGoing;
  91. do {
  92. keepGoing = false;
  93. // Check apples
  94. t = board.checkApple(tile);
  95. if (t != 0) {
  96. score += t;
  97. ++ret[3];
  98. System.out.println(name + " Apple @" + tile + " " + t + " points");
  99. }
  100. // Check ladder
  101. t = board.checkLadder(tile);
  102. if (t != tile) {
  103. System.out.println(name + " Ladder @" + tile + " new position " + t);
  104. tile = t;
  105. ++ret[2];
  106. keepGoing = true;
  107. }
  108. // Check snakes
  109. t = board.checkSnake(tile);
  110. if (t != tile) {
  111. System.out.println(name + " Ouch!! Snake @" + tile + " new position " + t);
  112. tile = t;
  113. ++ret[1];
  114. keepGoing = true;
  115. }
  116. } while (keepGoing);
  117. ret[0] = this.tile = tile;
  118. return ret;
  119. }
  120. /**@} */
  121. /** @name Data members (private) */
  122. /** @{ */
  123. private int playerId; /**< Player's ID */
  124. private String name; /**< Player's name */
  125. private int score; /**< Player's score */
  126. private Board board; /**< Reference to current board */
  127. private int tile; /**< Player's tile location */
  128. private int turn; /**< Player's turn of playing */
  129. /** @} */
  130. }