diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ch/epfl/xblast/ArgumentChecker.java | 26 | ||||
-rw-r--r-- | src/ch/epfl/xblast/PlayerID.java | 16 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Bomb.java | 116 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Player.java | 332 |
4 files changed, 490 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java new file mode 100644 index 0000000..311807e --- /dev/null +++ b/src/ch/epfl/xblast/ArgumentChecker.java | |||
@@ -0,0 +1,26 @@ | |||
1 | package ch.epfl.xblast; | ||
2 | |||
3 | /** | ||
4 | * ArgumentChecker. | ||
5 | * | ||
6 | * @author Pacien TRAN-GIRARD (261948) | ||
7 | * @author Timothée FLOURE (257420) | ||
8 | */ | ||
9 | public final class ArgumentChecker { | ||
10 | |||
11 | /** | ||
12 | * Returns the given value if it is non-negative. | ||
13 | * | ||
14 | * @param value the tested value | ||
15 | * @return the given value if non-negative | ||
16 | * @throws IllegalArgumentException if the value is inferior to 0 | ||
17 | */ | ||
18 | public static int requireNonNegative(int value) { | ||
19 | if (value >= 0) { | ||
20 | return value; | ||
21 | } else { | ||
22 | throw new IllegalArgumentException(); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | } | ||
diff --git a/src/ch/epfl/xblast/PlayerID.java b/src/ch/epfl/xblast/PlayerID.java new file mode 100644 index 0000000..c2c5c58 --- /dev/null +++ b/src/ch/epfl/xblast/PlayerID.java | |||
@@ -0,0 +1,16 @@ | |||
1 | package ch.epfl.xblast; | ||
2 | |||
3 | /** | ||
4 | * IDs the 4 different players. | ||
5 | * | ||
6 | * @author Pacien TRAN-GIRARD (261948) | ||
7 | * @author Timothée FLOURE (257420) | ||
8 | */ | ||
9 | public enum PlayerID { | ||
10 | |||
11 | PLAYER_1, | ||
12 | PLAYER_2, | ||
13 | PLAYER_3, | ||
14 | PLAYER_4 | ||
15 | |||
16 | } | ||
diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java new file mode 100644 index 0000000..12e8888 --- /dev/null +++ b/src/ch/epfl/xblast/server/Bomb.java | |||
@@ -0,0 +1,116 @@ | |||
1 | package ch.epfl.xblast.server; | ||
2 | |||
3 | import ch.epfl.cs108.Sq; | ||
4 | import ch.epfl.xblast.ArgumentChecker; | ||
5 | import ch.epfl.xblast.Cell; | ||
6 | import ch.epfl.xblast.Direction; | ||
7 | import ch.epfl.xblast.PlayerID; | ||
8 | |||
9 | import java.util.ArrayList; | ||
10 | import java.util.List; | ||
11 | import java.util.Objects; | ||
12 | |||
13 | /** | ||
14 | * A Bomb. | ||
15 | * | ||
16 | * @author Pacien TRAN-GIRARD (261948) | ||
17 | * @author Timothée FLOURE (257420) | ||
18 | */ | ||
19 | public final class Bomb { | ||
20 | |||
21 | private PlayerID ownerId; | ||
22 | private Cell position; | ||
23 | private Sq<Integer> fuseLengths; | ||
24 | private int range; | ||
25 | |||
26 | /** | ||
27 | * Generates one arm of explosion. | ||
28 | */ | ||
29 | private Sq<Sq<Cell>> explosionArmTowards(Direction dir) { | ||
30 | return Sq | ||
31 | .constant(Sq.iterate(position, position -> position.neighbor(dir)).limit(range)) | ||
32 | .limit(Ticks.EXPLOSION_TICKS); | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Instantiates a new Bomb. | ||
37 | * | ||
38 | * @param ownerId id of the owner of the bomb | ||
39 | * @param position position of the bomb | ||
40 | * @param fuseLengths length of the bomb's fuse | ||
41 | * @param range range of the bomb | ||
42 | * @throws IllegalArgumentException if range is negative or fuseLenghts is empty | ||
43 | * @throws NullPointerException if ownerId, position or fuseLengths is null | ||
44 | */ | ||
45 | public Bomb(PlayerID ownerId, Cell position, Sq<Integer> fuseLengths, int range) { | ||
46 | this.ownerId = Objects.requireNonNull(ownerId); | ||
47 | this.position = Objects.requireNonNull(position); | ||
48 | |||
49 | this.fuseLengths = Objects.requireNonNull(fuseLengths); | ||
50 | if (this.fuseLengths.isEmpty()) | ||
51 | throw new IllegalArgumentException(); | ||
52 | |||
53 | this.range = ArgumentChecker.requireNonNegative(range); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Instantiates a new Bomb. | ||
58 | * | ||
59 | * @param ownerId id of the owner of the bomb | ||
60 | * @param position position of the bomb | ||
61 | * @param fuseLength length of the bomb's fuse | ||
62 | * @param range range of the bomb | ||
63 | * @throws IllegalArgumentException if range or fuseLengths is negative | ||
64 | * @throws NullPointerException if ownerId, position or fuseLengths is null | ||
65 | */ | ||
66 | public Bomb(PlayerID ownerId, Cell position, int fuseLength, int range) { | ||
67 | this(ownerId, position, Sq.iterate(fuseLength, fl -> fl - 1), range); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * @return the ID of the owner of the bomb | ||
72 | */ | ||
73 | public PlayerID ownerId() { | ||
74 | return ownerId; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * @return the position of the bomb | ||
79 | */ | ||
80 | public Cell position() { | ||
81 | return position; | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * @return the length of the fuse | ||
86 | */ | ||
87 | public Sq<Integer> fuseLengths() { | ||
88 | return fuseLengths; | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * @return the remaining time before the explosion | ||
93 | */ | ||
94 | public int fuseLength() { | ||
95 | return fuseLengths.head(); | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * @return the range of the Bomb | ||
100 | */ | ||
101 | public int range() { | ||
102 | return range; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * @return the explosion | ||
107 | */ | ||
108 | public List<Sq<Sq<Cell>>> explosion() { | ||
109 | List<Sq<Sq<Cell>>> explosion = new ArrayList<>(); | ||
110 | for (Direction dir : Direction.values()) { | ||
111 | explosion.add(explosionArmTowards(dir)); | ||
112 | } | ||
113 | return explosion; | ||
114 | } | ||
115 | |||
116 | } | ||
diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java new file mode 100644 index 0000000..7b5ac26 --- /dev/null +++ b/src/ch/epfl/xblast/server/Player.java | |||
@@ -0,0 +1,332 @@ | |||
1 | package ch.epfl.xblast.server; | ||
2 | |||
3 | import ch.epfl.cs108.Sq; | ||
4 | import ch.epfl.xblast.*; | ||
5 | |||
6 | import java.util.Objects; | ||
7 | |||
8 | /** | ||
9 | * A Player. | ||
10 | * | ||
11 | * @author Pacien TRAN-GIRARD (261948) | ||
12 | * @author Timothée FLOURE (257420) | ||
13 | */ | ||
14 | public final class Player { | ||
15 | |||
16 | /** | ||
17 | * The life state of a player. | ||
18 | */ | ||
19 | public static final class LifeState { | ||
20 | |||
21 | /** | ||
22 | * Enum containing all the possible life states. | ||
23 | */ | ||
24 | public enum State { | ||
25 | INVULNERABLE, | ||
26 | VULNERABLE, | ||
27 | DYING, | ||
28 | DEAD | ||
29 | } | ||
30 | |||
31 | private final int lives; | ||
32 | private final State state; | ||
33 | |||
34 | /** | ||
35 | * Instantiates a new LifeSate. | ||
36 | * | ||
37 | * @param lives the number of lives | ||
38 | * @param state the state | ||
39 | * @throws IllegalArgumentException if lives is negative | ||
40 | */ | ||
41 | public LifeState(int lives, State state) { | ||
42 | this.lives = ArgumentChecker.requireNonNegative(lives); | ||
43 | this.state = state; | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * @return the number of lives | ||
48 | */ | ||
49 | public int lives() { | ||
50 | return lives; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * @return the state | ||
55 | */ | ||
56 | public State state() { | ||
57 | return state; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * @return true if the actual state allow to move | ||
62 | */ | ||
63 | public boolean canMove() { | ||
64 | return (state() == State.INVULNERABLE || state() == State.VULNERABLE); | ||
65 | } | ||
66 | |||
67 | } | ||
68 | |||
69 | /** | ||
70 | * The "directed" position of a player. | ||
71 | */ | ||
72 | public static final class DirectedPosition { | ||
73 | |||
74 | private final SubCell position; | ||
75 | private final Direction direction; | ||
76 | |||
77 | /** | ||