aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/server/GameState.java
blob: ef792b627a02f50b3ee38cc250733a4c3cc4f5a4 (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
package ch.epfl.xblast.server;

import ch.epfl.cs108.Sq;
import ch.epfl.xblast.ArgumentChecker;
import ch.epfl.xblast.Cell;
import ch.epfl.xblast.PlayerID;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
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 {

    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;

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

    /**
     * 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 this.players
                .stream()
                .filter(Player::isAlive)
                .collect(Collectors.toList());
    }

}