Browse Source

A maximum number of rounds per game added for safety

tags/v1.0
Christos Houtouridis 5 years ago
parent
commit
f4fe897e17
1 changed files with 12 additions and 5 deletions
  1. +12
    -5
      src/SnakePkg/Game.java

+ 12
- 5
src/SnakePkg/Game.java View File

@@ -32,6 +32,7 @@ public class Game {
/** Constants */ /** Constants */
/**@{ */ /**@{ */
static final int MAX_PLAYERS = 4; /**< The maximum number of allowed players in the game */ static final int MAX_PLAYERS = 4; /**< The maximum number of allowed players in the game */
static final int MAX_GAME_ROUNDS = 10000; /**< the maximum allowed round of the game */
/**@} */ /**@} */
/** Private data members */ /** Private data members */
@@ -163,7 +164,7 @@ public class Game {
} }
/** /**
* A game round. In each round every player plays when is its turn
* A game round. In each round every player plays when is his turn
* *
* @return The winner if we have one, or null * @return The winner if we have one, or null
*/ */
@@ -221,20 +222,26 @@ public class Game {
Player winner; Player winner;
do // Keep going until someone finishes do // Keep going until someone finishes
winner = game.round (); winner = game.round ();
while (winner == null);
game.scoreSort (); // sort players based on their score
while (winner == null
&& game.getRound() < MAX_GAME_ROUNDS);
if (game.getRound() == MAX_GAME_ROUNDS) {
// Check if we finished
System.out.println("Game did not finished in " + MAX_GAME_ROUNDS + " rounds. Abort.");
return;
}
// Print the results // Print the results
System.out.println("***** Game finished *****"); System.out.println("***** Game finished *****");
System.out.println(""); System.out.println("");
System.out.println("Rounds: " + game.getRound()); System.out.println("Rounds: " + game.getRound());
System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]"); System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]");
System.out.println("Score: "); System.out.println("Score: ");
game.scoreSort (); // sort players based on their score
for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) { for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) {
// Loop all players // Loop all players
Player p = game.getPlayers().get(i); Player p = game.getPlayers().get(i);
if (p == winner) if (p == winner)
System.out.println(" *" +p.getName() + ": " + p.getScore() +" points");
System.out.println(" * " +p.getName() + ": " + p.getScore() +" points");
else else
System.out.println(" " +p.getName() + ": " + p.getScore() +" points"); System.out.println(" " +p.getName() + ": " + p.getScore() +" points");
} }


Loading…
Cancel
Save