diff options
author | Pacien | 2016-03-01 18:45:54 +0100 |
---|---|---|
committer | Pacien | 2016-03-01 18:45:54 +0100 |
commit | 264fec2ec938a6c525ca6f569692118a8216ca19 (patch) | |
tree | 0cf7fe569b6de16c4bd4dc8776fb82dfeeda312e /src/ch/epfl | |
parent | 45880b06c053e75e1778a598245015c65440a5bc (diff) | |
parent | cc88afb9fe448b759966e94b07692fb6a154b2bf (diff) | |
download | xblast-264fec2ec938a6c525ca6f569692118a8216ca19.tar.gz |
Merge branch '02_board' into 'master'
02_board
02 board
See merge request !1
Diffstat (limited to 'src/ch/epfl')
-rw-r--r-- | src/ch/epfl/xblast/Direction.java | 2 | ||||
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 47 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Block.java | 58 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Board.java | 145 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Ticks.java | 41 |
5 files changed, 292 insertions, 1 deletions
diff --git a/src/ch/epfl/xblast/Direction.java b/src/ch/epfl/xblast/Direction.java index 700f9be..030038b 100644 --- a/src/ch/epfl/xblast/Direction.java +++ b/src/ch/epfl/xblast/Direction.java | |||
@@ -2,7 +2,7 @@ package ch.epfl.xblast; | |||
2 | 2 | ||
3 | /** | 3 | /** |
4 | * A Direction. | 4 | * A Direction. |
5 | * | 5 | * |
6 | * @author Pacien TRAN-GIRARD (261948) | 6 | * @author Pacien TRAN-GIRARD (261948) |
7 | * @author Timothée FLOURE (257420) | 7 | * @author Timothée FLOURE (257420) |
8 | */ | 8 | */ |
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java new file mode 100644 index 0000000..51e76b0 --- /dev/null +++ b/src/ch/epfl/xblast/Lists.java | |||
@@ -0,0 +1,47 @@ | |||
1 | package ch.epfl.xblast; | ||
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.Collections; | ||
5 | import java.util.List; | ||
6 | import java.util.stream.Collectors; | ||
7 | import java.util.stream.Stream; | ||
8 | |||
9 | /** | ||
10 | * Lists utility class providing common operations on lists. | ||
11 | * | ||
12 | * @author Pacien TRAN-GIRARD (261948) | ||
13 | * @author Timothée FLOURE (257420) | ||
14 | */ | ||
15 | public final class Lists { | ||
16 | |||
17 | /** | ||
18 | * Returns a reversed copy of the given list, leaving the original one unmodified. | ||
19 | * | ||
20 | * @param l the list to reverse | ||
21 | * @param <T> the type of the list's elements | ||
22 | * @return a reversed copy of the list. | ||
23 | */ | ||
24 | public static <T> List<T> reversed(List<T> l) { | ||
25 | List<T> r = new ArrayList<>(l); | ||
26 | Collections.reverse(r); | ||
27 | return r; | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Returns a symmetric version of the list, without repeating the last element of the input list. | ||
32 | * For instance, mirrored([kay]) will return [kayak]. | ||
33 | * | ||
34 | * @param l the input list | ||
35 | * @param <T> the type of the list's elements | ||
36 | * @return the mirrored list | ||
37 | * @throws IllegalArgumentException if the given list is empty | ||
38 | */ | ||
39 | public static <T> List<T> mirrored(List<T> l) { | ||
40 | if (l == null || l.size() == 0) throw new IllegalArgumentException(); | ||
41 | |||
42 | return Stream | ||
43 | .concat(l.stream(), Lists.reversed(l).stream().skip(1)) | ||
44 | .collect(Collectors.toList()); | ||
45 | } | ||
46 | |||
47 | } | ||
diff --git a/src/ch/epfl/xblast/server/Block.java b/src/ch/epfl/xblast/server/Block.java new file mode 100644 index 0000000..409f68e --- /dev/null +++ b/src/ch/epfl/xblast/server/Block.java | |||
@@ -0,0 +1,58 @@ | |||
1 | package ch.epfl.xblast.server; | ||
2 | |||
3 | /** | ||
4 | * A Block. | ||
5 | * | ||
6 | * @author Pacien TRAN-GIRARD (261948) | ||
7 | * @author Timothée FLOURE (257420) | ||
8 | */ | ||
9 | public enum Block { | ||
10 | |||
11 | /** | ||
12 | * 'Free' block, no wall. | ||
13 | */ | ||
14 | FREE, | ||
15 | |||
16 | /** | ||
17 | * Indestructible block. | ||
18 | */ | ||
19 | INDESTRUCTIBLE_WALL, | ||
20 | |||
21 | /** | ||
22 | * Destructible block. | ||
23 | */ | ||
24 | DESTRUCTIBLE_WALL, | ||
25 | |||
26 | /** | ||
27 | * Crumbling Wall. | ||
28 | */ | ||
29 | CRUMBLING_WALL; | ||
30 | |||
31 | /** | ||
32 | * Returns T(this block is free). | ||
33 | * | ||
34 | * @return T(this block is free) | ||
35 | */ | ||
36 | public boolean isFree() { | ||
37 | return this == FREE; | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Returns T(this block can host a player). | ||
42 | * | ||
43 | * @return T(this block can host a player) | ||
44 | */ | ||
45 | public boolean canHostPlayer() { | ||
46 | return this.isFree(); | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Returns T(this block cast a shadow). | ||
51 | * | ||
52 | * @return T(this block cast a shadow) | ||
53 | */ | ||
54 | public boolean castsShadow() { | ||
55 | return this == INDESTRUCTIBLE_WALL || this == DESTRUCTIBLE_WALL || this == CRUMBLING_WALL; | ||
56 | } | ||
57 | |||
58 | } | ||
diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java new file mode 100644 index 0000000..5e03671 --- /dev/null +++ b/src/ch/epfl/xblast/server/Board.java | |||
@@ -0,0 +1,145 @@ | |||
1 | package ch.epfl.xblast.server; | ||
2 | |||
3 | import ch.epfl.cs108.Sq; | ||
4 | import ch.epfl.xblast.Cell; | ||
5 | import ch.epfl.xblast.Lists; | ||
6 | |||
7 | import java.util.ArrayList; | ||
8 | import java.util.List; | ||
9 | |||
10 | /** | ||
11 | * A two-dimensional Board in which the game takes place. | ||
12 | * | ||
13 | * @author Pacien TRAN-GIRARD (261948) | ||
14 | * @author Timothée FLOURE (257420) | ||
15 | */ | ||
16 | public final class Board { | ||
17 | |||
18 | private static final int BLOCKS_LIST_SIZE = 195; | ||
19 | private static final int BOARD_ROWS = 13; | ||
20 | private static final int BOARD_COLUMNS = 15; | ||
21 | private static final int INNER_BOARD_ROWS = BOARD_ROWS - 2; | ||
22 | private static final int INNER_BOARD_COLUMNS = BOARD_COLUMNS - 2; | ||
23 | private static final int QUADRANT_ROWS = 6; | ||
24 | private static final int QUADRANT_COLUMNS = 7; | ||
25 | |||
26 | /** | ||
27 | * Throw an exception if the matrix does not have the given number of rows/columns. | ||
28 | * | ||
29 | * @param matrix the tested matrix | ||
30 | * @param rows the expected number of rows | ||
31 | * @param columns the expected number of columns | ||
32 | * @throws IllegalArgumentException if the matrix does not comply with the given sizes. | ||
33 | */ | ||
34 | private static void checkBlockMatrix(List<List<Block>> matrix, int rows, int columns) { | ||
35 | if (matrix == null || matrix.size() != rows) | ||
36 | throw new IllegalArgumentException(); | ||
37 | |||
38 | for (int i = 0; i < rows; i++) | ||
39 | if (matrix.get(i).size() != columns) | ||
40 | throw new IllegalArgumentException(); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * List containing all the blocks of the board. | ||
45 | */ | ||
46 | private List<Sq<Block>> blocks; | ||
47 | |||
48 | /** | ||
49 | * Instantiates a new Board with the given sequence of blocks. | ||
50 | * | ||
51 | * @param blocks sequence containing all the blocks of the Boards | ||
52 | * @throws IllegalArgumentException if the blocks is not composed of BLOC_LIST_SIZE elements | ||
53 | */ | ||
54 | public Board(List<Sq<Block>> blocks) { | ||
55 | if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) | ||
56 | throw new IllegalArgumentException(); | ||
57 | |||
58 | this.blocks = blocks; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Build a new Board with the given Matrix. | ||
63 | * | ||
64 | * @param rows list containing all the rows | ||
65 | * @return a new Board built with given rows | ||
66 | * @throws IllegalArgumentException if rows is not BOARD_ROWS * BOARD_COLUMNS | ||
67 | */ | ||
68 | public static Board ofRows(List<List<Block>> rows) { | ||
69 | checkBlockMatrix(rows, BOARD_ROWS, BOARD_COLUMNS); | ||
70 | List<Sq<Block>> blocksSequence = new ArrayList<>(); | ||
71 | |||
72 | for (List<Block> row : rows) | ||
73 | for (Block aRow : row) | ||
74 | blocksSequence.add(Sq.constant(aRow)); | ||
75 | |||
76 | return new Board(blocksSequence); | ||
77 | } | ||
78 | |||
79 | /** | ||
80 | * Build a walled board filled with the given inner rows | ||
81 | * | ||
82 | * @param innerBlocks lists of the internal rows | ||
83 | * @return a new walled board filled with the given rows | ||
84 | * @throws IllegalArgumentException if innerBlocks is not INNER_BOARD_ROWS * INNER_BOARD_COLUMNS | ||
85 | */ | ||
86 | public static Board ofInnerBlocksWalled(List<List<Block>> innerBlocks) { | ||
87 | checkBlockMatrix(innerBlocks, INNER_BOARD_ROWS, INNER_BOARD_COLUMNS); | ||
88 | List<List<Block>> rowsList = new ArrayList<>(); | ||
89 | List<Block> wallLine = new ArrayList<>(); | ||
90 | |||
91 | for (int i = 0; i < BOARD_COLUMNS; i++) | ||
92 | wallLine.add(Block.INDESTRUCTIBLE_WALL); | ||
93 | |||
94 | for (List<Block> row : innerBlocks) { | ||
95 | row.add(0, Block.INDESTRUCTIBLE_WALL); | ||
96 | row.add(Block.INDESTRUCTIBLE_WALL); | ||
97 | |||
98 | rowsList.add(row); | ||
99 | } | ||
100 | |||
101 | rowsList.add(0, wallLine); | ||
102 | rowsList.add(wallLine); | ||
103 | |||
104 | return ofRows(rowsList); | ||