From 31b29f7418540f956257894560a09f6ccd6ee091 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Fri, 6 May 2016 19:34:37 +0200 Subject: Refactor default level building --- src/ch/epfl/xblast/PlayerID.java | 17 +++- src/ch/epfl/xblast/server/Board.java | 48 +++++++--- src/ch/epfl/xblast/server/GameState.java | 2 + src/ch/epfl/xblast/server/Level.java | 104 ++------------------- src/ch/epfl/xblast/server/Player.java | 95 ++++++++++++------- src/ch/epfl/xblast/server/painter/BlockImage.java | 29 +++++- .../epfl/xblast/server/painter/BoardPainter.java | 5 +- 7 files changed, 156 insertions(+), 144 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/xblast/PlayerID.java b/src/ch/epfl/xblast/PlayerID.java index c2c5c58..9377fdf 100644 --- a/src/ch/epfl/xblast/PlayerID.java +++ b/src/ch/epfl/xblast/PlayerID.java @@ -11,6 +11,21 @@ public enum PlayerID { PLAYER_1, PLAYER_2, PLAYER_3, - PLAYER_4 + PLAYER_4; + + public Cell initialPosition() { + switch (this) { + case PLAYER_1: + return new Cell(1, 1); + case PLAYER_2: + return new Cell(13, 1); + case PLAYER_3: + return new Cell(13, 11); + case PLAYER_4: + return new Cell(1, 11); + default: + return null; + } + } } diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java index 4519781..4f5e045 100644 --- a/src/ch/epfl/xblast/server/Board.java +++ b/src/ch/epfl/xblast/server/Board.java @@ -4,6 +4,7 @@ import ch.epfl.cs108.Sq; import ch.epfl.xblast.Cell; import ch.epfl.xblast.Lists; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -17,6 +18,8 @@ import java.util.stream.Collectors; */ public final class Board { + static final Board DEFAULT_BOARD = buildDefaultBoard(); + /** * Distance (in SubCells) from a bomb to block a player. */ @@ -38,21 +41,22 @@ public final class Board { private static final int BLOCKS_LIST_SIZE = BOARD_ROWS * BOARD_COLUMNS; /** - * List containing all the blocks of the board. - */ - private final List> blocks; - - /** - * Instantiates a new Board with the given sequence of blocks. + * Build the default board of the game. * - * @param blocks sequence containing all the blocks of the Boards - * @throws IllegalArgumentException if the blocks is not composed of BLOC_LIST_SIZE elements + * @return the default board. */ - public Board(List> blocks) { - if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) - throw new IllegalArgumentException(); - - this.blocks = Lists.immutableList(blocks); + private static Board buildDefaultBoard() { + Block __ = Block.FREE; + Block XX = Block.INDESTRUCTIBLE_WALL; + Block xx = Block.DESTRUCTIBLE_WALL; + + return Board.ofQuadrantNWBlocksWalled(Arrays.asList( + Arrays.asList(__, __, __, __, __, xx, __), + Arrays.asList(__, XX, xx, XX, xx, XX, xx), + Arrays.asList(__, xx, __, __, __, xx, __), + Arrays.asList(xx, XX, __, XX, XX, XX, XX), + Arrays.asList(__, xx, __, xx, __, __, __), + Arrays.asList(xx, XX, xx, XX, xx, XX, __))); } /** @@ -127,6 +131,24 @@ public final class Board { return new Board(blockSequence); } + /** + * List containing all the blocks of the board. + */ + private final List> blocks; + + /** + * Instantiates a new Board with the given sequence of blocks. + * + * @param blocks sequence containing all the blocks of the Boards + * @throws IllegalArgumentException if the blocks is not composed of BLOC_LIST_SIZE elements + */ + public Board(List> blocks) { + if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) + throw new IllegalArgumentException(); + + this.blocks = Lists.immutableList(blocks); + } + /** * Returns the block related to the given cell. * diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 564ef38..16cb01b 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -16,6 +16,8 @@ import java.util.stream.Collectors; */ public final class GameState { + static final GameState DEFAULT_GAME_STATE = new GameState(Board.DEFAULT_BOARD, Player.DEFAULT_PLAYERS); + private final int ticks; private final Board board; private final List players; diff --git a/src/ch/epfl/xblast/server/Level.java b/src/ch/epfl/xblast/server/Level.java index 155b99a..a386429 100644 --- a/src/ch/epfl/xblast/server/Level.java +++ b/src/ch/epfl/xblast/server/Level.java @@ -1,112 +1,27 @@ package ch.epfl.xblast.server; -import ch.epfl.xblast.*; -import ch.epfl.xblast.server.painter.*; - -import java.util.Arrays; -import java.util.List; -import java.util.HashMap; +import ch.epfl.xblast.server.painter.BoardPainter; /** + * A level. + * + * @author Pacien TRAN-GIRARD (261948) * @author Timothée FLOURE (257420) */ public final class Level { - /** Players' initial parameters **/ - private static final int PLAYER_INITIAL_LIVES = 3; - private static final int PLAYER_INITIAL_BOMB_MAXIMUM = 5; - private static final int PLAYER_INITIAL_BOMB_RANGE = 5; - - /** Players' initial positions (Ugly!) **/ - private static final Cell PLAYER_1_INITIAL_POSITION = new Cell(1,1); - private static final Cell PLAYER_2_INITIAL_POSITION = new Cell(13,1); - private static final Cell PLAYER_3_INITIAL_POSITION = new Cell(13,11); - private static final Cell PLAYER_4_INITIAL_POSITION = new Cell(1,11); - - /** Level values **/ - private final BoardPainter painter; - private final GameState initialState; - - /** - * Build the default board of the game. - * - * @return the default board - */ - private static BoardPainter buildDefaultBoardPainter() { - // Create the blocks map - HashMap blocksMap = new HashMap<>(); - - // Fill the blocks map - blocksMap.put(Block.FREE, BlockImage.IRON_FLOOR); - blocksMap.put(Block.DESTRUCTIBLE_WALL, BlockImage.EXTRA); - blocksMap.put(Block.CRUMBLING_WALL, BlockImage.EXTRA_O); - blocksMap.put(Block.INDESTRUCTIBLE_WALL, BlockImage.DARK_BLOCK); - blocksMap.put(Block.BONUS_BOMB, BlockImage.BONUS_BOMB); - blocksMap.put(Block.BONUS_RANGE, BlockImage.BONUS_RANGE); - - // Create and return the board painter - return new BoardPainter(blocksMap, BlockImage.IRON_FLOOR_S); - } - - /** - * Build the default game state of the game. - * - * @return the default game state - */ - private static GameState buildDefaultGameState() { - return new GameState(buildDefaultBoard(), buildDefaultPlayers()); - } - - /** - * Build the default board of the game. - * - * @return the default board. - */ - private static Board buildDefaultBoard() { - - // Ugly! - Block __ = Block.FREE; - Block XX = Block.INDESTRUCTIBLE_WALL; - Block xx = Block.DESTRUCTIBLE_WALL; - - return Board.ofQuadrantNWBlocksWalled( - Arrays.asList( - Arrays.asList(__, __, __, __, __, xx, __), - Arrays.asList(__, XX, xx, XX, xx, XX, xx), - Arrays.asList(__, xx, __, __, __, xx, __), - Arrays.asList(xx, XX, __, XX, XX, XX, XX), - Arrays.asList(__, xx, __, xx, __, __, __), - Arrays.asList(xx, XX, xx, XX, xx, XX, __))); - } - - /** - * Build the default players of the games. - * - * @return a list of the 4 players built using the default parameters. - */ - private static List buildDefaultPlayers() { - // Ugly! - return Arrays.asList( - new Player(PlayerID.PLAYER_1, PLAYER_INITIAL_LIVES, PLAYER_1_INITIAL_POSITION, - PLAYER_INITIAL_BOMB_MAXIMUM, PLAYER_INITIAL_BOMB_RANGE), - new Player(PlayerID.PLAYER_2, PLAYER_INITIAL_LIVES, PLAYER_2_INITIAL_POSITION, - PLAYER_INITIAL_BOMB_MAXIMUM, PLAYER_INITIAL_BOMB_RANGE), - new Player(PlayerID.PLAYER_3, PLAYER_INITIAL_LIVES, PLAYER_3_INITIAL_POSITION, - PLAYER_INITIAL_BOMB_MAXIMUM, PLAYER_INITIAL_BOMB_RANGE), - new Player(PlayerID.PLAYER_4, PLAYER_INITIAL_LIVES, PLAYER_4_INITIAL_POSITION, - PLAYER_INITIAL_BOMB_MAXIMUM, PLAYER_INITIAL_BOMB_RANGE) - ); - } - /** * Level build with the default images and the default players/explosions/board parameters. */ - public static final Level DEFAULT_LEVEL = new Level(buildDefaultBoardPainter(), buildDefaultGameState()); + static final Level DEFAULT_LEVEL = new Level(BoardPainter.DEFAULT_BOARD_PAINTER, GameState.DEFAULT_GAME_STATE); + + private final BoardPainter painter; + private final GameState initialState; /** * Instantiates a new level. * - * @param painter painter related to the level + * @param painter painter related to the level * @param initialState initial game state of the level */ public Level(BoardPainter painter, GameState initialState) { @@ -131,4 +46,5 @@ public final class Level { public GameState initialState() { return this.initialState; } + } diff --git a/src/ch/epfl/xblast/server/Player.java b/src/ch/epfl/xblast/server/Player.java index ad1dfe8..46e254d 100644 --- a/src/ch/epfl/xblast/server/Player.java +++ b/src/ch/epfl/xblast/server/Player.java @@ -3,7 +3,11 @@ package ch.epfl.xblast.server; import ch.epfl.cs108.Sq; import ch.epfl.xblast.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; /** * A Player. @@ -13,10 +17,65 @@ import java.util.Objects; */ public final class Player { + private static final int INITIAL_LIVES = 3; + private static final int INITIAL_MAX_BOMB = 5; + private static final int INITIAL_BOMB_RANGE = 5; + private static final Direction INITIAL_DIRECTION = Direction.S; + + static final List DEFAULT_PLAYERS = buildDefaultPlayers(); + + /** + * Builds the default players of the games. + * + * @return a list of the 4 players built using the default parameters. + */ + private static List buildDefaultPlayers() { + return Collections.unmodifiableList(Arrays.stream(PlayerID.values()) + .map(Player::buildDefaultPlayer) + .collect(Collectors.toList())); + } + + /** + * Builds a player with default initial characteristics. + * + * @param id the player id + * @return the player + */ + private static Player buildDefaultPlayer(PlayerID id) { + return new Player(id, INITIAL_LIVES, id.initialPosition(), INITIAL_MAX_BOMB, INITIAL_BOMB_RANGE); + } + /** - * The default Direction of a new Player. + * Builds a default LifeState sequence with the given number of lives. + * + * @param lives number of lives of the desired sequence + * @return the sequence */ - private static final Direction DEFAULT_DIRECTION = Direction.S; + private static Sq buildLifeStateSequence(int lives) { + if (ArgumentChecker.requireNonNegative(lives) <= 0) + return Sq.constant(new LifeState(lives, LifeState.State.DEAD)); + + LifeState invulnerability = new LifeState(lives, LifeState.State.INVULNERABLE); + LifeState vulnerability = new LifeState(lives, LifeState.State.VULNERABLE); + + return Sq + .repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability) + .concat(Sq.constant(vulnerability)); + } + + /** + * Builds a default DirectedPosition sequence at the given position. + * + * @param pos the position + * @return the sequence + */ + private static Sq buildDefaultDirectedPositionSequence(Cell pos) { + DirectedPosition dp = new DirectedPosition( + SubCell.centralSubCellOf(Objects.requireNonNull(pos)), + Player.INITIAL_DIRECTION); + + return DirectedPosition.stopped(dp); + } private final PlayerID id; private final Sq lifeStates; @@ -68,38 +127,6 @@ public final class Player { this.bombRange = ArgumentChecker.requireNonNegative(bombRange); } - /** - * Builds a default LifeState sequence with the given number of lives. - * - * @param lives number of lives of the desired sequence - * @return the sequence - */ - private static Sq buildLifeStateSequence(int lives) { - if (ArgumentChecker.requireNonNegative(lives) <= 0) - return Sq.constant(new LifeState(lives, LifeState.State.DEAD)); - - LifeState invulnerability = new LifeState(lives, LifeState.State.INVULNERABLE); - LifeState vulnerability = new LifeState(lives, LifeState.State.VULNERABLE); - - return Sq - .repeat(Ticks.PLAYER_INVULNERABLE_TICKS, invulnerability) - .concat(Sq.constant(vulnerability)); - } - - /** - * Builds a default DirectedPosition sequence at the given position. - * - * @param pos the position - * @return the sequence - */ - private static Sq buildDefaultDirectedPositionSequence(Cell pos) { - DirectedPosition dp = new DirectedPosition( - SubCell.centralSubCellOf(Objects.requireNonNull(pos)), - Player.DEFAULT_DIRECTION); - - return DirectedPosition.stopped(dp); - } - /** * Builds and returns the Player's next life LifeState sequence. * diff --git a/src/ch/epfl/xblast/server/painter/BlockImage.java b/src/ch/epfl/xblast/server/painter/BlockImage.java index 85a44be..5a15471 100644 --- a/src/ch/epfl/xblast/server/painter/BlockImage.java +++ b/src/ch/epfl/xblast/server/painter/BlockImage.java @@ -1,5 +1,11 @@ package ch.epfl.xblast.server.painter; +import ch.epfl.xblast.server.Block; + +import java.util.Collections; +import java.util.EnumMap; +import java.util.Map; + /** * Block images enumeration. * @@ -41,6 +47,27 @@ public enum BlockImage { /** * Bomb Range Bonus. */ - BONUS_RANGE + BONUS_RANGE; + + public static final Map DEFAULT_BLOCK_MAP = buildDefaultBlockMap(); + public static final BlockImage SHADOWED_BLOCK_IMAGE = IRON_FLOOR_S; + + /** + * Builds the default block-image map. + * + * @return the default block-image map + */ + private static Map buildDefaultBlockMap() { + Map bm = new EnumMap<>(Block.class); + + bm.put(Block.FREE, IRON_FLOOR); + bm.put(Block.DESTRUCTIBLE_WALL, EXTRA); + bm.put(Block.CRUMBLING_WALL, EXTRA_O); + bm.put(Block.INDESTRUCTIBLE_WALL, DARK_BLOCK); + bm.put(Block.BONUS_BOMB, BONUS_BOMB); + bm.put(Block.BONUS_RANGE, BONUS_RANGE); + + return Collections.unmodifiableMap(bm); + } } diff --git a/src/ch/epfl/xblast/server/painter/BoardPainter.java b/src/ch/epfl/xblast/server/painter/BoardPainter.java index 34d0974..c032e9c 100644 --- a/src/ch/epfl/xblast/server/painter/BoardPainter.java +++ b/src/ch/epfl/xblast/server/painter/BoardPainter.java @@ -6,7 +6,6 @@ import ch.epfl.xblast.Lists; import ch.epfl.xblast.server.Block; import ch.epfl.xblast.server.Board; -import java.util.Collections; import java.util.Map; /** @@ -17,6 +16,9 @@ import java.util.Map; */ public final class BoardPainter { + public static final BoardPainter DEFAULT_BOARD_PAINTER = + new BoardPainter(BlockImage.DEFAULT_BLOCK_MAP, BlockImage.SHADOWED_BLOCK_IMAGE); + private final Map blocksMap; private final BlockImage shadowedFreeBlock; @@ -31,6 +33,7 @@ public final class BoardPainter { this.shadowedFreeBlock = shadowedFreeBlock; } + /** * Returns the bits sequence of the block related to the given Cell. * -- cgit v1.2.3