From 7b228fed7b480cc5825bb168567a556269f785cb Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 14 Mar 2016 13:32:49 +0100 Subject: Fill the GameState class as requested in week 04 --- src/ch/epfl/xblast/server/GameState.java | 108 +++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 625767a..685bc54 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -2,10 +2,18 @@ package ch.epfl.xblast.server; import java.util.List; import java.util.Optional; +import java.util.Objects; +import java.util.ArrayList; +import java.util.Optional; import ch.epfl.xblast.*; import ch.epfl.cs108.Sq; +/** + * @author Timothée FLOURE (257420) + */ + public final class GameState { + private final int ticks; private final Board board; private final List players; @@ -13,37 +21,123 @@ public final class GameState { private final List>> explosions; private final List> blasts; + /** + * Instanciates a new GameState. + * + * @param ticks thie tick corresponding to the state + * @param board the board + * @param players list of the players + * @param bombs list of the bombs + * @param explosions list of the explosions + * @param blasts + * @throws IllegalArguementException if ticks is negative or players does not contains 4 players. + * @throws NullPointerException if any element except ticks is null. + */ public GameState(int ticks, Board board, List players, List bombs, List>> explosions, List> blasts) { + this.ticks = ArgumentChecker.requireNonNegative(ticks); + this.board = Objects.requireNonNull(board); + if (players.size() == 4) { + this.players = players; + } else { + throw new IllegalArgumentException(); + } + this.bombs = Objects.requireNonNull(bombs); + this.explosions = Objects.requireNonNull(explosions); + this.blasts = Objects.requireNonNull(blasts); } + /** + * Instanciates a new (clean) GameState. + * + * @param board the board + * @param players list of the players + */ public GameState(Board board, List players) { + this(0, + board, + players, + new ArrayList(), + new ArrayList>>(), + new ArrayList>() + ); + } + /** + * @return the tick related to the state. + */ public int ticks() { - return 0; + return ticks; } + /** + * Returns T(the game is over) + * + * @return true if all the players are dead or if the game reachead the time limit + */ public boolean isGameOver() { - return false; + int alivePlayerCount = 0; + for (Player player : players) { + if (player.isAlive()) {alivePlayerCount += 1;} + } + if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) { + return true; + } else { + return false; + } } + /** + * Return the remaining time. + * + * @return the ramaining game time (in seconds) + */ public double remainingTime() { - return 0; + return (double) (Ticks.TOTAL_TICKS - this.ticks) / Ticks.TICKS_PER_SECOND; } + /** + * Returns the winner of the game. + * + * @return the ID of the player who winned the game. + */ public Optional winner() { - return null; + if (players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { + PlayerID winnerId = players.get(0).id(); + return Optional.of(winnerId); + } else { + return Optional.empty(); + } } + /** + * @return the game board + */ public Board board() { - return null; + return this.board; } + /** + * @return the players + */ public List players() { - return null; + return this.players; } + /** + * Returns the alive players. + * + * @return a list of the alive players + */ public List alivePlayers() { - return null; + List alivePlayers = new ArrayList<>(); + + for (Player player : players) { + if (player.isAlive()) { + alivePlayers.add(player); + } + } + + return alivePlayers; } } -- cgit v1.2.3