A maximum number of rounds per game added for safety

This commit is contained in:
Christos Houtouridis 2018-11-06 16:47:25 +02:00
parent 3feb2963b6
commit f4fe897e17

View File

@ -32,6 +32,7 @@ public class Game {
/** Constants */
/**@{ */
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 */
@ -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
*/
@ -221,8 +222,13 @@ public class Game {
Player winner;
do // Keep going until someone finishes
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
System.out.println("***** Game finished *****");
@ -230,6 +236,7 @@ public class Game {
System.out.println("Rounds: " + game.getRound());
System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]");
System.out.println("Score: ");
game.scoreSort (); // sort players based on their score
for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) {
// Loop all players
Player p = game.getPlayers().get(i);