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.

Game.java 7.7 KiB

6 anni fa
6 anni fa
6 anni fa
6 anni fa
6 anni fa
6 anni fa
6 anni fa
6 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // Print caption
  191. System.out.println("================== Snake Game ==================");
  192. System.out.println ("Board: " +lines +"x" +columns +" with " +numOfSnakes +" snakes, "
  193. +numOfLadders +" ladders and " +numOfApples +" apples.");
  194. System.out.println ("Players: 2"); // For now
  195. System.out.println("");
  196. // Board creation
  197. Game game = new Game (lines, columns, numOfSnakes, numOfLadders, numOfApples);
  198. // game.getBoard().createElementBoard(); // Not explicitly required
  199. game.registerPlayer(1, "Player 1"); // Player registration
  200. game.registerPlayer(2, "Player 2");
  201. game.playOrder(); // Choose play order
  202. Player winner;
  203. do // Keep going until someone finishes
  204. winner = game.round ();
  205. while (winner == null);
  206. game.scoreSort (); // sort players based on their score
  207. // Print the results
  208. System.out.println("***** Game finished *****");
  209. System.out.println("");
  210. System.out.println("Rounds: " + game.getRound());
  211. System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]");
  212. System.out.println("Score: ");
  213. for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) {
  214. // Loop all players
  215. Player p = game.getPlayers().get(i);
  216. if (p == winner)
  217. System.out.println(" *" +p.getName() + ": " + p.getScore() +" points");
  218. else
  219. System.out.println(" " +p.getName() + ": " + p.getScore() +" points");
  220. }
  221. }
  222. }