aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/server/Block.java31
-rw-r--r--src/ch/epfl/xblast/server/GameState.java28
2 files changed, 30 insertions, 29 deletions
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 @@
1package ch.epfl.xblast.server; 1package ch.epfl.xblast.server;
2 2
3import java.util.NoSuchElementException; 3import java.util.NoSuchElementException;
4import java.util.Random;
4 5
5/** 6/**
6 * Blocks. 7 * Blocks.
@@ -41,9 +42,25 @@ public enum Block {
41 BONUS_RANGE(Bonus.INC_RANGE); 42 BONUS_RANGE(Bonus.INC_RANGE);
42 43
43 /** 44 /**
45 * The list of bonuses to choose randomly from.
46 */
47 private static final Block[] RANDOM_BONUSES = new Block[]{BONUS_BOMB, BONUS_RANGE, FREE};
48
49 /**
50 * The seed used for random value generation.
51 * Constant between executions to make tests reproducible.
52 */
53 private static final int RANDOM_SEED = 2016;
54
55 /**
56 * Pseudo-random source for randomized behaviours.
57 */
58 private static final Random RANDOM_SOURCE = new Random(RANDOM_SEED);
59
60 /**
44 * Corresponding bonus, or null. 61 * Corresponding bonus, or null.
45 */ 62 */
46 private Bonus maybeAssociatedBonus; 63 private final Bonus maybeAssociatedBonus;
47 64
48 /** 65 /**
49 * Main builder, used by the bonus blocks. 66 * Main builder, used by the bonus blocks.
@@ -56,7 +73,17 @@ public enum Block {
56 * Default builder, used by the non-bonus blocks. 73 * Default builder, used by the non-bonus blocks.
57 */ 74 */
58 Block() { 75 Block() {
59 this.maybeAssociatedBonus = null; 76 this(null);
77 }
78
79 /**
80 * Returns a randomly chosen bonus Block with an equal probability distribution.
81 *
82 * @return a random bonus block
83 */
84 static Block getRandomBonusBlock() {
85 int randomIndex = RANDOM_SOURCE.nextInt(RANDOM_BONUSES.length);
86 return RANDOM_BONUSES[randomIndex];
60 } 87 }
61 88
62 /** 89 /**
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;
17 */ 17 */
18public final class GameState { 18public final class GameState {
19 19
20 /**
21 * The list of bonuses to choose randomly from.
22 */
23 private static final Block[] RANDOM_BONUSES = new Block[]{Block.BONUS_BOMB, Block.BONUS_RANGE, Block.FREE};
24
25 /**
26 * The seed used for random value generation.
27 * Constant between executions to make tests reproducible.
28 */
29 private static final int RANDOM_SEED = 2016;
30
31 /**
32 * Pseudo-random source for randomized behaviours.
33 */
34 private static final Random RANDOM_SOURCE = new Random(GameState.RANDOM_SEED);
35
36 private final int ticks; 20 private final int ticks;
37 private final Board board; 21 private final Board board;
38 private final List<Player> players; 22 private final List<Player> players;
@@ -75,16 +59,6 @@ public final class GameState {
75 } 59 }
76 60
77 /** 61 /**
78 * Returns a randomly chosen bonus Block with an equal probability distribution.
79 *
80 * @return a random bonus block
81 */
82 private static Block randomBonus() {
83 int randomIndex = RANDOM_SOURCE.nextInt(GameState.RANDOM_BONUSES.length);
84 return GameState.RANDOM_BONUSES[randomIndex];
85 }
86
87 /**
88 * Maps bombs to their owning player ID. 62 * Maps bombs to their owning player ID.
89 * 63 *
90 * @param b the list of bombs 64 * @param b the list of bombs
@@ -146,7 +120,7 @@ public final class GameState {
146 if (blastedCells1.contains(c)) 120 if (blastedCells1.contains(c))
147 if (b == Block.DESTRUCTIBLE_WALL) 121 if (b == Block.DESTRUCTIBLE_WALL)
148 return Sq.repeat(Ticks.WALL_CRUMBLING_TICKS, Block.CRUMBLING_WALL) 122 return Sq.repeat(Ticks.WALL_CRUMBLING_TICKS, Block.CRUMBLING_WALL)
149 .concat(Sq.constant(GameState.randomBonus())); 123 .concat(Sq.constant(Block.getRandomBonusBlock()));
150 124
151 else if (b.isBonus()) 125 else if (b.isBonus())
152 return Sq.repeat(Ticks.BONUS_DISAPPEARING_TICKS, b) 126 return Sq.repeat(Ticks.BONUS_DISAPPEARING_TICKS, b)