From 22f54da84e8e7e7a52fbc418ac6f168aff77997a Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 9 Apr 2016 21:51:38 +0200 Subject: Fix Player and Bomb specific test failures --- src/ch/epfl/xblast/server/Bomb.java | 19 +++++++++++++------ src/ch/epfl/xblast/server/Player.java | 22 ++++++++++++---------- 2 files changed, 25 insertions(+), 16 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 c1a6b12..dd26ddd 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -22,6 +22,17 @@ public final class Bomb { private static final UnaryOperator FUSE_STEP_FUNCTION = fl -> fl - 1; + /** + * Builds and returns a new fuse sequence of given length. + * + * @param fuseLength the fuse length + * @return the fuse sequence + */ + private static Sq buildFuseSequence(int fuseLength) { + int fl = ArgumentChecker.requireNonNegative(fuseLength); + return Sq.iterate(fl, Bomb.FUSE_STEP_FUNCTION).limit(fl); + } + private PlayerID ownerId; private Cell position; private Sq fuseLengths; @@ -49,11 +60,7 @@ public final class Bomb { public Bomb(PlayerID ownerId, Cell position, Sq 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.fuseLengths = ArgumentChecker.requireNonEmpty(fuseLengths); this.range = ArgumentChecker.requireNonNegative(range); } @@ -68,7 +75,7 @@ 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, position, Sq.iterate(fuseLength, Bomb.FUSE_STEP_FUNCTION), range); + this(ownerId, position, Bomb.buildFuseSequence(fuseLength), range); } /** diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index afff209..e8f3145 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -40,7 +40,7 @@ public final class Player { */ public LifeState(int lives, State state) { this.lives = ArgumentChecker.requireNonNegative(lives); - this.state = state; + this.state = Objects.requireNonNull(state); } /** @@ -166,16 +166,18 @@ public final class Player { * @return the sequence */ 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 - ); + int l = ArgumentChecker.requireNonNegative(lives); + + switch (l) { + case 0: + return Sq.constant(new LifeState(l, LifeState.State.DEAD)); - return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability).concat(Sq.constant(vulnerability)); + default: + LifeState invulnerability = new LifeState(l, LifeState.State.INVULNERABLE); + LifeState vulnerability = new LifeState(l, LifeState.State.VULNERABLE); + return Sq.repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability).concat(Sq.constant(vulnerability)); + + } } /** -- cgit v1.2.3