A java snake game for A.U.TH. Data structures class
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

211 lignes
6.7 KiB

  1. package SnakePkg;
  2. /**
  3. * @mainpage
  4. * @title Snake game project. -- Part 1 --
  5. *
  6. * This is the code documentation page of the Snake game project.
  7. * Listed are:
  8. * * All used classes
  9. * * All member functions
  10. * * All data members
  11. *
  12. * @author Christos Choutouridis 8997
  13. * @email cchoutou@ece.auth.gr
  14. */
  15. import java.lang.Math;
  16. import java.util.ArrayList;
  17. /**
  18. * @class Game
  19. *
  20. * @brief This is the main control class of the game.
  21. *
  22. * This class includes the main function.Using this class's api
  23. * the user can create a board, register players and roll the game.
  24. *
  25. * @author Christos Choutouridis 8997
  26. * @email cchoutou@ece.auth.gr
  27. */
  28. public class Game {
  29. /** Constants */
  30. /**@{ */
  31. static final int MAX_PLAYERS = 4; /**< The maximum number of allowed players in the game */
  32. /**@} */
  33. /** Private data members */
  34. /** @{ */
  35. private int round; /**< The current round of the game */
  36. private Board board; /**< A reference to board */
  37. private ArrayList<Player> players; /**< A reference to players */
  38. /** @} */
  39. /** private api */
  40. /** @{ */
  41. /**
  42. * Dice functionality
  43. * @return An integer in the range [1 .. 6]
  44. */
  45. private int _dice () {
  46. return (int)(1 + Math.random()*5);
  47. }
  48. /**
  49. * Search the players already in the players vector and compare their turn to play
  50. * with the result of a dice. If there is another one with the same dice result return true.
  51. *
  52. * @param turn The dice result to check in order to find player's turn to play
  53. * @param players Reference to already register players
  54. * @return True if there is another player with the same dice result
  55. */
  56. private boolean _search (int die, ArrayList<Player> players) {
  57. for (int i =0; i<players.size() ; ++i)
  58. if (players.get(i).getTurn() == die)
  59. return true;
  60. return false;
  61. }
  62. /** @} */
  63. /** Constructors */
  64. /** @{ */
  65. /** Default doing nothing constructor */
  66. Game () {
  67. round = 0;
  68. board = new Board();
  69. players = new ArrayList<>();
  70. }
  71. /**
  72. * @brief The main constructor
  73. * This constructor create a board and prepares the board for the game.
  74. * After this call the user can call @ref registerPlayer()
  75. * @param N The row for the board
  76. * @param M The columns of the board
  77. * @param numOfSnakes Number of snakes to place
  78. * @param numOfLadders Number of ladders to place
  79. * @param numOfApples Number of Apples to place
  80. */
  81. Game (int N, int M, int numOfSnakes, int numOfLadders, int numOfApples) {
  82. round = 0;
  83. // delegate constructors
  84. board = new Board (N, M, numOfSnakes, numOfLadders, numOfApples);
  85. players = new ArrayList<>();
  86. board.createElementBoard(); //Not critical, but placed here as project requirement
  87. }
  88. /** @} */
  89. /** Get/Set interface */
  90. /** @{ */
  91. int getRound () { return round; }
  92. void setRound (int round) { this.round = round; }
  93. /** Get reference to board */
  94. Board getBoard () { return board; }
  95. /** Set board
  96. * @param board Reference to board to use
  97. * @note This requires board must be allocated elsewhere.
  98. */
  99. void setBoard (Board board) {
  100. this.board = board;
  101. }
  102. /** Get reference to players */
  103. ArrayList<Player> getPlayers() { return players; }
  104. /**
  105. * Set players
  106. * @param players Reference to players to use
  107. * @note This requires players must be allocated elsewhere.
  108. */
  109. void setPlayers(ArrayList<Player> players) {
  110. this.players = players;
  111. }
  112. /** @} */
  113. /** Public functionality */
  114. /** @{ */
  115. /**
  116. * Register a player to the game
  117. * @param playerId The player ID to use
  118. * @param name The player name to use
  119. * @return The status of the operation
  120. */
  121. boolean registerPlayer (int playerId, String name) {
  122. if (players.size() >= MAX_PLAYERS)
  123. return false;
  124. players.add(new Player(playerId, name, board));
  125. return true;
  126. }
  127. /**
  128. * @brief Set the playing order of players
  129. * This function emulates the classic roll of dice to decide which player
  130. * plays first which second and so on.
  131. */
  132. void playOrder () {
  133. int d;
  134. for (int i =0 ; i<players.size() ; ++i) {
  135. do
  136. // Keep rolling the dice as the die belongs to another user
  137. d = _dice();
  138. while (_search (d, players));
  139. players.get(i).setTurn(d);
  140. }
  141. // Sort players vector
  142. players.sort((p1, p2) -> Integer.compare(p1.getTurn(), p2.getTurn()));
  143. }
  144. /**
  145. * A game round. In each round every player plays when is its turn
  146. *
  147. * @return The winner if we have one, or null
  148. */
  149. Player round () {
  150. int [] mret;
  151. ++round; // keep track of round
  152. // traverse the players vector and move each player on the board
  153. // using a dice throw
  154. for (int i =0 ; i<players.size() ; ++i) {
  155. mret = players.get(i).move (players.get(i).getTile(), _dice());
  156. if (mret[0]>= board.getN()*board.getM())
  157. // The first one here is the winner
  158. return players.get(i);
  159. }
  160. return null; // No one finished yet
  161. }
  162. /** @} */
  163. /**
  164. * @brief Main
  165. * As the requirements of the project suggested.
  166. * We:
  167. * * Create a game
  168. * * Manually create a board
  169. * * Register 2 players (John Doe for now)
  170. * * Place a predefined number of snakes, ladders and apples
  171. * * Deploy the game by calling @ref playOrder() and @ref round()
  172. * At the end we print the results and exit
  173. */
  174. public static void main(String[] args) {
  175. Game game = new Game(20, 10, 3, 3, 6); // Board creation
  176. game.registerPlayer(1, "Player 1"); // Player registration
  177. game.registerPlayer(2, "Player 2");
  178. game.playOrder(); // Choose play order
  179. Player winner;
  180. do // Keep going until someone finishes
  181. winner = game.round ();
  182. while (winner == null);
  183. // Print the results
  184. System.out.println("Game finished.");
  185. System.out.println("Rounds played: " + game.getRound());
  186. System.out.println("Winner: " + winner.getName() + ". Score: " + winner.getScore());
  187. for (int i=0 ; i<game.getPlayers().size() ; ++i) {
  188. // Loop all the non-winning players
  189. Player p = game.getPlayers().get(i);
  190. if (p != winner)
  191. System.out.println(p.getName() + ". Score: " + p.getScore());
  192. }
  193. }
  194. }