aboutsummaryrefslogtreecommitdiff
path: root/test/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-03-26 13:44:46 +0100
committerPacien TRAN-GIRARD2016-03-26 13:44:46 +0100
commit552d82f34ee4b60785164bc40d6dd5b5d8923609 (patch)
tree1c85c722e095d7498d7c2ab3c73141d7a0153675 /test/ch/epfl
parentfa66073ed8bb68b144c3e7397ef95ce0d8442ae8 (diff)
downloadxblast-552d82f34ee4b60785164bc40d6dd5b5d8923609.tar.gz
Implement random event live simulation
Diffstat (limited to 'test/ch/epfl')
-rw-r--r--test/ch/epfl/xblast/server/BoardTest.java21
-rw-r--r--test/ch/epfl/xblast/simulation/RandomSimulation.java80
2 files changed, 101 insertions, 0 deletions
diff --git a/test/ch/epfl/xblast/server/BoardTest.java b/test/ch/epfl/xblast/server/BoardTest.java
index a7794d1..44feffb 100644
--- a/test/ch/epfl/xblast/server/BoardTest.java
+++ b/test/ch/epfl/xblast/server/BoardTest.java
@@ -5,6 +5,7 @@ import ch.epfl.xblast.Cell;
5import org.junit.Test; 5import org.junit.Test;
6 6
7import java.util.ArrayList; 7import java.util.ArrayList;
8import java.util.Arrays;
8import java.util.List; 9import java.util.List;
9 10
10import static org.junit.Assert.*; 11import static org.junit.Assert.*;
@@ -15,6 +16,26 @@ import static org.junit.Assert.*;
15 */ 16 */
16public class BoardTest { 17public class BoardTest {
17 18
19 /**
20 * Builds and returns a NW Quadrant map.
21 *
22 * @return the map
23 */
24 public static List<List<Block>> buildNWQuadrantMap() {
25 Block __ = Block.FREE;
26 Block XX = Block.INDESTRUCTIBLE_WALL;
27 Block OO = Block.DESTRUCTIBLE_WALL;
28
29 List<List<Block>> map = new ArrayList<>(7);
30 map.add(Arrays.asList(__, __, __, __, __, __, __));
31 map.add(Arrays.asList(__, XX, OO, XX, OO, XX, OO));
32 map.add(Arrays.asList(__, OO, __, __, __, OO, __));
33 map.add(Arrays.asList(OO, XX, __, XX, XX, XX, XX));
34 map.add(Arrays.asList(__, OO, __, OO, __, __, __));
35 map.add(Arrays.asList(OO, XX, OO, XX, OO, XX, __));
36 return map;
37 }
38
18 @Test(expected = IllegalArgumentException.class) 39 @Test(expected = IllegalArgumentException.class)
19 public void isBoardBuilderEmptyInputThrowingException() { 40 public void isBoardBuilderEmptyInputThrowingException() {
20 List<Sq<Block>> blocks = new ArrayList<>(); 41 List<Sq<Block>> blocks = new ArrayList<>();
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 @@
1package ch.epfl.xblast.simulation;
2
3import ch.epfl.xblast.Cell;
4import ch.epfl.xblast.PlayerID;
5import ch.epfl.xblast.server.Board;
6import ch.epfl.xblast.server.BoardTest;
7import ch.epfl.xblast.server.GameState;
8import ch.epfl.xblast.server.Player;
9import ch.epfl.xblast.server.debug.GameStatePrinter;
10import ch.epfl.xblast.server.debug.RandomEventGenerator;
11
12import java.util.Arrays;
13import java.util.List;
14
15/**
16 * Random game simulation.
17 *
18 * @author Pacien TRAN-GIRARD (261948)
19 */
20public 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}