diff options
Diffstat (limited to 'test/ch/epfl/xblast/simulation/RandomSimulation.java')
-rw-r--r-- | test/ch/epfl/xblast/simulation/RandomSimulation.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/test/ch/epfl/xblast/simulation/RandomSimulation.java b/test/ch/epfl/xblast/simulation/RandomSimulation.java new file mode 100644 index 0000000..3888650 --- /dev/null +++ b/test/ch/epfl/xblast/simulation/RandomSimulation.java | |||
@@ -0,0 +1,80 @@ | |||
1 | package ch.epfl.xblast.simulation; | ||
2 | |||
3 | import ch.epfl.xblast.Cell; | ||
4 | import ch.epfl.xblast.PlayerID; | ||
5 | import ch.epfl.xblast.server.Board; | ||
6 | import ch.epfl.xblast.server.BoardTest; | ||
7 | import ch.epfl.xblast.server.GameState; | ||
8 | import ch.epfl.xblast.server.Player; | ||
9 | import ch.epfl.xblast.server.debug.GameStatePrinter; | ||
10 | import ch.epfl.xblast.server.debug.RandomEventGenerator; | ||
11 | |||
12 | import java.util.Arrays; | ||
13 | import java.util.List; | ||
14 | |||
15 | /** | ||
16 | * Random game simulation. | ||
17 | * | ||
18 | * @author Pacien TRAN-GIRARD (261948) | ||
19 | */ | ||
20 | public class RandomSimulation { | ||
21 | |||
22 | private static final long DISPLAY_DELAY = 50; // in milliseconds | ||
23 | |||
24 | private static final int PLAYER_LIVES = 4; | ||
25 | private static final int PLAYER_MAX_BOMBS = 4; | ||
26 | private static final int PLAYER_BOMB_RANGE = 4; | ||
27 | |||
28 | private static final int SEED = 2016; | ||
29 | private static final int SPEED_CHANGE_PROB = 30; | ||
30 | private static final int BOMB_PROB = 100; | ||
31 | private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); | ||
32 | |||
33 | private static Board buildTestBoard() { | ||
34 | return Board.ofQuadrantNWBlocksWalled(BoardTest.buildNWQuadrantMap()); | ||
35 | } | ||
36 | |||
37 | private static Player newPlayer(PlayerID id, Cell pos) { | ||
38 | return new Player(id, PLAYER_LIVES, pos, PLAYER_MAX_BOMBS, PLAYER_BOMB_RANGE); | ||
39 | } | ||
40 | |||
41 | private static List<Player> buildPlayersList() { | ||
42 | return Arrays.asList( | ||
43 | newPlayer(PlayerID.PLAYER_1, new Cell(1, 1)), | ||
44 | newPlayer(PlayerID.PLAYER_2, new Cell(13, 1)), | ||
45 | newPlayer(PlayerID.PLAYER_3, new Cell(13, 11)), | ||
46 | newPlayer(PlayerID.PLAYER_4, new Cell(1, 11))); | ||
47 | } | ||
48 | |||
49 | private static GameState buildInitialGameState() { | ||
50 | return new GameState(buildTestBoard(), buildPlayersList()); | ||
51 | } | ||
52 | |||
53 | private static GameState nextGameState(GameState gs) { | ||
54 | return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); | ||
55 | } | ||
56 | |||
57 | private static boolean isSimulationOver(GameState gs) { | ||
58 | return gs == null || gs.isGameOver(); | ||
59 | } | ||
60 | |||
61 | private static void delay() { | ||
62 | try { | ||
63 | Thread.sleep(DISPLAY_DELAY); | ||
64 | } catch (InterruptedException e) { | ||
65 | e.printStackTrace(); | ||
66 | System.exit(1); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | private static void displayGameState(GameState gs) { | ||
71 | GameStatePrinter.printGameState(gs); | ||
72 | delay(); | ||
73 | } | ||
74 | |||
75 | public static void main(String[] args) { | ||
76 | for (GameState gs = buildInitialGameState(); !isSimulationOver(gs); gs = nextGameState(gs)) | ||
77 | displayGameState(gs); | ||
78 | } | ||
79 | |||
80 | } | ||