From bf4eeeaca44404599b5f9c74e714e1830704c30c Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 5 May 2016 22:26:18 +0200 Subject: Move random bonus block generation to corresponding enum class --- src/ch/epfl/xblast/server/Block.java | 31 +++++++++++++++++++++++++++++-- src/ch/epfl/xblast/server/GameState.java | 28 +--------------------------- 2 files changed, 30 insertions(+), 29 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java index 5ada099..4f3d033 100644 --- a/src/ch/epfl/xblast/server/Block.java +++ b/src/ch/epfl/xblast/server/Block.java @@ -1,6 +1,7 @@ package ch.epfl.xblast.server; import java.util.NoSuchElementException; +import java.util.Random; /** * Blocks. @@ -40,10 +41,26 @@ public enum Block { */ BONUS_RANGE(Bonus.INC_RANGE); + /** + * The list of bonuses to choose randomly from. + */ + private static final Block[] RANDOM_BONUSES = new Block[]{BONUS_BOMB, BONUS_RANGE, FREE}; + + /** + * The seed used for random value generation. + * Constant between executions to make tests reproducible. + */ + private static final int RANDOM_SEED = 2016; + + /** + * Pseudo-random source for randomized behaviours. + */ + private static final Random RANDOM_SOURCE = new Random(RANDOM_SEED); + /** * Corresponding bonus, or null. */ - private Bonus maybeAssociatedBonus; + private final Bonus maybeAssociatedBonus; /** * Main builder, used by the bonus blocks. @@ -56,7 +73,17 @@ public enum Block { * Default builder, used by the non-bonus blocks. */ Block() { - this.maybeAssociatedBonus = null; + this(null); + } + + /** + * Returns a randomly chosen bonus Block with an equal probability distribution. + * + * @return a random bonus block + */ + static Block getRandomBonusBlock() { + int randomIndex = RANDOM_SOURCE.nextInt(RANDOM_BONUSES.length); + return RANDOM_BONUSES[randomIndex]; } /** diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 33a8f5f..b304a4d 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -17,22 +17,6 @@ import java.util.stream.Stream; */ public final class GameState { - /** - * The list of bonuses to choose randomly from. - */ - private static final Block[] RANDOM_BONUSES = new Block[]{Block.BONUS_BOMB, Block.BONUS_RANGE, Block.FREE}; - - /** - * The seed used for random value generation. - * Constant between executions to make tests reproducible. - */ - private static final int RANDOM_SEED = 2016; - - /** - * Pseudo-random source for randomized behaviours. - */ - private static final Random RANDOM_SOURCE = new Random(GameState.RANDOM_SEED); - private final int ticks; private final Board board; private final List players; @@ -74,16 +58,6 @@ public final class GameState { this.blasts = Lists.immutableList(Objects.requireNonNull(blasts)); } - /** - * Returns a randomly chosen bonus Block with an equal probability distribution. - * - * @return a random bonus block - */ - private static Block randomBonus() { - int randomIndex = RANDOM_SOURCE.nextInt(GameState.RANDOM_BONUSES.length); - return GameState.RANDOM_BONUSES[randomIndex]; - } - /** * Maps bombs to their owning player ID. * @@ -146,7 +120,7 @@ public final class GameState { if (blastedCells1.contains(c)) if (b == Block.DESTRUCTIBLE_WALL) return Sq.repeat(Ticks.WALL_CRUMBLING_TICKS, Block.CRUMBLING_WALL) - .concat(Sq.constant(GameState.randomBonus())); + .concat(Sq.constant(Block.getRandomBonusBlock())); else if (b.isBonus()) return Sq.repeat(Ticks.BONUS_DISAPPEARING_TICKS, b) -- cgit v1.2.3