aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-06 19:34:37 +0200
committerPacien TRAN-GIRARD2016-05-06 19:34:37 +0200
commit31b29f7418540f956257894560a09f6ccd6ee091 (patch)
tree76905e4f4f9c2a8f31755fe2ed4ee89a26f33d9e /src/ch/epfl
parentcba582ddc6a77e0d1a2725c3f7ca079e66479888 (diff)
downloadxblast-31b29f7418540f956257894560a09f6ccd6ee091.tar.gz
Refactor default level building
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/PlayerID.java17
-rw-r--r--src/ch/epfl/xblast/server/Board.java48
-rw-r--r--src/ch/epfl/xblast/server/GameState.java2
-rw-r--r--src/ch/epfl/xblast/server/Level.java104
-rw-r--r--src/ch/epfl/xblast/server/Player.java95
-rw-r--r--src/ch/epfl/xblast/server/painter/BlockImage.java29
-rw-r--r--src/ch/epfl/xblast/server/painter/BoardPainter.java5
7 files changed, 156 insertions, 144 deletions
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 {
11 PLAYER_1, 11 PLAYER_1,
12 PLAYER_2, 12 PLAYER_2,
13 PLAYER_3, 13 PLAYER_3,
14 PLAYER_4 14 PLAYER_4;
15
16 public Cell initialPosition() {
17 switch (this) {
18 case PLAYER_1:
19 return new Cell(1, 1);
20 case PLAYER_2:
21 return new Cell(13, 1);
22 case PLAYER_3:
23 return new Cell(13, 11);
24 case PLAYER_4:
25 return new Cell(1, 11);
26 default:
27 return null;
28 }
29 }
15 30
16} 31}
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;
4import ch.epfl.xblast.Cell; 4import ch.epfl.xblast.Cell;
5import ch.epfl.xblast.Lists; 5import ch.epfl.xblast.Lists;
6 6
7import java.util.Arrays;
7import java.util.Collection; 8import java.util.Collection;
8import java.util.Collections; 9import java.util.Collections;
9import java.util.List; 10import java.util.List;
@@ -17,6 +18,8 @@ import java.util.stream.Collectors;
17 */ 18 */
18public final class Board { 19public final class Board {
19 20
21 static final Board DEFAULT_BOARD = buildDefaultBoard();
22
20 /** 23 /**
21 * Distance (in SubCells) from a bomb to block a player. 24 * Distance (in SubCells) from a bomb to block a player.
22 */ 25 */
@@ -38,21 +41,22 @@ public final class Board {
38 private static final int BLOCKS_LIST_SIZE = BOARD_ROWS * BOARD_COLUMNS; 41 private static final int BLOCKS_LIST_SIZE = BOARD_ROWS * BOARD_COLUMNS;
39 42
40 /** 43 /**
41 * List containing all the blocks of the board. 44 * Build the default board of the game.
42 */
43 private final List<Sq<Block>> blocks;
44
45 /**
46 * Instantiates a new Board with the given sequence of blocks.
47 * 45 *
48 * @param blocks sequence containing all the blocks of the Boards 46 * @return the default board.
49 * @throws IllegalArgumentException if the blocks is not composed of BLOC_LIST_SIZE elements
50 */ 47 */
51 public Board(List<Sq<Block>> blocks) { 48 private static Board buildDefaultBoard() {
52 if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) 49 Block __ = Block.FREE;
53 throw new IllegalArgumentException(); 50 Block XX = Block.INDESTRUCTIBLE_WALL;
54 51 Block xx = Block.DESTRUCTIBLE_WALL;
55 this.blocks = Lists.immutableList(blocks); 52
53 return Board.ofQuadrantNWBlocksWalled(Arrays.asList(
54 Arrays.asList(__, __, __, __, __, xx, __),
55 Arrays.asList(__, XX, xx, XX, xx, XX, xx),
56 Arrays.asList(__, xx, __, __, __, xx, __),
57 Arrays.asList(xx, XX, __, XX, XX, XX, XX),
58 Arrays.asList(__, xx, __, xx, __, __, __),
59 Arrays.asList(xx, XX, xx, XX, xx, XX, __)));
56 } 60 }
57 61
58 /** 62 /**
@@ -128,6 +132,24 @@ public final class Board {
128 } 132 }
129 133
130 /** 134 /**
135 * List containing all the blocks of the board.
136 */
137 private final List<Sq<Block>> blocks;
138
139 /**
140 * Instantiates a new Board with the given sequence of blocks.
141 *
142 * @param blocks sequence containing all the blocks of the Boards
143 * @throws IllegalArgumentException if the blocks is not composed of BLOC_LIST_SIZE elements
144 */
145 public Board(List<Sq<Block>> blocks) {
146 if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE)
147 throw new IllegalArgumentException();
148
149 this.blocks = Lists.immutableList(blocks);
150 }
151
152 /**
131 * Returns the block related to the given cell. 153 * Returns the block related to the given cell.
132 * 154 *
133 * @param c cell 155 * @param c 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;
16 */ 16 */
17public final class GameState { 17public final class GameState {
18 18
19 static final GameState DEFAULT_GAME_STATE = new GameState(Board.DEFAULT_BOARD, Player.DEFAULT_PLAYERS);
20
19 private final int ticks; 21 private final int ticks;
20 private final Board board; 22 private final Board board;
21 private final List<Player> players; 23 private final List<Player> 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 @@
1package ch.epfl.xblast.server; 1package ch.epfl.xblast.server;
2 2
3import ch.epfl.xblast.*; 3import ch.epfl.xblast.server.painter.BoardPainter;
4import ch.epfl.xblast.server.painter.*;
5
6import java.util.Arrays;
7import java.util.List;
8import java.util.HashMap;
9 4
10/** 5/**
6 * A level.
7 *
8 * @author Pacien TRAN-GIRARD (261948)
11 * @author Timothée FLOURE (257420) 9 * @author Timothée FLOURE (257420)
12 */ 10 */
13public final class Level { 11public final class Level {
14 12
15 /** Players' initial parameters **/
16 private static final int PLAYER_INITIAL_LIVES = 3;
17 private static final int PLAYER_INITIAL_BOMB_MAXIMUM = 5;
18 private static final int PLAYER_INITIAL_BOMB_RANGE = 5;
19
20 /** Players' initial positions (Ugly!) **/
21 private static final Cell PLAYER_1_INITIAL_POSITION = new Cell(1,1);
22 private static final Cell PLAYER_2_INITIAL_POSITION = new Cell(13,1);
23 private static final Cell PLAYER_3_INITIAL_POSITION = new Cell(13,11);
24 private static final Cell PLAYER_4_INITIAL_POSITION = new Cell(1,11);
25
26 /** Level values **/
27 private final BoardPainter painter;
28 private final GameState initialState;
29
30 /**
31 * Build the default board of the game.
32 *
33 * @return the default board
34 */
35 private static BoardPainter buildDefaultBoardPainter() {
36 // Create the blocks map
37 HashMap<Block,BlockImage> blocksMap = new HashMap<>();
38
39 // Fill the blocks map
40 blocksMap.put(Block.FREE, BlockImage.IRON_FLOOR);
41 blocksMap.put(Block.DESTRUCTIBLE_WALL, BlockImage.EXTRA);
42 blocksMap.put(Block.CRUMBLING_WALL, BlockImage.EXTRA_O);
43 blocksMap.put(Block.INDESTRUCTIBLE_WALL, BlockImage.DARK_BLOCK);
44 blocksMap.put(Block.BONUS_BOMB, BlockImage.BONUS_BOMB);
45 blocksMap.put(Block.BONUS_RANGE, BlockImage.BONUS_RANGE);
46
47 // Create and return the board painter
48 return new BoardPainter(blocksMap, BlockImage.IRON_FLOOR_S);
49 }
50
51 /**
52 * Build the default game state of the game.
53 *
54 * @return the default game state
55 */
56 private static GameState buildDefaultGameState() {
57 return new GameState(buildDefaultBoard(), buildDefaultPlayers());
58 }
59
60 /**
61 * Build the default board of the game.
62 *
63 * @return the default board.
64 */
65 private static Board buildDefaultBoard() {
66
67 // Ugly!
68 Block __ = Block.FREE;
69 Block XX = Block.INDESTRUCTIBLE_WALL;
70 Block xx = Block.DESTRUCTIBLE_WALL;
71
72 return Board.ofQuadrantNWBlocksWalled(
73 Arrays.asList(
74 Arrays.asList(__, __, __, __, __, xx, __),
75 Arrays.asList(__, XX, xx, XX, xx, XX, xx),
76 Arrays.asList(__, xx, __, __, __, xx, __),
77 Arrays.asList(xx, XX, __, XX, XX, XX, XX),
78 Arrays.asList(__, xx, __, xx, __, __, __),
79 Arrays.asList(xx, XX, xx, XX, xx, XX, __)));
80 }
81
82 /**
83 * Build the default players of the games.
84 *
85 * @return a list of the 4 players built using the default parameters.
86 */
87 private static List<Player> buildDefaultPlayers() {