From 552d82f34ee4b60785164bc40d6dd5b5d8923609 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 26 Mar 2016 13:44:46 +0100 Subject: Implement random event live simulation --- test/ch/epfl/xblast/server/BoardTest.java | 21 ++++++ .../epfl/xblast/simulation/RandomSimulation.java | 80 ++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 test/ch/epfl/xblast/simulation/RandomSimulation.java (limited to 'test') 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; import org.junit.Test; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static org.junit.Assert.*; @@ -15,6 +16,26 @@ import static org.junit.Assert.*; */ public class BoardTest { + /** + * Builds and returns a NW Quadrant map. + * + * @return the map + */ + public static List> buildNWQuadrantMap() { + Block __ = Block.FREE; + Block XX = Block.INDESTRUCTIBLE_WALL; + Block OO = Block.DESTRUCTIBLE_WALL; + + List> map = new ArrayList<>(7); + map.add(Arrays.asList(__, __, __, __, __, __, __)); + map.add(Arrays.asList(__, XX, OO, XX, OO, XX, OO)); + map.add(Arrays.asList(__, OO, __, __, __, OO, __)); + map.add(Arrays.asList(OO, XX, __, XX, XX, XX, XX)); + map.add(Arrays.asList(__, OO, __, OO, __, __, __)); + map.add(Arrays.asList(OO, XX, OO, XX, OO, XX, __)); + return map; + } + @Test(expected = IllegalArgumentException.class) public void isBoardBuilderEmptyInputThrowingException() { List> 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 @@ +package ch.epfl.xblast.simulation; + +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.PlayerID; +import ch.epfl.xblast.server.Board; +import ch.epfl.xblast.server.BoardTest; +import ch.epfl.xblast.server.GameState; +import ch.epfl.xblast.server.Player; +import ch.epfl.xblast.server.debug.GameStatePrinter; +import ch.epfl.xblast.server.debug.RandomEventGenerator; + +import java.util.Arrays; +import java.util.List; + +/** + * Random game simulation. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public class RandomSimulation { + + private static final long DISPLAY_DELAY = 50; // in milliseconds + + private static final int PLAYER_LIVES = 4; + private static final int PLAYER_MAX_BOMBS = 4; + private static final int PLAYER_BOMB_RANGE = 4; + + private static final int SEED = 2016; + private static final int SPEED_CHANGE_PROB = 30; + private static final int BOMB_PROB = 100; + private static final RandomEventGenerator RANDOM_EVENT_GENERATOR = new RandomEventGenerator(SEED, SPEED_CHANGE_PROB, BOMB_PROB); + + private static Board buildTestBoard() { + return Board.ofQuadrantNWBlocksWalled(BoardTest.buildNWQuadrantMap()); + } + + private static Player newPlayer(PlayerID id, Cell pos) { + return new Player(id, PLAYER_LIVES, pos, PLAYER_MAX_BOMBS, PLAYER_BOMB_RANGE); + } + + private static List buildPlayersList() { + return Arrays.asList( + newPlayer(PlayerID.PLAYER_1, new Cell(1, 1)), + newPlayer(PlayerID.PLAYER_2, new Cell(13, 1)), + newPlayer(PlayerID.PLAYER_3, new Cell(13, 11)), + newPlayer(PlayerID.PLAYER_4, new Cell(1, 11))); + } + + private static GameState buildInitialGameState() { + return new GameState(buildTestBoard(), buildPlayersList()); + } + + private static GameState nextGameState(GameState gs) { + return gs.next(RANDOM_EVENT_GENERATOR.randomSpeedChangeEvents(), RANDOM_EVENT_GENERATOR.randomBombDropEvents()); + } + + private static boolean isSimulationOver(GameState gs) { + return gs == null || gs.isGameOver(); + } + + private static void delay() { + try { + Thread.sleep(DISPLAY_DELAY); + } catch (InterruptedException e) { + e.printStackTrace(); + System.exit(1); + } + } + + private static void displayGameState(GameState gs) { + GameStatePrinter.printGameState(gs); + delay(); + } + + public static void main(String[] args) { + for (GameState gs = buildInitialGameState(); !isSimulationOver(gs); gs = nextGameState(gs)) + displayGameState(gs); + } + +} -- cgit v1.2.3