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 --- src/ch/epfl/xblast/server/GameState.java | 12 ++++ src/ch/epfl/xblast/server/Player.java | 11 +++ .../epfl/xblast/server/debug/GameStatePrinter.java | 15 ++-- test/ch/epfl/xblast/server/BoardTest.java | 21 ++++++ .../epfl/xblast/simulation/RandomSimulation.java | 80 ++++++++++++++++++++++ 5 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 test/ch/epfl/xblast/simulation/RandomSimulation.java 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 { .collect(Collectors.toList()); } + @Override + public String toString() { + return "GameState{" + + "ticks=" + ticks + + ", board=" + board + + ", players=" + players + + ", bombs=" + bombs + + ", explosions=" + explosions + + ", blasts=" + blasts + + '}'; + } + } 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 { return new Bomb(this.id(), this.position().containingCell(), Ticks.BOMB_FUSE_TICKS, this.bombRange()); } + @Override + public String toString() { + return "Player{" + + "id=" + id + + ", lifeStates=" + lifeStates + + ", directedPos=" + directedPos + + ", maxBombs=" + maxBombs + + ", bombRange=" + bombRange + + '}'; + } + } 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; * Game state printer utility class that outputs the board to the terminal. * * @author EPFL + * @author Pacien TRAN-GIRARD (261948) */ public final class GameStatePrinter { @@ -19,9 +20,15 @@ public final class GameStatePrinter { } public static void printGameState(GameState s) { - List ps = s.alivePlayers(); - Board board = s.board(); + printStats(s); + printBoard(s.board(), s.alivePlayers()); + } + + private static void printStats(GameState s) { + System.out.println(s); + } + private static void printBoard(Board b, List ps) { for (int y = 0; y < Cell.ROWS; ++y) { xLoop: for (int x = 0; x < Cell.COLUMNS; ++x) { @@ -32,8 +39,8 @@ public final class GameStatePrinter { continue xLoop; } } - Block b = board.blockAt(c); - System.out.print(stringForBlock(b)); + Block block = b.blockAt(c); + System.out.print(stringForBlock(block)); } System.out.println(); } 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