aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/server/GameState.java
blob: 6e7f8c48ebb33b1295ad63adb69406f6902298b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package ch.epfl.xblast.server;

import ch.epfl.cs108.Sq;
import ch.epfl.xblast.*;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * GameState representing the current game state.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class GameState {

    /**
     * The list of player priority order permutations.
     */
    private static final List<List<PlayerID>> PLAYER_PRIORITY_ORDERS = GameState.buildPlayerPriorityOrderList();

    /**
     * Builds and returns the player priority order permutations.
     *
     * @return the list of player priority orders
     */
    private static List<List<PlayerID>> buildPlayerPriorityOrderList() {
        return Lists.permutations(Arrays.asList(PlayerID.values()));
    }

    /**
     * Filters the given list of players and returns the lively ones.
     *
     * @param players a list of players
     * @return the list of alive players
     */
    private static List<Player> alivePlayers(List<Player> players) {
        return players.stream()
                .filter(Player::isAlive)
                .collect(Collectors.toList());
    }

    /**
     * Maps the given bombs to their position.
     *
     * @param bombs a list of bombs
     * @return a map of positions and their bombs
     */
    private static Map<Cell, Bomb> bombedCells(List<Bomb> bombs) {
        return bombs.stream()
                .collect(Collectors.toMap(Bomb::position, Function.identity()));
    }

    /**
     * Returns a set of cells that contains at least one blast in the given blasts sequences.
     *
     * @param blasts the list of blast sequences
     * @return the set of blasted cells
     */
    private static Set<Cell> blastedCells(List<Sq<Cell>> blasts) {
        return blasts.stream()
                .map(Sq::head)
                .collect(Collectors.toSet());
    }

    /**
     * Computes the next state of a blast.
     *
     * @param blasts0     existing particles
     * @param board0      the game's board
     * @param explosions0 active explosions
     * @return the position of the explosion's particles for the next state.
     */
    private static List<Sq<Cell>> nextBlasts(List<Sq<Cell>> blasts0, Board board0, List<Sq<Sq<Cell>>> explosions0) {
        return Stream.concat(
                blasts0.stream()
                        .filter(blastSeq -> !blastSeq.tail().isEmpty())
                        .filter(blastSeq -> board0.blockAt(blastSeq.head()).isFree())
                        .map(Sq::tail),
                explosions0.stream()
                        .map(Sq::head)
        ).collect(Collectors.toList());
    }

    /**
     * Computes and returns the next board state of the given board according to the given events.
     *
     * @param board0          the previous board
     * @param consumedBonuses the set of consumed bonuses
     * @param blastedCells1   the set of newly blasted cells
     * @return the next board
     */
    private static Board nextBoard(Board board0, Set<Cell> consumedBonuses, Set<Cell> blastedCells1) {
        return null; // TODO
    }

    /**
     * Computes and returns the next player list given the current one and the given events and states.
     *
     * @param players0          the previous player list
     * @param playerBonuses     the map of player bonuses
     * @param bombedCells1      the set of newly bombed cells
     * @param board1            the newly updated board
     * @param blastedCells1     the set of newly blasted cells
     * @param speedChangeEvents the speed change events
     * @return the next player list
     */
    private static List<Player> nextPlayers(
            List<Player> players0,
            Map<PlayerID, Bonus> playerBonuses,
            Set<Cell> bombedCells1,
            Board board1,
            Set<Cell> blastedCells1,
            Map<PlayerID, Optional<Direction>> speedChangeEvents) {
        return null; // TODO
    }

    /**
     * Computes and returns the next state of the given explosion list.
     *
     * @param explosions0 the previous explosion state
     * @return the next explosion state
     */
    private static List<Sq<Sq<Cell>>> nextExplosions(List<Sq<Sq<Cell>>> explosions0) {
        return null; // TODO
    }

    /**
     * Computes and returns the list of newly dropped bombs by players according to the given player states and events.
     *
     * @param players0       the previous player states
     * @param bombDropEvents the bomb drop events
     * @param bombs0         the previous bomb state
     * @return the newly dropped bombs
     */
    private static List<Bomb> newlyDroppedBombs(
            List<Player> players0,
            Set<PlayerID> bombDropEvents,
            List<Bomb> bombs0) {
        return null; // TODO
    }

    private final int ticks;
    private final Board board;
    private final List<Player> players;
    private final List<Bomb> bombs;
    private final List<Sq<Sq<Cell>>> explosions;
    private final List<Sq<Cell>> blasts;

    /**
     * Instantiates a new GameState.
     *
     * @param ticks      the tick corresponding to the state
     * @param board      the game's board
     * @param players    list of the players
     * @param bombs      list of the bombs
     * @param explosions list of the explosions
     * @param blasts     list of particle blasts
     * @throws IllegalArgumentException 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<Player> players, List<Bomb> bombs, List<Sq<Sq<Cell>>> explosions, List<Sq<Cell>> blasts) {
        this.ticks = ArgumentChecker.requireNonNegative(ticks);
        this.board = Objects.requireNonNull(board);

        if (players.size() != 4) throw new IllegalArgumentException();
        this.players = players;

        this.bombs = Objects.requireNonNull(bombs);
        this.explosions = Objects.requireNonNull(explosions);
        this.blasts = Objects.requireNonNull(blasts);
    }

    /**
     * Instantiates a new (clean) GameState.
     *
     * @param board   the board
     * @param players list of the players
     */
    public GameState(Board board, List<Player> players) {
        this(0, board, players, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    }

    /**
     * @return the tick related to the state.
     */
    public int ticks() {
        return this.ticks;
    }

    /**
     * Returns T(the game is over).
     *
     * @return true if all the players are dead or if the game reached the time limit
     */
    public boolean isGameOver() {
        return this.alivePlayers().isEmpty() || this.ticks >= Ticks.TOTAL_TICKS;
    }

    /**
     * Return the remaining time.
     *
     * @return the remaining game time (in seconds)
     */
    public double remainingTime() {
        return ((double) (Ticks.TOTAL_TICKS - this.ticks)) / Ticks.TICKS_PER_SECOND;
    }

    /**
     * Returns the winner of the game.
     *
     * @return the ID of the player who won the game.
     */
    public Optional<PlayerID> winner() {
        if (this.players.size() == 1 && this.ticks < Ticks.TOTAL_TICKS)
            return Optional.of(this.players.get(0).id());
        else
            return Optional.empty();
    }

    /**
     * @return the game board
     */
    public Board board() {
        return this.board;
    }

    /**
     * @return the players
     */
    public List<Player> players() {
        return this.players;
    }

    /**
     * Returns the alive players.
     *
     * @return a list of the alive players
     */
    public List<Player> alivePlayers() {
        return GameState.alivePlayers(this.players);
    }

    /**
     * Returns a mapping of Cells and their Bomb.
     *
     * @return the map of bombs
     */
    public Map<Cell, Bomb> bombedCells() {
        return GameState.bombedCells(this.bombs);
    }

    /**
     * Returns the set of cells which contains at least one explosion particle.
     *
     * @return the set of blasted cells
     */
    public Set<Cell> blastedCells() {
        return GameState.blastedCells(this.blasts);
    }

    /**
     * Computes and returns the game state for the next tick, given the current state and the given events.
     *
     * @param speedChangeEvents the speed change events
     * @param bombDropEvents    the bomb drop events
     * @return the next game state
     */
    public GameState next(Map<PlayerID, Optional<Direction>> speedChangeEvents, Set<PlayerID> bombDropEvents) {
        return null; // TODO
    }

    /**
     * Returns the current player priority order permutation.
     *
     * @return the player priority order
     */
    private List<PlayerID> currentPlayerPriorityOrder() {
        int priorityIndex = this.ticks % GameState.PLAYER_PRIORITY_ORDERS.size();
        return GameState.PLAYER_PRIORITY_ORDERS.get(priorityIndex);
    }

    /**
     * Resolves a conflict according to the current priority order.
     *
     * @param claimants the list of claimants
     * @return the highest priority player
     */
    private PlayerID resolveConflict(List<PlayerID> claimants) {
        return this.currentPlayerPriorityOrder()
                .stream()
                .filter(claimants::contains)
                .findFirst()
                .get();
    }

}