From 2165f60e83fa4e36183c2821955dbd77d86af3f0 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 7 Mar 2016 15:04:33 +0100 Subject: Basics of the third week (Player & Bomb) --- src/ch/epfl/xblast/ArgumentChecker.java | 23 +++ src/ch/epfl/xblast/PlayerID.java | 13 ++ src/ch/epfl/xblast/server/Bomb.java | 116 +++++++++++++ src/ch/epfl/xblast/server/Player.java | 291 ++++++++++++++++++++++++++++++++ 4 files changed, 443 insertions(+) create mode 100644 src/ch/epfl/xblast/ArgumentChecker.java create mode 100644 src/ch/epfl/xblast/PlayerID.java create mode 100644 src/ch/epfl/xblast/server/Bomb.java create mode 100644 src/ch/epfl/xblast/server/Player.java (limited to 'src/ch/epfl') 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 @@ +package ch.epfl.xblast; + +/** + * ArgumentChecker. + * + * @author Timothée FLOURE (257420) + */ +public final class ArgumentChecker { + /** + * Return the given value if it is non-negative + * + * @param value the tested value + * @throws IllegalArgumentException if the value is inferior to 0 + * @return the given value if non-negative + */ + public static int requireNonNegative(int value) { + if (value >= 0) { + return value; + } else { + throw new IllegalArgumentException(); + } + } +} 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 @@ +package ch.epfl.xblast; + +/** + * IDs the 4 different players. + * + * @author Timothée FLOURE (257420) + */ +public enum PlayerID { + PLAYER_1, + PLAYER_2, + PLAYER_3, + PLAYER_4 +} 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 @@ +package ch.epfl.xblast.server; + +import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.Cell; +import ch.epfl.cs108.Sq; +import ch.epfl.xblast.ArgumentChecker; +import ch.epfl.xblast.Direction; + +import java.util.Objects; +import java.util.List; +import java.util.ArrayList; + +/** + * @author Timothée FLOURE (257420) + */ +public final class Bomb { + private PlayerID ownerId; + private Cell position; + private Sq fuseLengths; + private int range; + + /** + * Generate one arm of explosion + */ + private Sq> explosionArmTowards(Direction dir) { + return Sq.constant( Sq.iterate(position, position -> position.neighbor(dir)).limit(range)).limit(Ticks.EXPLOSION_TICKS); + } + + /** + * Instanciates 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 IllegalArguementException if range is negative or fuseLenghts is empty + * @throws NullPointerException if ownerId, position or fuseLengths is null + */ + public Bomb(PlayerID ownerId, Cell position, Sq fuseLengths, int range) { + this.ownerId = Objects.requireNonNull(ownerId); + this.position = Objects.requireNonNull(position); + if (fuseLengths.isEmpty()) { + throw new IllegalArgumentException(); + } else { + this.fuseLengths = Objects.requireNonNull(fuseLengths); + } + this.range = ArgumentChecker.requireNonNegative(range); + } + + /** + * Instanciates 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 IllegalArguementException 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 = Objects.requireNonNull(ownerId); + this.position = Objects.requireNonNull(position); + if (fuseLength == 0) { + throw new IllegalArgumentException(); + } else { + fuseLengths = Sq.iterate(fuseLength, fuseLengths -> fuseLength - 1 ); + } + this.range = ArgumentChecker.requireNonNegative(range); + } + + /** + * @return the ID of the owner of the bomb + */ + public PlayerID ownerId() { + return ownerId; + } + + /** + * @return the position of the bomb + */ + public Cell position() { + return position; + } + + /** + * @return the length of the fuse + */ + public Sq fuseLengths() { + return fuseLengths; + } + + /** + * @return the remaining time before the explosion + */ + public int fuseLength() { + return fuseLengths.head(); + } + + /** + * @return the range of the Bomb + */ + public int range() { + return range; + } + + /** + * @return the explosion + */ + public List>> explosion() { + List>> explosion = new ArrayList<>(); + for (Direction dir : Direction.values()) { + explosion.add(explosionArmTowards(dir)); + } + return explosion; + } +} 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 @@ +package ch.epfl.xblast.server; + +import ch.epfl.xblast.ArgumentChecker; +import ch.epfl.xblast.Direction; +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.SubCell; +import ch.epfl.xblast.PlayerID; +import ch.epfl.cs108.Sq; + +import java.util.Objects; + +/** + * Player. + * + * @author Timothée FLOURE (257420) + */ +public final class Player { + /** + * The life state of a player. + */ + public static final class LifeState { + /** + * Enum containing all the possible states. + */ + public enum State { + INVULNERABLE, + VULNERABLE, + DYING, + DEAD + } + + private final int lives; + private final State state; + + /** + * Instanciates a new LifeSate. + * + * @param lives the number of lifes + * @param state the state + * @throws IllegalArgumentException if lives is negative + */ + public LifeState(int lives, State state) { + this.lives = ArgumentChecker.requireNonNegative(lives); + this.state = state; + } + + /** + * @return the number of lives + */ + public int lives() { + return lives; + } + + /** + * @return the state + */ + public State state() { + return state; + } + + /** + * @return true if the actual state allow to move + */ + public boolean canMove() { + return (state() == State.INVULNERABLE || state() == State.VULNERABLE); + } + } + + /** + * The "directed" position of a player. + */ + public static final class DirectedPosition { + private final SubCell position; + private final Direction direction; + + /** + * @return an infinite sequence of directed positions corresponding to a stopped player. + */ + public static Sq stopped(DirectedPosition p) { + return Sq.constant(p); + } + + /** + * @return an infinite sequence of directed position corresponding to a moving player. + */ + public static Sq moving(DirectedPosition p) { + return Sq.iterate(p, x -> x.withPosition(x.position().neighbor(x.direction()))); + } + + /** + * Instanciates a new DirectedPos + * + * @param position the position of the player + * @param direction the direction of the player + * @throws IllegalArgumentException if position or direction is null + */ + public DirectedPosition(SubCell position, Direction direction) { + this.position = Objects.requireNonNull(position); + this.direction = Objects.requireNonNull(direction); + } + + /** + * @return the position + */ + public SubCell position() { + return position; + } + + /** + * @return a new directed position with the given position and the previous direction + */ + public DirectedPosition withPosition(SubCell newPosition) { + return new DirectedPosition(newPosition, direction); + } + + /** + * @return the direction + */ + public Direction direction() { + return direction; + } + + /** + * @return a new directed position with the previous position and the given direction + */ + public DirectedPosition withDirection(Direction newDirection) { + return new DirectedPosition(position, newDirection); + } + } + + private final PlayerID id; + private final Sq lifeStates; + private final Sq directedPos; + private final int maxBombs; + private final int bombRange; + + /** + * Instanciates a new Player. + * + * @param id + * @param lifeStates + * @param directedPos + * @param maxBombs + * @param bombRange + * @throws IllegalArfuementException + * @throws NullPointerExeption + */ + public Player(PlayerID id, Sq lifeStates, Sq directedPos, int maxBombs, int bombRange) { + this.id = Objects.requireNonNull(id); + this.lifeStates = Objects.requireNonNull(lifeStates); + this.directedPos = Objects.requireNonNull(directedPos); + this.maxBombs = ArgumentChecker.requireNonNegative(maxBombs); + this.bombRange = ArgumentChecker.requireNonNegative(bombRange); + } + + /** + * Instanciates a new Player. + * + * @param id + * @param lives + * @param position + * @param maxBombs + * @param bombRange + * @throws IllegalArgumentException + * @throws NullPointerExeption + */ + public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { + this.id = Objects.requireNonNull(id); + DirectedPosition newDirectedPos = new DirectedPosition(SubCell.centralSubCellOf(Objects.requireNonNull(position)), Direction.S); + this.directedPos = DirectedPosition.stopped(newDirectedPos); + LifeState invulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.INVULNERABLE); + LifeState vulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.VULNERABLE); + this.lifeStates = Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); + this.maxBombs = ArgumentChecker.requireNonNegative(maxBombs); + this.bombRange = ArgumentChecker.requireNonNegative(bombRange); + } + + /** + * @return the player's ID + */ + public PlayerID id() { + return id(); + } + + /** + * @return the player's life states + */ + public Sq lifeStates() { + return lifeStates; + } + + /** + * @return the sequence related to the player's next life + */ + public Sq statesForNextLife() { + LifeState dying = new LifeState(lives(), LifeState.State.DYING); + Sq nextLifeState = Sq.repeat(Ticks.PLAYER_DYING_TICKS, dying); + + int newLives = lives() - 1; + + if (newLives >= 0) { + LifeState dead = new LifeState(newLives, LifeState.State.DEAD); + nextLifeState = nextLifeState.concat(Sq.constant(dead)); + } else { + LifeState invulnerable = new LifeState(newLives, LifeState.State.INVULNERABLE); + LifeState vulnerable = new LifeState(newLives, LifeState.State.VULNERABLE); + + nextLifeState = nextLifeState.concat(Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerable)); + nextLifeState = nextLifeState.concat(Sq.constant(vulnerable)); + + } + return nextLifeState; + } + + /** + * @return the current life state of the player + */ + public LifeState lifeState() { + return lifeStates.head(); + } + + /** + * @return the current number of lifes of the player + */ + public int lives() { + return lifeStates.head().lives(); + } + + /** + * @return true is the player has more than 0 lives + */ + public boolean isAlive() { + return lives() >= 0; + } + + /** + * @return the directed position sequence of the player + */ + public Sq directedPositions() { + return directedPos; + } + + /** + * @return the position of the player + */ + public SubCell position() { + return directedPos.head().position(); + } + + /** + * @return the current direction of the player + */ + public Direction direction() { + return directedPos.head().direction(); + } + + /** + * @return the maximum number of bombs that the player can use + */ + public int maxBombs() { + return maxBombs; + } + + /** + * @return a new Player with the new maximum of bombs + */ + public Player withMaxBombs(int newMaxBombs) { + return new Player(id, lifeStates, directedPos, newMaxBombs, bombRange); + } + + /** + * @return the range of the player's bomb + */ + public int bombRange() { + return bombRange; + } + + /** + * @return a new Player with the new bomb range + */ + public Player withBombRange(int newBombRange) { + return new Player(id, lifeStates, directedPos, maxBombs, newBombRange); + } + + /** + * @return a new bomb posed by the Player + */ + public Bomb newBomb() { + return new Bomb(id, position().containingCell(), Ticks.BOMB_FUSE_TICKS, bombRange); + } +} -- cgit v1.2.3 From 0b7d819060a6e954d156e155c5329b2c3577f8d3 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 7 Mar 2016 15:27:00 +0100 Subject: Fix the Bomb & Player secondary builder --- src/ch/epfl/xblast/server/Bomb.java | 4 +--- src/ch/epfl/xblast/server/Player.java | 7 ++++--- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java index 3f49f64..38bd4c5 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -58,14 +58,12 @@ public final class Bomb { * @throws NullPointerException if ownerId, position or fuseLengths is null */ public Bomb(PlayerID ownerId, Cell position, int fuseLength, int range) { - this.ownerId = Objects.requireNonNull(ownerId); - this.position = Objects.requireNonNull(position); if (fuseLength == 0) { throw new IllegalArgumentException(); } else { fuseLengths = Sq.iterate(fuseLength, fuseLengths -> fuseLength - 1 ); } - this.range = ArgumentChecker.requireNonNegative(range); + this.Bomb(ownerId, position, fuseLengths, range); } /** diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index b3ae851..57a280a 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -166,13 +166,14 @@ public final class Player { */ public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { this.id = Objects.requireNonNull(id); - DirectedPosition newDirectedPos = new DirectedPosition(SubCell.centralSubCellOf(Objects.requireNonNull(position)), Direction.S); - this.directedPos = DirectedPosition.stopped(newDirectedPos); + DirectedPosition newDirectedPosition = new DirectedPosition(SubCell.centralSubCellOf(Objects.requireNonNull(position)), Direction.S); + DirectedPosition directedPositionSequence = DirectedPosition.stopped(newDirectedPos); LifeState invulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.INVULNERABLE); LifeState vulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.VULNERABLE); - this.lifeStates = Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); + Sq lifeStateSequence = Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); this.maxBombs = ArgumentChecker.requireNonNegative(maxBombs); this.bombRange = ArgumentChecker.requireNonNegative(bombRange); + this.Player(id, lifeStateSequence,directedPositionSequence, maxBombs, bombRange); } /** -- cgit v1.2.3 From 1998636c6b0a3177da22760ef9ae19897f804bba Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 7 Mar 2016 15:29:35 +0100 Subject: Remove useless lines from the secondary builder of the Player class --- src/ch/epfl/xblast/server/Player.java | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index 57a280a..668b948 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -165,14 +165,11 @@ public final class Player { * @throws NullPointerExeption */ public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { - this.id = Objects.requireNonNull(id); DirectedPosition newDirectedPosition = new DirectedPosition(SubCell.centralSubCellOf(Objects.requireNonNull(position)), Direction.S); DirectedPosition directedPositionSequence = DirectedPosition.stopped(newDirectedPos); LifeState invulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.INVULNERABLE); LifeState vulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.VULNERABLE); Sq lifeStateSequence = Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); - this.maxBombs = ArgumentChecker.requireNonNegative(maxBombs); - this.bombRange = ArgumentChecker.requireNonNegative(bombRange); this.Player(id, lifeStateSequence,directedPositionSequence, maxBombs, bombRange); } -- cgit v1.2.3 From be399f170a086000ffa774c9dcfb9a5eb6233ab2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 7 Mar 2016 16:28:51 +0100 Subject: Fix Bomb secondary constructor for decreasing fuse lengths sequences --- src/ch/epfl/xblast/server/Bomb.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java index 38bd4c5..889dde0 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.ArrayList; /** + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public final class Bomb { @@ -58,12 +59,7 @@ public final class Bomb { * @throws NullPointerException if ownerId, position or fuseLengths is null */ public Bomb(PlayerID ownerId, Cell position, int fuseLength, int range) { - if (fuseLength == 0) { - throw new IllegalArgumentException(); - } else { - fuseLengths = Sq.iterate(fuseLength, fuseLengths -> fuseLength - 1 ); - } - this.Bomb(ownerId, position, fuseLengths, range); + this(ownerId, position, Sq.iterate(fuseLength, fl -> fl - 1 ), range); } /** -- cgit v1.2.3 From 2c5bd508c1573a3bbc334dc121e3c4fca322b84c Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 7 Mar 2016 16:40:53 +0100 Subject: Fix default life-state and directed position sequences Player constructor --- src/ch/epfl/xblast/server/Player.java | 38 +++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index 668b948..8ab4cc6 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -12,6 +12,7 @@ import java.util.Objects; /** * Player. * + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public final class Player { @@ -128,12 +129,36 @@ public final class Player { } } + private static final Direction DEFAULT_DIRECTION = Direction.S; + private final PlayerID id; private final Sq lifeStates; private final Sq directedPos; private final int maxBombs; private final int bombRange; + private static Sq buildDefaultLifeStateSequence(int lives) { + LifeState invulnerability = new LifeState( + ArgumentChecker.requireNonNegative(lives), + LifeState.State.INVULNERABLE + ); + LifeState vulnerability = new LifeState( + ArgumentChecker.requireNonNegative(lives), + LifeState.State.VULNERABLE + ); + + return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); + } + + private static Sq buildDefaultDirectedPositionSequence(Cell pos) { + DirectedPosition dp = new DirectedPosition( + SubCell.centralSubCellOf(Objects.requireNonNull(pos)), + Player.DEFAULT_DIRECTION + ); + + return DirectedPosition.stopped(dp); + } + /** * Instanciates a new Player. * @@ -165,12 +190,13 @@ public final class Player { * @throws NullPointerExeption */ public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { - DirectedPosition newDirectedPosition = new DirectedPosition(SubCell.centralSubCellOf(Objects.requireNonNull(position)), Direction.S); - DirectedPosition directedPositionSequence = DirectedPosition.stopped(newDirectedPos); - LifeState invulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.INVULNERABLE); - LifeState vulnerability = new LifeState(ArgumentChecker.requireNonNegative(lives), LifeState.State.VULNERABLE); - Sq lifeStateSequence = Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); - this.Player(id, lifeStateSequence,directedPositionSequence, maxBombs, bombRange); + this( + id, + Player.buildDefaultLifeStateSequence(lives), + Player.buildDefaultDirectedPositionSequence(position), + maxBombs, + bombRange + ); } /** -- cgit v1.2.3 From 9eab4086c339af39c36b58954f4a99f22f29f4de Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 7 Mar 2016 16:43:57 +0100 Subject: Fix Player id getter failure --- src/ch/epfl/xblast/server/Player.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index 8ab4cc6..59fb962 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -203,7 +203,7 @@ public final class Player { * @return the player's ID */ public PlayerID id() { - return id(); + return id; } /** -- cgit v1.2.3 From bccd4e10938c55f9b30283338120689ea2e1738e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 7 Mar 2016 16:56:11 +0100 Subject: Elaborate documentation (fix missing doc and typos) --- src/ch/epfl/xblast/ArgumentChecker.java | 3 +- src/ch/epfl/xblast/PlayerID.java | 1 + src/ch/epfl/xblast/server/Bomb.java | 14 ++++---- src/ch/epfl/xblast/server/Player.java | 57 +++++++++++++++++++++------------ 4 files changed, 47 insertions(+), 28 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java index 8e7ba76..a567fbb 100644 --- a/src/ch/epfl/xblast/ArgumentChecker.java +++ b/src/ch/epfl/xblast/ArgumentChecker.java @@ -3,11 +3,12 @@ package ch.epfl.xblast; /** * ArgumentChecker. * + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public final class ArgumentChecker { /** - * Return the given value if it is non-negative + * Returns the given value if it is non-negative. * * @param value the tested value * @throws IllegalArgumentException if the value is inferior to 0 diff --git a/src/ch/epfl/xblast/PlayerID.java b/src/ch/epfl/xblast/PlayerID.java index 05a280b..bb77405 100644 --- a/src/ch/epfl/xblast/PlayerID.java +++ b/src/ch/epfl/xblast/PlayerID.java @@ -3,6 +3,7 @@ package ch.epfl.xblast; /** * IDs the 4 different players. * + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public enum PlayerID { diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java index 889dde0..659adb6 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -11,6 +11,8 @@ import java.util.List; import java.util.ArrayList; /** + * A Bomb. + * * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ @@ -21,20 +23,20 @@ public final class Bomb { private int range; /** - * Generate one arm of explosion + * Generates one arm of explosion. */ private Sq> explosionArmTowards(Direction dir) { return Sq.constant( Sq.iterate(position, position -> position.neighbor(dir)).limit(range)).limit(Ticks.EXPLOSION_TICKS); } /** - * Instanciates a new Bomb. + * 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 IllegalArguementException if range is negative or fuseLenghts is empty + * @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 fuseLengths, int range) { @@ -49,13 +51,13 @@ public final class Bomb { } /** - * Instanciates a new Bomb. + * 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 fuseLength length of the bomb's fuse * @param range range of the bomb - * @throws IllegalArguementException if range or fuseLengths is negative + * @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) { diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index 59fb962..0548125 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -10,7 +10,7 @@ import ch.epfl.cs108.Sq; import java.util.Objects; /** - * Player. + * A Player. * * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) @@ -21,7 +21,7 @@ public final class Player { */ public static final class LifeState { /** - * Enum containing all the possible states. + * Enum containing all the possible life states. */ public enum State { INVULNERABLE, @@ -34,9 +34,9 @@ public final class Player { private final State state; /** - * Instanciates a new LifeSate. + * Instantiates a new LifeSate. * - * @param lives the number of lifes + * @param lives the number of lives * @param state the state * @throws IllegalArgumentException if lives is negative */ @@ -89,7 +89,7 @@ public final class Player { } /** - * Instanciates a new DirectedPos + * Instantiates a new DirectedPos * * @param position the position of the player * @param direction the direction of the player @@ -129,6 +129,9 @@ public final class Player { } } + /** + * The default Direction of a new Player. + */ private static final Direction DEFAULT_DIRECTION = Direction.S; private final PlayerID id; @@ -137,6 +140,12 @@ public final class Player { private final int maxBombs; private final int bombRange; + /** + * Builds a default LifeState sequence with the given number of lives. + * + * @param lives number of lives of the desired sequence + * @return the sequence + */ private static Sq buildDefaultLifeStateSequence(int lives) { LifeState invulnerability = new LifeState( ArgumentChecker.requireNonNegative(lives), @@ -150,6 +159,12 @@ public final class Player { return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); } + /** + * Builds a default DirectedPosition sequence at the given position. + * + * @param pos the position + * @return the sequence + */ private static Sq buildDefaultDirectedPositionSequence(Cell pos) { DirectedPosition dp = new DirectedPosition( SubCell.centralSubCellOf(Objects.requireNonNull(pos)), @@ -160,15 +175,15 @@ public final class Player { } /** - * Instanciates a new Player. + * Instantiates a new Player. * - * @param id - * @param lifeStates - * @param directedPos - * @param maxBombs - * @param bombRange - * @throws IllegalArfuementException - * @throws NullPointerExeption + * @param id the Player's id + * @param lifeStates a sequence of LifeState-s + * @param directedPos a sequence of DirectedPosition-s + * @param maxBombs the maximum number of Bomb-s the Player can carry + * @param bombRange the range of the Bomb-s + * @throws IllegalArgumentException + * @throws NullPointerException */ public Player(PlayerID id, Sq lifeStates, Sq directedPos, int maxBombs, int bombRange) { this.id = Objects.requireNonNull(id); @@ -179,15 +194,15 @@ public final class Player { } /** - * Instanciates a new Player. + * Instantiates a new Player. * - * @param id - * @param lives - * @param position - * @param maxBombs - * @param bombRange + * @param id the Player's id + * @param lives the number of lives of the Player + * @param position the starting position of the Player + * @param maxBombs the maximum number of Bomb-s the Player can carry + * @param bombRange the range of the Bomb-s * @throws IllegalArgumentException - * @throws NullPointerExeption + * @throws NullPointerException */ public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { this( @@ -244,7 +259,7 @@ public final class Player { } /** - * @return the current number of lifes of the player + * @return the current number of lives of the player */ public int lives() { return lifeStates.head().lives(); -- cgit v1.2.3 From 66b79d98f7014a811c12a3ab2ea313fb47605a32 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 7 Mar 2016 17:06:56 +0100 Subject: Fix non-sense non-empty null check --- src/ch/epfl/xblast/server/Bomb.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java index 659adb6..4b89752 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -42,11 +42,11 @@ public final class Bomb { public Bomb(PlayerID ownerId, Cell position, Sq fuseLengths, int range) { this.ownerId = Objects.requireNonNull(ownerId); this.position = Objects.requireNonNull(position); - if (fuseLengths.isEmpty()) { + + this.fuseLengths = Objects.requireNonNull(fuseLengths); + if (this.fuseLengths.isEmpty()) throw new IllegalArgumentException(); - } else { - this.fuseLengths = Objects.requireNonNull(fuseLengths); - } + this.range = ArgumentChecker.requireNonNegative(range); } -- cgit v1.2.3 From 69ec741acb840c444471579d53aafd94eae80795 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 7 Mar 2016 17:08:07 +0100 Subject: Reformat code --- src/ch/epfl/xblast/ArgumentChecker.java | 4 ++- src/ch/epfl/xblast/PlayerID.java | 2 ++ src/ch/epfl/xblast/server/Bomb.java | 32 +++++++++++--------- src/ch/epfl/xblast/server/Player.java | 52 +++++++++++++++++---------------- 4 files changed, 50 insertions(+), 40 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/ArgumentChecker.java b/src/ch/epfl/xblast/ArgumentChecker.java index a567fbb..311807e 100644 --- a/src/ch/epfl/xblast/ArgumentChecker.java +++ b/src/ch/epfl/xblast/ArgumentChecker.java @@ -7,12 +7,13 @@ package ch.epfl.xblast; * @author Timothée FLOURE (257420) */ public final class ArgumentChecker { + /** * Returns the given value if it is non-negative. * * @param value the tested value - * @throws IllegalArgumentException if the value is inferior to 0 * @return the given value if non-negative + * @throws IllegalArgumentException if the value is inferior to 0 */ public static int requireNonNegative(int value) { if (value >= 0) { @@ -21,4 +22,5 @@ public final class ArgumentChecker { throw new IllegalArgumentException(); } } + } diff --git a/src/ch/epfl/xblast/PlayerID.java b/src/ch/epfl/xblast/PlayerID.java index bb77405..c2c5c58 100644 --- a/src/ch/epfl/xblast/PlayerID.java +++ b/src/ch/epfl/xblast/PlayerID.java @@ -7,8 +7,10 @@ package ch.epfl.xblast; * @author Timothée FLOURE (257420) */ public enum PlayerID { + PLAYER_1, PLAYER_2, PLAYER_3, PLAYER_4 + } diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java index 4b89752..12e8888 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -1,14 +1,14 @@ package ch.epfl.xblast.server; -import ch.epfl.xblast.PlayerID; -import ch.epfl.xblast.Cell; 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.Objects; -import java.util.List; import java.util.ArrayList; +import java.util.List; +import java.util.Objects; /** * A Bomb. @@ -17,6 +17,7 @@ import java.util.ArrayList; * @author Timothée FLOURE (257420) */ public final class Bomb { + private PlayerID ownerId; private Cell position; private Sq fuseLengths; @@ -26,18 +27,20 @@ public final class Bomb { * Generates one arm of explosion. */ private Sq> explosionArmTowards(Direction dir) { - return Sq.constant( Sq.iterate(position, position -> position.neighbor(dir)).limit(range)).limit(Ticks.EXPLOSION_TICKS); + 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 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 + * @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 + * @throws NullPointerException if ownerId, position or fuseLengths is null */ public Bomb(PlayerID ownerId, Cell position, Sq fuseLengths, int range) { this.ownerId = Objects.requireNonNull(ownerId); @@ -53,15 +56,15 @@ public final class Bomb { /** * Instantiates a new Bomb. * - * @param ownerId id of the owner of the bomb - * @param position position of the 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 + * @param range range of the bomb * @throws IllegalArgumentException if range or fuseLengths is negative - * @throws NullPointerException if ownerId, position or fuseLengths is null + * @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); + this(ownerId, position, Sq.iterate(fuseLength, fl -> fl - 1), range); } /** @@ -109,4 +112,5 @@ public final class Bomb { } return explosion; } + } diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index 0548125..7b5ac26 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -1,11 +1,7 @@ package ch.epfl.xblast.server; -import ch.epfl.xblast.ArgumentChecker; -import ch.epfl.xblast.Direction; -import ch.epfl.xblast.Cell; -import ch.epfl.xblast.SubCell; -import ch.epfl.xblast.PlayerID; import ch.epfl.cs108.Sq; +import ch.epfl.xblast.*; import java.util.Objects; @@ -16,10 +12,12 @@ import java.util.Objects; * @author Timothée FLOURE (257420) */ public final class Player { + /** * The life state of a player. */ public static final class LifeState { + /** * Enum containing all the possible life states. */ @@ -65,13 +63,15 @@ public final class Player { public boolean canMove() { return (state() == State.INVULNERABLE || state() == State.VULNERABLE); } + } /** * The "directed" position of a player. */ public static final class DirectedPosition { - private final SubCell position; + + private final SubCell position; private final Direction direction; /** @@ -91,7 +91,7 @@ public final class Player { /** * Instantiates a new DirectedPos * - * @param position the position of the player + * @param position the position of the player * @param direction the direction of the player * @throws IllegalArgumentException if position or direction is null */ @@ -122,11 +122,12 @@ public final class Player { } /** - * @return a new directed position with the previous position and the given direction + * @return a new directed position with the previous position and the given direction */ public DirectedPosition withDirection(Direction newDirection) { return new DirectedPosition(position, newDirection); } + } /** @@ -156,7 +157,7 @@ public final class Player { LifeState.State.VULNERABLE ); - return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerability).concat(Sq.constant(vulnerability)); + return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability).concat(Sq.constant(vulnerability)); } /** @@ -177,11 +178,11 @@ public final class Player { /** * Instantiates a new Player. * - * @param id the Player's id - * @param lifeStates a sequence of LifeState-s + * @param id the Player's id + * @param lifeStates a sequence of LifeState-s * @param directedPos a sequence of DirectedPosition-s - * @param maxBombs the maximum number of Bomb-s the Player can carry - * @param bombRange the range of the Bomb-s + * @param maxBombs the maximum number of Bomb-s the Player can carry + * @param bombRange the range of the Bomb-s * @throws IllegalArgumentException * @throws NullPointerException */ @@ -196,22 +197,22 @@ public final class Player { /** * Instantiates a new Player. * - * @param id the Player's id - * @param lives the number of lives of the Player - * @param position the starting position of the Player - * @param maxBombs the maximum number of Bomb-s the Player can carry + * @param id the Player's id + * @param lives the number of lives of the Player + * @param position the starting position of the Player + * @param maxBombs the maximum number of Bomb-s the Player can carry * @param bombRange the range of the Bomb-s * @throws IllegalArgumentException * @throws NullPointerException */ public Player(PlayerID id, int lives, Cell position, int maxBombs, int bombRange) { - this( - id, - Player.buildDefaultLifeStateSequence(lives), - Player.buildDefaultDirectedPositionSequence(position), - maxBombs, - bombRange - ); + this( + id, + Player.buildDefaultLifeStateSequence(lives), + Player.buildDefaultDirectedPositionSequence(position), + maxBombs, + bombRange + ); } /** @@ -244,7 +245,7 @@ public final class Player { LifeState invulnerable = new LifeState(newLives, LifeState.State.INVULNERABLE); LifeState vulnerable = new LifeState(newLives, LifeState.State.VULNERABLE); - nextLifeState = nextLifeState.concat(Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS,invulnerable)); + nextLifeState = nextLifeState.concat(Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerable)); nextLifeState = nextLifeState.concat(Sq.constant(vulnerable)); } @@ -327,4 +328,5 @@ public final class Player { public Bomb newBomb() { return new Bomb(id, position().containingCell(), Ticks.BOMB_FUSE_TICKS, bombRange); } + } -- cgit v1.2.3