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.

243 lignes
7.8 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. }
  87. /** @} */
  88. /** Get/Set interface */
  89. /** @{ */
  90. int getRound () { return round; }
  91. void setRound (int round) { this.round = round; }
  92. /** Get reference to board */
  93. Board getBoard () { return board; }
  94. /** Set board
  95. * @param board Reference to board to use
  96. * @note This requires board must be allocated elsewhere.
  97. */
  98. void setBoard (Board board) {
  99. this.board = board;
  100. }
  101. /** Get reference to players */
  102. ArrayList<Player> getPlayers() { return players; }
  103. /**
  104. * Set players
  105. * @param players Reference to players to use
  106. * @note This requires players must be allocated elsewhere.
  107. */
  108. void setPlayers(ArrayList<Player> players) {
  109. this.players = players;
  110. }
  111. /** @} */
  112. /** Public functionality */
  113. /** @{ */
  114. /**
  115. * Register a player to the game
  116. * @param playerId The player ID to use
  117. * @param name The player name to use
  118. * @return The status of the operation
  119. */
  120. boolean registerPlayer (int playerId, String name) {
  121. if (players.size() >= MAX_PLAYERS)
  122. return false;
  123. players.add(new Player(playerId, name, board));
  124. return true;
  125. }
  126. /**
  127. * @brief Set the playing order of players
  128. * This function emulates the classic roll of dice to decide which player
  129. * plays first which second and so on.
  130. */
  131. void playOrder () {
  132. int d;
  133. for (int i =0 ; i<players.size() ; ++i) {
  134. do
  135. // Keep rolling the dice as the die belongs to another user
  136. d = _dice();
  137. while (_search (d, players));
  138. players.get(i).setTurn(d);
  139. }
  140. // Sort players vector
  141. players.sort((p1, p2) ->
  142. Integer.compare (p1.getTurn(), p2.getTurn())
  143. );
  144. }
  145. /**
  146. * Sort the players according to their score
  147. */
  148. void scoreSort () {
  149. players.sort((p1, p2) ->
  150. Integer.compare (p1.getScore(), p2.getScore())
  151. );
  152. }
  153. /**
  154. * A game round. In each round every player plays when is its turn
  155. *
  156. * @return The winner if we have one, or null
  157. */
  158. Player round () {
  159. int [] mret;
  160. ++round; // keep track of round
  161. // traverse the players vector and move each player on the board
  162. // using a dice throw
  163. for (int i =0 ; i<players.size() ; ++i) {
  164. mret = players.get(i).move (players.get(i).getTile(), _dice());
  165. if (mret[0]>= board.getN()*board.getM())
  166. // The first one here is the winner
  167. return players.get(i);
  168. }
  169. return null; // No one finished yet
  170. }
  171. /** @} */
  172. /**
  173. * @brief Main
  174. * As the requirements of the project suggested.
  175. * We:
  176. * * Create a game
  177. * * Manually create a board
  178. * * Register 2 players (John Doe for now)
  179. * * Place a predefined number of snakes, ladders and apples
  180. * * Deploy the game by calling @ref playOrder() and @ref round()
  181. * At the end we print the results and exit
  182. */
  183. public static void main(String[] args) {
  184. // Current project requirements
  185. int lines = 20;
  186. int columns = 10;
  187. int numOfSnakes = 3;
  188. int numOfLadders = 3;
  189. int numOfApples = 6;
  190. int numOfPlayers = 2;
  191. // Print caption
  192. System.out.println("================== Snake Game ==================");
  193. System.out.println("Board: " +lines +"x" +columns +" with " +numOfSnakes +" snakes, "
  194. +numOfLadders +" ladders and " +numOfApples +" apples.");
  195. System.out.println("Players: " + numOfPlayers);
  196. System.out.println("");
  197. // Board creation
  198. Game game = new Game (lines, columns, numOfSnakes, numOfLadders, numOfApples);
  199. // game.getBoard().createElementBoard(); // Not explicitly required
  200. // Player registration
  201. for (int i=0 ; i<numOfPlayers && i<MAX_PLAYERS; ++i)
  202. game.registerPlayer(i+1, String.format("Player %d", i+1));
  203. game.playOrder(); // Choose play order
  204. Player winner;
  205. do // Keep going until someone finishes
  206. winner = game.round ();
  207. while (winner == null);
  208. game.scoreSort (); // sort players based on their score
  209. // Print the results
  210. System.out.println("***** Game finished *****");
  211. System.out.println("");
  212. System.out.println("Rounds: " + game.getRound());
  213. System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]");
  214. System.out.println("Score: ");
  215. for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) {
  216. // Loop all players
  217. Player p = game.getPlayers().get(i);
  218. if (p == winner)
  219. System.out.println(" *" +p.getName() + ": " + p.getScore() +" points");
  220. else
  221. System.out.println(" " +p.getName() + ": " + p.getScore() +" points");
  222. }
  223. }
  224. }