aboutsummaryrefslogtreecommitdiff
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
parentfa66073ed8bb68b144c3e7397ef95ce0d8442ae8 (diff)
downloadxblast-552d82f34ee4b60785164bc40d6dd5b5d8923609.tar.gz
Implement random event live simulation
-rw-r--r--src/ch/epfl/xblast/server/GameState.java12
-rw-r--r--src/ch/epfl/xblast/server/Player.java11
-rw-r--r--src/ch/epfl/xblast/server/debug/GameStatePrinter.java15
-rw-r--r--test/ch/epfl/xblast/server/BoardTest.java21
-rw-r--r--test/ch/epfl/xblast/simulation/RandomSimulation.java80
5 files changed, 135 insertions, 4 deletions
diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java
index 1abe17a..4b00c3e 100644
--- a/src/ch/epfl/xblast/server/GameState.java
+++ b/src/ch/epfl/xblast/server/GameState.java
@@ -477,4 +477,16 @@ public final class GameState {
477 .collect(Collectors.toList()); 477 .collect(Collectors.toList());
478 } 478 }
479 479
480 @Override
481 public String toString() {
482 return "GameState{" +
483 "ticks=" + ticks +
484 ", board=" + board +
485 ", players=" + players +
486 ", bombs=" + bombs +
487 ", explosions=" + explosions +
488 ", blasts=" + blasts +
489 '}';
490 }
491
480} 492}
diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java
index 094d395..d2f7b88 100644
--- a/src/ch/epfl/xblast/server/Player.java
+++ b/src/ch/epfl/xblast/server/Player.java
@@ -329,4 +329,15 @@ public final class Player {
329 return new Bomb(this.id(), this.position().containingCell(), Ticks.BOMB_FUSE_TICKS, this.bombRange()); 329 return new Bomb(this.id(), this.position().containingCell(), Ticks.BOMB_FUSE_TICKS, this.bombRange());
330 } 330 }
331 331
332 @Override
333 public String toString() {
334 return "Player{" +
335 "id=" + id +
336 ", lifeStates=" + lifeStates +
337 ", directedPos=" + directedPos +
338 ", maxBombs=" + maxBombs +
339 ", bombRange=" + bombRange +
340 '}';
341 }
342
332} 343}
diff --git a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
index ea8b360..d4d91e5 100644
--- a/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
+++ b/src/ch/epfl/xblast/server/debug/GameStatePrinter.java
@@ -12,6 +12,7 @@ import java.util.List;
12 * Game state printer utility class that outputs the board to the terminal. 12 * Game state printer utility class that outputs the board to the terminal.
13 * 13 *
14 * @author EPFL 14 * @author EPFL
15 * @author Pacien TRAN-GIRARD (261948)
15 */ 16 */
16public final class GameStatePrinter { 17public final class GameStatePrinter {
17 18
@@ -19,9 +20,15 @@ public final class GameStatePrinter {
19 } 20 }
20 21
21 public static void printGameState(GameState s) { 22 public static void printGameState(GameState s) {
22 List<Player> ps = s.alivePlayers(); 23 printStats(s);
23 Board board = s.board(); 24 printBoard(s.board(), s.alivePlayers());
25 }
26
27 private static void printStats(GameState s) {
28 System.out.println(s);
29 }
24 30
31 private static void printBoard(Board b, List<Player> ps) {
25 for (int y = 0; y < Cell.ROWS; ++y) { 32 for (int y = 0; y < Cell.ROWS; ++y) {
26 xLoop: 33 xLoop:
27 for (int x = 0; x < Cell.COLUMNS; ++x) { 34 for (int x = 0; x < Cell.COLUMNS; ++x) {
@@ -32,8 +39,8 @@ public final class GameStatePrinter {
32 continue xLoop; 39 continue xLoop;
33 } 40 }
34 } 41 }
35 Block b = board.blockAt(c); 42 Block block = b.blockAt(c);
36 System.out.print(stringForBlock(b)); 43 System.out.print(stringForBlock(block));
37 } 44 }
38 System.out.println(); 45 System.out.println();
39 } 46 }
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}