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