diff options
author | Timothée Floure | 2016-03-14 13:32:49 +0100 |
---|---|---|
committer | Timothée Floure | 2016-03-14 13:32:49 +0100 |
commit | 7b228fed7b480cc5825bb168567a556269f785cb (patch) | |
tree | 4ecd21e0fb92384d37f365409bbc2f75e858cc2c /src/ch | |
parent | a7fc716ae4123d21254d041797fe114fc44a88e3 (diff) | |
download | xblast-7b228fed7b480cc5825bb168567a556269f785cb.tar.gz |
Fill the GameState class as requested in week 04
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/server/GameState.java | 108 |
1 files changed, 101 insertions, 7 deletions
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; | |||
2 | 2 | ||
3 | import java.util.List; | 3 | import java.util.List; |
4 | import java.util.Optional; | 4 | import java.util.Optional; |
5 | import java.util.Objects; | ||
6 | import java.util.ArrayList; | ||
7 | import java.util.Optional; | ||
5 | import ch.epfl.xblast.*; | 8 | import ch.epfl.xblast.*; |
6 | import ch.epfl.cs108.Sq; | 9 | import ch.epfl.cs108.Sq; |
7 | 10 | ||
11 | /** | ||
12 | * @author Timothée FLOURE (257420) | ||
13 | */ | ||
14 | |||
8 | public final class GameState { | 15 | public final class GameState { |
16 | |||
9 | private final int ticks; | 17 | private final int ticks; |
10 | private final Board board; | 18 | private final Board board; |
11 | private final List<Player> players; | 19 | private final List<Player> players; |
@@ -13,37 +21,123 @@ public final class GameState { | |||
13 | private final List<Sq<Sq<Cell>>> explosions; | 21 | private final List<Sq<Sq<Cell>>> explosions; |
14 | private final List<Sq<Cell>> blasts; | 22 | private final List<Sq<Cell>> blasts; |
15 | 23 | ||
24 | /** | ||
25 | * Instanciates a new GameState. | ||
26 | * | ||
27 | * @param ticks thie tick corresponding to the state | ||
28 | * @param board the board | ||
29 | * @param players list of the players | ||
30 | * @param bombs list of the bombs | ||
31 | * @param explosions list of the explosions | ||
32 | * @param blasts | ||
33 | * @throws IllegalArguementException if ticks is negative or players does not contains 4 players. | ||
34 | * @throws NullPointerException if any element except ticks is null. | ||
35 | */ | ||
16 | public GameState(int ticks, Board board, List<Player> players, List<Bomb> bombs, List<Sq<Sq<Cell>>> explosions, List<Sq<Cell>> blasts) { | 36 | public GameState(int ticks, Board board, List<Player> players, List<Bomb> bombs, List<Sq<Sq<Cell>>> explosions, List<Sq<Cell>> blasts) { |
37 | this.ticks = ArgumentChecker.requireNonNegative(ticks); | ||
38 | this.board = Objects.requireNonNull(board); | ||
39 | if (players.size() == 4) { | ||
40 | this.players = players; | ||
41 | } else { | ||
42 | throw new IllegalArgumentException(); | ||
43 | } | ||
44 | this.bombs = Objects.requireNonNull(bombs); | ||
45 | this.explosions = Objects.requireNonNull(explosions); | ||
46 | this.blasts = Objects.requireNonNull(blasts); | ||
17 | } | 47 | } |
18 | 48 | ||
49 | /** | ||
50 | * Instanciates a new (clean) GameState. | ||
51 | * | ||
52 | * @param board the board | ||
53 | * @param players list of the players | ||
54 | */ | ||
19 | public GameState(Board board, List<Player> players) { | 55 | public GameState(Board board, List<Player> players) { |
56 | this(0, | ||
57 | board, | ||
58 | players, | ||
59 | new ArrayList<Bomb>(), | ||
60 | new ArrayList<Sq<Sq<Cell>>>(), | ||
61 | new ArrayList<Sq<Cell>>() | ||
62 | ); | ||
63 | |||
20 | } | 64 | } |
21 | 65 | ||
66 | /** | ||
67 | * @return the tick related to the state. | ||
68 | */ | ||
22 | public int ticks() { | 69 | public int ticks() { |
23 | return 0; | 70 | return ticks; |
24 | } | 71 | } |
25 | 72 | ||
73 | /** | ||
74 | * Returns T(the game is over) | ||
75 | * | ||
76 | * @return true if all the players are dead or if the game reachead the time limit | ||
77 | */ | ||
26 | public boolean isGameOver() { | 78 | public boolean isGameOver() { |
27 | return false; | 79 | int alivePlayerCount = 0; |
80 | for (Player player : players) { | ||
81 | if (player.isAlive()) {alivePlayerCount += 1;} | ||
82 | } | ||
83 | if (alivePlayerCount == 0 || this.ticks >= Ticks.TOTAL_TICKS) { | ||
84 | return true; | ||
85 | } else { | ||
86 | return false; | ||
87 | } | ||
28 | } | 88 | } |
29 | 89 | ||
90 | /** | ||
91 | * Return the remaining time. | ||
92 | * | ||
93 | * @return the ramaining game time (in seconds) | ||
94 | */ | ||
30 | public double remainingTime() { | 95 | public double remainingTime() { |
31 | return 0; | 96 | return (double) (Ticks.TOTAL_TICKS - this.ticks) / Ticks.TICKS_PER_SECOND; |
32 | } | 97 | } |
33 | 98 | ||
99 | /** | ||
100 | * Returns the winner of the game. | ||
101 | * | ||
102 | * @return the ID of the player who winned the game. | ||
103 | */ | ||
34 | public Optional<PlayerID> winner() { | 104 | public Optional<PlayerID> winner() { |
35 | return null; | 105 | if (players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS) { |
106 | PlayerID winnerId = players.get(0).id(); | ||
107 | return Optional.of(winnerId); | ||
108 | } else { | ||
109 | return Optional.empty(); | ||
110 | } | ||
36 | } | 111 | } |
37 | 112 | ||
113 | /** | ||
114 | * @return the game board | ||
115 | */ | ||
38 | public Board board() { | 116 | public Board board() { |
39 | return null; | 117 | return this.board; |
40 | } | 118 | } |
41 | 119 | ||
120 | /** | ||
121 | * @return the players | ||
122 | */ | ||
42 | public List<Player> players() { | 123 | public List<Player> players() { |
43 | return null; | 124 | return this.players; |
44 | } | 125 | } |
45 | 126 | ||
127 | /** | ||
128 | * Returns the alive players. | ||
129 | * | ||
130 | * @return a list of the alive players | ||
131 | */ | ||
46 | public List<Player> alivePlayers() { | 132 | public List<Player> alivePlayers() { |
47 | return null; | 133 | List<Player> alivePlayers = new ArrayList<>(); |
134 | |||
135 | for (Player player : players) { | ||
136 | if (player.isAlive()) { | ||
137 | alivePlayers.add(player); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return alivePlayers; | ||
48 | } | 142 | } |
49 | } | 143 | } |