A maximum number of rounds per game added for safety
This commit is contained in:
parent
3feb2963b6
commit
f4fe897e17
@ -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,8 +222,13 @@ 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);
|
while (winner == null
|
||||||
game.scoreSort (); // sort players based on their score
|
&& 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 *****");
|
||||||
@ -230,11 +236,12 @@ public class Game {
|
|||||||
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…
x
Reference in New Issue
Block a user