|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- package SnakePkg;
-
- /**
- * @mainpage
- * @title Snake game project. -- Part 1 --
- *
- * This is the code documentation page of the Snake game project.
- * Listed are:
- * * All used classes
- * * All member functions
- * * All data members
- *
- * @author Christos Choutouridis 8997
- * @email cchoutou@ece.auth.gr
- */
-
- import java.lang.Math;
- import java.util.ArrayList;
-
- /**
- * @class Game
- *
- * @brief This is the main control class of the game.
- *
- * This class includes the main function.Using this class's api
- * the user can create a board, register players and roll the game.
- *
- * @author Christos Choutouridis 8997
- * @email cchoutou@ece.auth.gr
- */
- public class Game {
- /** Constants */
- /**@{ */
- static final int MAX_PLAYERS = 4; /**< The maximum number of allowed players in the game */
- /**@} */
-
- /** Private data members */
- /** @{ */
- private int round; /**< The current round of the game */
- private Board board; /**< A reference to board */
- private ArrayList<Player> players; /**< A reference to players */
- /** @} */
-
- /** private api */
- /** @{ */
- /**
- * Dice functionality
- * @return An integer in the range [1 .. 6]
- */
- private int _dice () {
- return (int)(1 + Math.random()*5);
- }
- /**
- * Search the players already in the players vector and compare their turn to play
- * with the result of a dice. If there is another one with the same dice result return true.
- *
- * @param turn The dice result to check in order to find player's turn to play
- * @param players Reference to already register players
- * @return True if there is another player with the same dice result
- */
- private boolean _search (int die, ArrayList<Player> players) {
- for (int i =0; i<players.size() ; ++i)
- if (players.get(i).getTurn() == die)
- return true;
- return false;
- }
- /** @} */
-
-
- /** Constructors */
- /** @{ */
- /** Default doing nothing constructor */
- Game () {
- round = 0;
- board = new Board();
- players = new ArrayList<>();
- }
-
- /**
- * @brief The main constructor
- * This constructor create a board and prepares the board for the game.
- * After this call the user can call @ref registerPlayer()
- * @param N The row for the board
- * @param M The columns of the board
- * @param numOfSnakes Number of snakes to place
- * @param numOfLadders Number of ladders to place
- * @param numOfApples Number of Apples to place
- */
- Game (int N, int M, int numOfSnakes, int numOfLadders, int numOfApples) {
- round = 0;
- // delegate constructors
- board = new Board (N, M, numOfSnakes, numOfLadders, numOfApples);
- players = new ArrayList<>();
- }
- /** @} */
-
- /** Get/Set interface */
- /** @{ */
- int getRound () { return round; }
- void setRound (int round) { this.round = round; }
- /** Get reference to board */
- Board getBoard () { return board; }
- /** Set board
- * @param board Reference to board to use
- * @note This requires board must be allocated elsewhere.
- */
- void setBoard (Board board) {
- this.board = board;
- }
- /** Get reference to players */
- ArrayList<Player> getPlayers() { return players; }
- /**
- * Set players
- * @param players Reference to players to use
- * @note This requires players must be allocated elsewhere.
- */
- void setPlayers(ArrayList<Player> players) {
- this.players = players;
- }
- /** @} */
-
- /** Public functionality */
- /** @{ */
- /**
- * Register a player to the game
- * @param playerId The player ID to use
- * @param name The player name to use
- * @return The status of the operation
- */
- boolean registerPlayer (int playerId, String name) {
- if (players.size() >= MAX_PLAYERS)
- return false;
- players.add(new Player(playerId, name, board));
- return true;
- }
- /**
- * @brief Set the playing order of players
- * This function emulates the classic roll of dice to decide which player
- * plays first which second and so on.
- */
- void playOrder () {
- int d;
- for (int i =0 ; i<players.size() ; ++i) {
- do
- // Keep rolling the dice as the die belongs to another user
- d = _dice();
- while (_search (d, players));
- players.get(i).setTurn(d);
- }
- // Sort players vector
- players.sort((p1, p2) ->
- Integer.compare (p1.getTurn(), p2.getTurn())
- );
- }
-
- /**
- * Sort the players according to their score
- */
- void scoreSort () {
- players.sort((p1, p2) ->
- Integer.compare (p1.getScore(), p2.getScore())
- );
- }
-
- /**
- * A game round. In each round every player plays when is its turn
- *
- * @return The winner if we have one, or null
- */
- Player round () {
- int [] mret;
- ++round; // keep track of round
-
- // traverse the players vector and move each player on the board
- // using a dice throw
- for (int i =0 ; i<players.size() ; ++i) {
- mret = players.get(i).move (players.get(i).getTile(), _dice());
- if (mret[0]>= board.getN()*board.getM())
- // The first one here is the winner
- return players.get(i);
- }
- return null; // No one finished yet
- }
- /** @} */
-
- /**
- * @brief Main
- * As the requirements of the project suggested.
- * We:
- * * Create a game
- * * Manually create a board
- * * Register 2 players (John Doe for now)
- * * Place a predefined number of snakes, ladders and apples
- * * Deploy the game by calling @ref playOrder() and @ref round()
- * At the end we print the results and exit
- */
- public static void main(String[] args) {
- // Current project requirements
- int lines = 20;
- int columns = 10;
- int numOfSnakes = 3;
- int numOfLadders = 3;
- int numOfApples = 6;
-
- // Print caption
- System.out.println("================== Snake Game ==================");
- System.out.println ("Board: " +lines +"x" +columns +" with " +numOfSnakes +" snakes, "
- +numOfLadders +" ladders and " +numOfApples +" apples.");
- System.out.println ("Players: 2"); // For now
- System.out.println("");
- // Board creation
- Game game = new Game (lines, columns, numOfSnakes, numOfLadders, numOfApples);
- // game.getBoard().createElementBoard(); // Not explicitly required
-
- game.registerPlayer(1, "Player 1"); // Player registration
- game.registerPlayer(2, "Player 2");
- game.playOrder(); // Choose play order
-
- Player winner;
- do // Keep going until someone finishes
- winner = game.round ();
- while (winner == null);
- game.scoreSort (); // sort players based on their score
-
- // Print the results
- System.out.println("***** Game finished *****");
- System.out.println("");
- System.out.println("Rounds: " + game.getRound());
- System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]");
- System.out.println("Score: ");
- for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) {
- // Loop all players
- Player p = game.getPlayers().get(i);
- if (p == winner)
- System.out.println(" *" +p.getName() + ": " + p.getScore() +" points");
- else
- System.out.println(" " +p.getName() + ": " + p.getScore() +" points");
- }
- }
- }
|