aboutsummaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/xblast/Cell.java12
-rw-r--r--src/ch/epfl/xblast/Lists.java118
-rw-r--r--src/ch/epfl/xblast/client/painter/TimeLinePainter.java5
-rw-r--r--src/ch/epfl/xblast/server/Block.java14
-rw-r--r--src/ch/epfl/xblast/server/GameStateSerializer.java4
5 files changed, 89 insertions, 64 deletions
diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java
index 6874b54..3f41e5c 100644
--- a/src/ch/epfl/xblast/Cell.java
+++ b/src/ch/epfl/xblast/Cell.java
@@ -30,12 +30,12 @@ public final class Cell implements Comparable<Cell> {
30 /** 30 /**
31 * The list of the board's Cell-s, major-ordered. 31 * The list of the board's Cell-s, major-ordered.
32 */ 32 */
33 public static final List<Cell> ROW_MAJOR_ORDER = Collections.unmodifiableList(rowMajorOrder()); 33 public static final List<Cell> ROW_MAJOR_ORDER = rowMajorOrder();
34 34
35 /** 35 /**
36 * The list of the board's Cell-s, spiral-ordered. 36 * The list of the board's Cell-s, spiral-ordered.
37 */ 37 */
38 public static final List<Cell> SPIRAL_ORDER = Collections.unmodifiableList(spiralOrder()); 38 public static final List<Cell> SPIRAL_ORDER = spiralOrder();
39 39
40 /** 40 /**
41 * The coordinates of the Cell. 41 * The coordinates of the Cell.
@@ -69,14 +69,14 @@ public final class Cell implements Comparable<Cell> {
69 * 69 *
70 * @return the list of Cell-s 70 * @return the list of Cell-s
71 */ 71 */
72 private static ArrayList<Cell> rowMajorOrder() { 72 private static List<Cell> rowMajorOrder() {
73 ArrayList<Cell> list = new ArrayList<>(COUNT); 73 ArrayList<Cell> list = new ArrayList<>(COUNT);
74 74
75 for (int row = 0; row < ROWS; ++row) 75 for (int row = 0; row < ROWS; ++row)
76 for (int col = 0; col < COLUMNS; ++col) 76 for (int col = 0; col < COLUMNS; ++col)
77 list.add(new Cell(col, row)); 77 list.add(new Cell(col, row));
78 78
79 return list; 79 return Collections.unmodifiableList(list);
80 } 80 }
81 81
82 /** 82 /**
@@ -84,7 +84,7 @@ public final class Cell implements Comparable<Cell> {
84 * 84 *
85 * @return the list of Cell-s 85 * @return the list of Cell-s
86 */ 86 */
87 private static ArrayList<Cell> spiralOrder() { 87 private static List<Cell> spiralOrder() {
88 ArrayList<Cell> list = new ArrayList<>(COUNT); 88 ArrayList<Cell> list = new ArrayList<>(COUNT);
89 ArrayList<Integer> ix = range(0, COLUMNS, 1); 89 ArrayList<Integer> ix = range(0, COLUMNS, 1);
90 ArrayList<Integer> iy = range(0, ROWS, 1); 90 ArrayList<Integer> iy = range(0, ROWS, 1);
@@ -101,7 +101,7 @@ public final class Cell implements Comparable<Cell> {
101 horizontal = !horizontal; 101 horizontal = !horizontal;
102 } 102 }
103 103
104 return list; 104 return Collections.unmodifiableList(list);
105 } 105 }
106 106
107 /** 107 /**
diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java
index bf0cafa..777fab2 100644
--- a/src/ch/epfl/xblast/Lists.java
+++ b/src/ch/epfl/xblast/Lists.java
@@ -37,33 +37,27 @@ public final class Lists {
37 } 37 }
38 38
39 /** 39 /**
40 * Returns a symmetric version of the list, without repeating the last element of the input list. 40 * Returns a copy of the given list with element e prepended.
41 * For instance, mirrored([kay]) will return [kayak].
42 * 41 *
43 * @param l the input list 42 * @param l the list
43 * @param e the element to prepend
44 * @param <T> the type of the list's elements 44 * @param <T> the type of the list's elements
45 * @return the mirrored list 45 * @return a copy of the list with the element prepended
46 * @throws IllegalArgumentException if the given list is empty
47 */ 46 */
48 public static <T> List<T> mirrored(List<T> l) { 47 public static <T> List<T> prepended(List<T> l, T e) {
49 if (l == null || l.size() == 0) throw new IllegalArgumentException(); 48 return Lists.inserted(l, 0, e);
50
51 return Stream
52 .concat(l.stream(), Lists.reversed(l).stream().skip(1))
53 .collect(Collectors.toList());
54 } 49 }
55 50
56 /** 51 /**
57 * Returns a reversed copy of the given list, leaving the original one unmodified. 52 * Returns a copy of the given list with element e appended.
58 * 53 *
59 * @param l the list to reverse 54 * @param l the list
55 * @param e the element to append
60 * @param <T> the type of the list's elements 56 * @param <T> the type of the list's elements
61 * @return a reversed copy of the list 57 * @return a copy of the list with the element appended
62 */ 58 */
63 private static <T> List<T> reversed(List<T> l) { 59 public static <T> List<T> appended(List<T> l, T e) {
64 List<T> r = new ArrayList<>(l); 60 return Lists.inserted(l, l.size(), e);
65 Collections.reverse(r);
66 return r;
67 } 61 }
68 62
69 /** 63 /**
@@ -89,46 +83,24 @@ public final class Lists {
89 * @return a copy of the list with the element inserted at start and end 83 * @return a copy of the list with the element inserted at start and end
90 */ 84 */
91 public static <T> List<T> surrounded(List<T> l, T e) { 85 public static <T> List<T> surrounded(List<T> l, T e) {
92 return Lists.inserted(Lists.inserted(l, 0, e), l.size() + 1, e); 86 return Lists.appended(Lists.prepended(l, e), e);
93 } 87 }
94 88
95 /** 89 /**
96 * Returns a copy of the given list with element e inserted at index i. 90 * Returns a symmetric version of the list, without repeating the last element of the input list.
97 * 91 * For instance, mirrored([kay]) will return [kayak].
98 * @param l the list
99 * @param i the insertion index
100 * @param e the element to insert
101 * @param <T> the type of the list's elements
102 * @return a copy of the list with the element inserted
103 */
104 public static <T> List<T> inserted(List<T> l, int i, T e) {
105 List<T> r = new LinkedList<>(l);
106 r.add(i, e);
107 return r;
108 }
109
110 /**
111 * Returns a copy of the given list with element e prepended.
112 * 92 *
113 * @param l the list 93 * @param l the input list
114 * @param e the element to prepend
115 * @param <T> the type of the list's elements 94 * @param <T> the type of the list's elements
116 * @return a copy of the list with the element prepended 95 * @return the mirrored list
96 * @throws IllegalArgumentException if the given list is empty
117 */ 97 */
118 public static <T> List<T> prepended(List<T> l, T e) { 98 public static <T> List<T> mirrored(List<T> l) {
119 return Lists.inserted(l, 0, e); 99 if (l == null || l.size() == 0) throw new IllegalArgumentException();
120 }
121 100
122 /** 101 return Stream
123 * Returns a copy of the given list with element e appended. 102 .concat(l.stream(), Lists.reversed(l).stream().skip(1))
124 * 103 .collect(Collectors.toList());
125 * @param l the list
126 * @param e the element to append
127 * @param <T> the type of the list's elements
128 * @return a copy of the list with the element appended
129 */
130 public static <T> List<T> appended(List<T> l, T e) {
131 return Lists.inserted(l, l.size(), e);
132 } 104 }
133 105
134 /** 106 /**
@@ -173,16 +145,56 @@ public final class Lists {
173 * @param lists the lists to concatenate 145 * @param lists the lists to concatenate
174 * @param <T> the type of the list's elements 146 * @param <T> the type of the list's elements
175 * @return the list result of the concatenation 147 * @return the list result of the concatenation
148 * @deprecated Use List<T> concatenated(List<List<T>> lists) instead to avoid parametrized varargs.
176 */ 149 */
177 public static <T> List<T> concatenated(List<T>... lists) { 150 public static <T> List<T> concatenated(List<T>... lists) {
151 return concatenated(Arrays.asList(lists));
152 }
153
154 /**
155 * Returns an immutable copy of the given lists, concatenated.
156 *
157 * @param lists the lists to concatenate
158 * @param <T> the type of the list's elements
159 * @return the list result of the concatenation
160 */
161 public static <T> List<T> concatenated(List<List<T>> lists) {
178 return Collections.unmodifiableList( 162 return Collections.unmodifiableList(
179 Stream.of(lists) 163 lists.stream()
180 .map(List::stream) 164 .map(List::stream)
181 .reduce(Stream::concat).orElse(Stream.empty()) 165 .reduce(Stream::concat).orElseGet(Stream::empty)
182 .collect(Collectors.toList())); 166 .collect(Collectors.toList()));
183 } 167 }
184 168
185 /** 169 /**
170 * Returns a reversed copy of the given list, leaving the original one unmodified.
171 *
172 * @param l the list to reverse
173 * @param <T> the type of the list's elements
174 * @return a reversed copy of the list
175 */
176 private static <T> List<T> reversed(List<T> l) {
177 List<T> r = new ArrayList<>(l);
178 Collections.reverse(r);
179 return r;
180 }
181
182 /**
183 * Returns a copy of the given list with element e inserted at index i.
184 *
185 * @param l the list
186 * @param i the insertion index
187 * @param e the element to insert
188 * @param <T> the type of the list's elements
189 * @return a copy of the list with the element inserted