Print-out rework

This commit is contained in:
Christos Houtouridis 2018-11-06 16:14:03 +02:00
parent 8c19955045
commit 8b60096e7b

View File

@ -91,7 +91,6 @@ public class Game {
// delegate constructors // delegate constructors
board = new Board (N, M, numOfSnakes, numOfLadders, numOfApples); board = new Board (N, M, numOfSnakes, numOfLadders, numOfApples);
players = new ArrayList<>(); players = new ArrayList<>();
board.createElementBoard(); //Not critical, but placed here as project requirement
} }
/** @} */ /** @} */
@ -149,7 +148,18 @@ public class Game {
players.get(i).setTurn(d); players.get(i).setTurn(d);
} }
// Sort players vector // Sort players vector
players.sort((p1, p2) -> Integer.compare(p1.getTurn(), p2.getTurn())); 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())
);
} }
/** /**
@ -185,7 +195,22 @@ public class Game {
* At the end we print the results and exit * At the end we print the results and exit
*/ */
public static void main(String[] args) { public static void main(String[] args) {
Game game = new Game(20, 10, 3, 3, 6); // Board creation // 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(1, "Player 1"); // Player registration
game.registerPlayer(2, "Player 2"); game.registerPlayer(2, "Player 2");
@ -195,16 +220,21 @@ public class Game {
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
// Print the results // Print the results
System.out.println("Game finished."); System.out.println("***** Game finished *****");
System.out.println("Rounds played: " + game.getRound()); System.out.println("");
System.out.println("Winner: " + winner.getName() + ". Score: " + winner.getScore()); System.out.println("Rounds: " + game.getRound());
for (int i=0 ; i<game.getPlayers().size() ; ++i) { System.out.println("Winner: " + winner.getName() + " [" + winner.getScore() +" points]");
// Loop all the non-winning players System.out.println("Score: ");
for (int i=game.getPlayers().size()-1 ; i>=0 ; --i) {
// 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() + ". Score: " + p.getScore()); System.out.println(" *" +p.getName() + ": " + p.getScore() +" points");
else
System.out.println(" " +p.getName() + ": " + p.getScore() +" points");
} }
} }
} }