From bd7c60725cdd0ad94f4854b67adf89dda5e4af57 Mon Sep 17 00:00:00 2001 From: Timothée Floure Date: Mon, 25 Apr 2016 12:43:52 +0200 Subject: Week 7 + basic tests --- src/ch/epfl/xblast/server/painter/BlockImage.java | 42 +++++++++++++++ .../epfl/xblast/server/painter/BoardPainter.java | 40 ++++++++++++++ .../xblast/server/painter/ExplosionPainter.java | 55 +++++++++++++++++++ .../epfl/xblast/server/painter/PlayerPainter.java | 44 ++++++++++++++++ test/ch/epfl/xblast/painter/BoardPainterTest.java | 57 ++++++++++++++++++++ .../epfl/xblast/painter/ExplosionPainterTest.java | 37 +++++++++++++ test/ch/epfl/xblast/painter/PlayerPainterTest.java | 61 ++++++++++++++++++++++ 7 files changed, 336 insertions(+) create mode 100644 src/ch/epfl/xblast/server/painter/BlockImage.java create mode 100644 src/ch/epfl/xblast/server/painter/BoardPainter.java create mode 100644 src/ch/epfl/xblast/server/painter/ExplosionPainter.java create mode 100644 src/ch/epfl/xblast/server/painter/PlayerPainter.java create mode 100644 test/ch/epfl/xblast/painter/BoardPainterTest.java create mode 100644 test/ch/epfl/xblast/painter/ExplosionPainterTest.java create mode 100644 test/ch/epfl/xblast/painter/PlayerPainterTest.java diff --git a/src/ch/epfl/xblast/server/painter/BlockImage.java b/src/ch/epfl/xblast/server/painter/BlockImage.java new file mode 100644 index 0000000..d5fa593 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/BlockImage.java @@ -0,0 +1,42 @@ +package ch.epfl.xblast.server.painter; + +/** + * @author Timothée FLOURE (257420 + */ +public enum BlockImage { + + /** + * Free block. + */ + IRON_FLOOR, + + /** + * Shadowed Free block. + */ + IRON_FLOOR_S, + + /** + * Dark Block (Indestructible wall). + */ + DARK_BLOCK, + + /** + * Wall possibly containing a Bonus. + */ + EXTRA, + + /** + * Crumbling Wall possibly containing a Bonus. + */ + EXTRA_O, + + /** + * MaxBomb bonus. + */ + BONUS_BOMB, + + /** + * Bomb Range Bonus. + */ + BONUS_RANGE +} diff --git a/src/ch/epfl/xblast/server/painter/BoardPainter.java b/src/ch/epfl/xblast/server/painter/BoardPainter.java new file mode 100644 index 0000000..69af129 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/BoardPainter.java @@ -0,0 +1,40 @@ +package ch.epfl.xblast.server.painter; + +import ch.epfl.xblast.*; +import ch.epfl.xblast.server.*; + +import java.util.Map; + +/** + * @author Timothée FLOURE (257420) + */ +public final class BoardPainter { + + private final Map blocksMap; + private final BlockImage shadowedFreeBlock; + + /** + * Instantiates a new BoardPainter. + * + * @param blocksMap map linking block to the images + * @param shadowedFreeBlock a "shadowed" block image + */ + public BoardPainter(Map blocksMap, BlockImage shadowedFreeBlock ) { + this.blocksMap = blocksMap; + this.shadowedFreeBlock = shadowedFreeBlock; + } + + /** + * Returns the bits sequence of the block related to the given Cell. + * + * @param board given board + * @param cell given cell + * @return the bits sequence related to the image of the given cell + */ + public byte byteForCell(Board board, Cell cell) { + if (board.blockAt(cell).isFree() && board.blockAt(cell.neighbor(Direction.W)).castsShadow()) + return (byte) shadowedFreeBlock.ordinal(); + + return (byte) this.blocksMap.get(board.blockAt(cell)).ordinal(); + } +} diff --git a/src/ch/epfl/xblast/server/painter/ExplosionPainter.java b/src/ch/epfl/xblast/server/painter/ExplosionPainter.java new file mode 100644 index 0000000..63181e9 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/ExplosionPainter.java @@ -0,0 +1,55 @@ +package ch.epfl.xblast.server.painter; + +import ch.epfl.xblast.server.*; + +/** + * @author Timothée FLOURE (257420) + */ +public final class ExplosionPainter { + private static final byte BLACK_BOMB_IMAGE = 20; + private static final byte WHITE_BOMB_IMAGE = 21; + + private static final byte NORTH_EXPLOSION = 0b1000; + private static final byte EAST_EXPLOSION = 0b0100; + private static final byte SOUTH_EXPLOSION = 0b0010; + private static final byte WEST_EXPLOSION = 0b0001; + + /** + * Return the image corresponding to a bomb depending on its fuse length. + * + * @param bomb given bomb + * @return the byte related to the image of the bomb (black or white) + */ + public static byte byteForBomb(Bomb bomb) + { + if (Integer.bitCount(bomb.fuseLength()) == 1 ) { + return WHITE_BOMB_IMAGE; + } else { + return BLACK_BOMB_IMAGE; + } + } + + /** + * Return the image corresponding to the explosion on a cell given the explosions of its neighbors. + * + * @param north is an explosion on the north cell + * @param east is an explosion on the east cell + * @param south is an explosion on the south cell + * @param west is an explosion on the west cell + * @return the byte corresponding to the related image + */ + public static byte byteForBlast(boolean north, boolean east, boolean south, boolean west) { + byte correspondingByte = 0b0; + + if (north) + correspondingByte += NORTH_EXPLOSION; + if (east) + correspondingByte += EAST_EXPLOSION; + if (south) + correspondingByte += SOUTH_EXPLOSION; + if (west) + correspondingByte += WEST_EXPLOSION; + + return correspondingByte; + } +} diff --git a/src/ch/epfl/xblast/server/painter/PlayerPainter.java b/src/ch/epfl/xblast/server/painter/PlayerPainter.java new file mode 100644 index 0000000..4f28554 --- /dev/null +++ b/src/ch/epfl/xblast/server/painter/PlayerPainter.java @@ -0,0 +1,44 @@ +package ch.epfl.xblast.server.painter; + +import ch.epfl.xblast.server.*; + +/** + * @author Timothée FLOURE (257420) + */ +public final class PlayerPainter { + private static final byte DEAD_PLAYER = 16; + private static final byte DYING_IMAGE_ID = 12; + private static final byte LAST_DYING_IMAGE_ID = 13; + private static final byte PLAYER_MULTIPLIER = 20; + + /** + * Returns the byte related to the image corresponding to the actual state of the given player. + * + * @param player the given player + * @param tick the actual tick of the game + * @return the byte related to the image of the the actual state of the player + */ + public static byte byteForPlayer(Player player, int tick) { + if (!player.isAlive()) + return DEAD_PLAYER; + + byte correspondingByte = (byte) (player.id().ordinal() * PLAYER_MULTIPLIER); + + if (player.lifeState().state() == Player.LifeState.State.DYING) + if (player.lives() > 0) + return (byte) (correspondingByte + DYING_IMAGE_ID); + else + return (byte) (correspondingByte + LAST_DYING_IMAGE_ID ); + + correspondingByte += player.direction().ordinal() * 3; + + switch (tick % 4) { // Magic Numbers !!! + case 1: + return (byte) (correspondingByte + 1); + case 3: + return (byte) (correspondingByte + 2); + default: + return (byte) (correspondingByte + 0); + } + } +} diff --git a/test/ch/epfl/xblast/painter/BoardPainterTest.java b/test/ch/epfl/xblast/painter/BoardPainterTest.java new file mode 100644 index 0000000..9ac8ad4 --- /dev/null +++ b/test/ch/epfl/xblast/painter/BoardPainterTest.java @@ -0,0 +1,57 @@ +package ch.epfl.xblast.painter; + +import ch.epfl.xblast.*; +import ch.epfl.xblast.server.*; +import ch.epfl.xblast.server.painter.*; + +import java.util.Arrays; +import java.util.HashMap; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Timothée Floure (257420) + */ +public class BoardPainterTest { + + public static Board createBoard() { + 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, __))); + } + + @Test + public void byteForCellTest() { + // 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); + + // Instanciates the painter + BoardPainter painter = new BoardPainter(blocksMap, BlockImage.IRON_FLOOR_S); + + // Create a dummy board + Board board = createBoard(); + Cell freeCell = new Cell(2,1); + Cell shadowedFreeCell = new Cell(1,1); + Cell walledCell = new Cell(0,0); + + Assert.assertEquals(painter.byteForCell(board, freeCell), BlockImage.IRON_FLOOR.ordinal()); + Assert.assertEquals(painter.byteForCell(board, shadowedFreeCell), BlockImage.IRON_FLOOR_S.ordinal()); + Assert.assertEquals(painter.byteForCell(board, walledCell), BlockImage.DARK_BLOCK.ordinal()); + + } + + +} diff --git a/test/ch/epfl/xblast/painter/ExplosionPainterTest.java b/test/ch/epfl/xblast/painter/ExplosionPainterTest.java new file mode 100644 index 0000000..204b1ad --- /dev/null +++ b/test/ch/epfl/xblast/painter/ExplosionPainterTest.java @@ -0,0 +1,37 @@ +package ch.epfl.xblast.painter; + +import ch.epfl.xblast.*; +import ch.epfl.xblast.server.*; +import ch.epfl.xblast.server.painter.*; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Timothée Floure (257420) + */ +public class ExplosionPainterTest { + @Test + public void byteForBombTest() { + Cell cell = new Cell(1,1); + int range = 5; + + Bomb bomb1 = new Bomb(PlayerID.PLAYER_1, cell, 8, range); + Bomb bomb2 = new Bomb(PlayerID.PLAYER_1, cell, 6, range); + Bomb bomb3 = new Bomb(PlayerID.PLAYER_1, cell, 2, range); + Bomb bomb4 = new Bomb(PlayerID.PLAYER_1, cell, 1, range); + + Assert.assertEquals(ExplosionPainter.byteForBomb(bomb1), (byte) 21); // white + Assert.assertEquals(ExplosionPainter.byteForBomb(bomb2), (byte) 20); // black + Assert.assertEquals(ExplosionPainter.byteForBomb(bomb3), (byte) 21); // white + Assert.assertEquals(ExplosionPainter.byteForBomb(bomb4), (byte) 21); // white + } + + @Test + public void byteForBlastTest() { + Assert.assertEquals((byte) 15,ExplosionPainter.byteForBlast(true,true,true,true)); // NESW + Assert.assertEquals((byte) 0,ExplosionPainter.byteForBlast(false,false,false,false)); // nesw + Assert.assertEquals((byte) 13,ExplosionPainter.byteForBlast(true,true,false,true)); // NEsW + Assert.assertEquals((byte) 4,ExplosionPainter.byteForBlast(false,true,false,false)); // nEsw + } +} \ No newline at end of file diff --git a/test/ch/epfl/xblast/painter/PlayerPainterTest.java b/test/ch/epfl/xblast/painter/PlayerPainterTest.java new file mode 100644 index 0000000..32b14a9 --- /dev/null +++ b/test/ch/epfl/xblast/painter/PlayerPainterTest.java @@ -0,0 +1,61 @@ +package ch.epfl.xblast.painter; + +import ch.epfl.cs108.Sq; +import ch.epfl.xblast.*; +import ch.epfl.xblast.server.*; +import ch.epfl.xblast.server.painter.*; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Timothée Floure (257420) + */ +public class PlayerPainterTest { + + private Player playerGenerator(PlayerID id,Direction direction,Player.LifeState.State state,SubCell position,int lives) { + Sq lifeStates = Sq.constant(new Player.LifeState(lives, state)); + Sq directedPositions = Player.DirectedPosition.stopped( + new Player.DirectedPosition(position,direction) + ); + // id, lifeStates, DirectedPositions, MaxBombs, BombRange + return new Player(id, lifeStates, directedPositions, 1, 1); + } + + @Test + public void byteForPlayerTest() { + SubCell position = new SubCell(1,1); + + // Build the players + Player player1 = playerGenerator(PlayerID.PLAYER_1, + Direction.S, + Player.LifeState.State.VULNERABLE, + position, + 5 //lives + ); + Player player2 = playerGenerator(PlayerID.PLAYER_2, + Direction.S, + Player.LifeState.State.DYING, + position, + 1 //lives + ); + Player player3 = playerGenerator(PlayerID.PLAYER_3, + Direction.N, + Player.LifeState.State.VULNERABLE, + position, + 5 // lives + ); + Player player4 = playerGenerator(PlayerID.PLAYER_4, + Direction.E, + Player.LifeState.State.VULNERABLE, + position, + 5 // lives + ); + + // Assert + Assert.assertEquals((byte) 7,PlayerPainter.byteForPlayer(player1,1)); // P1 S1 + Assert.assertEquals((byte) 32,PlayerPainter.byteForPlayer(player2,1)); // P2 losing life + Assert.assertEquals((byte) 40,PlayerPainter.byteForPlayer(player3,2)); // P3 N0 + Assert.assertEquals((byte) 65,PlayerPainter.byteForPlayer(player4,3)); // P4 E2 + } +} \ No newline at end of file -- cgit v1.2.3