Christos Houtouridis 6 лет назад
Родитель
Сommit
8b60096e7b
1 измененных файлов: 42 добавлений и 12 удалений
  1. +42
    -12
      src/SnakePkg/Game.java

+ 42
- 12
src/SnakePkg/Game.java Просмотреть файл

@@ -91,7 +91,6 @@ public class Game {
// delegate constructors
board = new Board (N, M, numOfSnakes, numOfLadders, numOfApples);
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);
}
// 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())
);
}
/**
@@ -184,8 +194,23 @@ public class Game {
* * 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) {
Game game = new Game(20, 10, 3, 3, 6); // Board creation
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");
@@ -195,16 +220,21 @@ public class Game {
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("Rounds played: " + game.getRound());
System.out.println("Winner: " + winner.getName() + ". Score: " + winner.getScore());
for (int i=0 ; i<game.getPlayers().size() ; ++i) {
// Loop all the non-winning players
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() + ". Score: " + p.getScore());
if (p == winner)
System.out.println(" *" +p.getName() + ": " + p.getScore() +" points");
else
System.out.println(" " +p.getName() + ": " + p.getScore() +" points");
}
}
}

Загрузка…
Отмена
Сохранить