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

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

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * A Bomb.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class Bomb {

    private PlayerID ownerId;
    private Cell position;
    private Sq<Integer> fuseLengths;
    private int range;

    /**
     * Generates one arm of explosion.
     */
    private Sq<Sq<Cell>> explosionArmTowards(Direction dir) {
        return Sq
                .constant(Sq.iterate(position, position -> position.neighbor(dir)).limit(range))
                .limit(Ticks.EXPLOSION_TICKS);
    }

    /**
     * Instantiates a new Bomb.
     *
     * @param ownerId     id of the owner of the bomb
     * @param position    position of the bomb
     * @param fuseLengths length of the bomb's fuse
     * @param range       range of the bomb
     * @throws IllegalArgumentException if range is negative or fuseLenghts is empty
     * @throws NullPointerException     if ownerId, position or fuseLengths is null
     */
    public Bomb(PlayerID ownerId, Cell position, Sq<Integer> fuseLengths, int range) {
        this.ownerId = Objects.requireNonNull(ownerId);
        this.position = Objects.requireNonNull(position);

        this.fuseLengths = Objects.requireNonNull(fuseLengths);
        if (this.fuseLengths.isEmpty())
            throw new IllegalArgumentException();

        this.range = ArgumentChecker.requireNonNegative(range);
    }

    /**
     * Instantiates a new Bomb.
     *
     * @param ownerId    id of the owner of the bomb
     * @param position   position of the bomb
     * @param fuseLength length of the bomb's fuse
     * @param range      range of the bomb
     * @throws IllegalArgumentException if range or fuseLengths is negative
     * @throws NullPointerException     if ownerId, position or fuseLengths is null
     */
    public Bomb(PlayerID ownerId, Cell position, int fuseLength, int range) {
        this(ownerId, position, Sq.iterate(fuseLength, fl -> fl - 1), range);
    }

    /**
     * @return the ID of the owner of the bomb
     */
    public PlayerID ownerId() {
        return this.ownerId;
    }

    /**
     * @return the position of the bomb
     */
    public Cell position() {
        return this.position;
    }

    /**
     * @return the length of the fuse
     */
    public Sq<Integer> fuseLengths() {
        return this.fuseLengths;
    }

    /**
     * @return the remaining time before the explosion
     */
    public int fuseLength() {
        return this.fuseLengths.head();
    }

    /**
     * @return the range of the Bomb
     */
    public int range() {
        return this.range;
    }

    /**
     * @return the explosion
     */
    public List<Sq<Sq<Cell>>> explosion() {
        return Stream.of(Direction.values())
                .map(this::explosionArmTowards)
                .collect(Collectors.toList());
    }

}