aboutsummaryrefslogtreecommitdiff
path: root/src/ch
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-02-22 14:45:17 +0100
committerPacien TRAN-GIRARD2016-02-22 14:45:17 +0100
commit9809f6e226d593c1e717b8d6ab576d986da9cbe3 (patch)
treeafbbd45f7a1674747a7971e59b2018ab883827c6 /src/ch
parentd343ce4619f25610957573ab1e9d3618b3f606a3 (diff)
downloadxblast-9809f6e226d593c1e717b8d6ab576d986da9cbe3.tar.gz
Implement the Cell class
Diffstat (limited to 'src/ch')
-rw-r--r--src/ch/epfl/xblast/Cell.java196
1 files changed, 196 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java
new file mode 100644
index 0000000..d5b15e7
--- /dev/null
+++ b/src/ch/epfl/xblast/Cell.java
@@ -0,0 +1,196 @@
1package ch.epfl.xblast;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.List;
6
7/**
8 * A Cell.
9 *
10 * @author Pacien TRAN-GIRARD (261948)
11 */
12public final class Cell {
13
14 /**
15 * The width of the board (number of columns).
16 */
17 public static final int COLUMNS = 15;
18
19 /**
20 * The height of the board (number of rows).
21 */
22 public static final int ROWS = 13;
23
24 /**
25 * The total number of Cell-s of the board.
26 */
27 public static final int COUNT = COLUMNS * ROWS;
28
29 /**
30 * The list of the board's Cell's, major-ordered.
31 */
32 public static final List<Cell> ROW_MAJOR_ORDER = Collections.unmodifiableList(rowMajorOrder());
33
34 /**
35 * The list of the board's Cell-s, spiral-ordered.
36 */
37 public static final List<Cell> SPIRAL_ORDER = Collections.unmodifiableList(spiralOrder());
38
39 /**
40 * Builds a major-ordered list of Cell-s.
41 *
42 * @return the list of Cell-s
43 */
44 private static ArrayList<Cell> rowMajorOrder() {
45 ArrayList<Cell> list = new ArrayList<>(COUNT);
46
47 for (int row = 0; row < ROWS; ++row)
48 for (int col = 0; col < COLUMNS; ++col)
49 list.add(new Cell(col, row));
50
51 return list;
52 }
53
54 /**
55 * Builds a spiral-ordered list of Cell-s.
56 *
57 * @return the list of Cell-s
58 */
59 private static ArrayList<Cell> spiralOrder() {
60 ArrayList<Cell> list = new ArrayList<>(COUNT);
61 ArrayList<Integer> ix = range(0, COLUMNS, 1);
62 ArrayList<Integer> iy = range(0, ROWS, 1);
63 boolean horizontal = true;
64
65 while (!ix.isEmpty() && !iy.isEmpty()) {
66 ArrayList<Integer> i1 = horizontal ? ix : iy;
67 ArrayList<Integer> i2 = horizontal ? iy : ix;
68 int c2 = i2.remove(0);
69 for (int c1 : i1)
70 list.add(horizontal ? new Cell(c1, c2) : new Cell(c2, c1));
71
72 Collections.reverse(i1);
73 horizontal = !horizontal;
74 }
75
76 return list;
77 }
78
79 /**
80 * Creates a list containing an arithmetic progression.
81 *
82 * @param from the starting value
83 * @param to the boundary value
84 * @param step the arithmetic step
85 * @return the arithmetic progression
86 */
87 private static ArrayList<Integer> range(int from, int to, int step) {
88 int n = (to - from) / step;
89 ArrayList<Integer> list = new ArrayList<>(n);
90
91 for (int i = 0; i < n; ++i)
92 list.add(from + (i * step));
93
94 return list;
95
96 }
97
98 /**
99 * Normalizes the given number (using the integer floor modulus).
100 *
101 * @param max the maximum (the divisor)
102 * @param n the number to normalize (the dividend)
103 * @return the normalized value
104 */
105 private static int normalize(int max, int n) {
106 return Math.floorMod(n, max);
107 }
108
109 /**
110 * The coordinates of the Cell.
111 */
112 private final int x, y;
113
114 /**
115 * Instantiates a new Cell with the given coordinates.
116 *
117 * @param x the x-coordinate
118 * @param y the y-coordinate
119 */
120 public Cell(int x, int y) {
121 this.x = normalize(COLUMNS, x);
122 this.y = normalize(ROWS, y);
123 }
124
125 /**
126 * Returns the normalized x-coordinate of the Cell.
127 *
128 * @return the x-coordinate
129 */
130 public int x() {
131 return this.x;
132 }
133
134 /**
135 * Returns the normalized y-coordinate of the Cell.
136 *
137 * @return the y-coordinate
138 */
139 public int y() {
140 return this.y;
141 }
142
143 /**
144 * Returns the index of the Cell (major ordered).
145 *
146 * @return the index of the Cell
147 */
148 public int rowMajorIndex() {
149 return this.x * ROWS + this.y;
150 }
151
152 /**
153 * Returns the neighboring Cell at the given Direction.
154 *
155 * @param dir the Direction
156 * @return the neighboring Cell
157 */
158 public Cell neighbor(Direction dir) {
159 switch (dir) {
160 case N:
161 return new Cell(this.x, this.y - 1);
162 case S:
163 return new Cell(this.x, this.y + 1);
164 case E:
165 return new Cell(this.x - 1, this.y);
166 case W:
167 return new Cell(this.x + 1, this.y);
168 default:
169 return null;
170 }
171 }
172
173 /**
174 * Returns T(the given Object is equal to this Cell (have the same coordinates)).
175 *
176 * @param that the Object to compare against
177 * @return T(the given Object is equal to this Cell)
178 */
179 @Override
180 public boolean equals(Object that) {
181 if (!super.equals(that)) return false;
182 if (!(that instanceof Cell)) return false;
183 return (((Cell) that).x == this.x && ((Cell) that).y == this.y);
184 }
185
186 /**
187 * Returns a String representation of the coordinates of the Cell.
188 *
189 * @return a String representation of the coordinates of the Cell.
190 */
191 @Override
192 public String toString() {
193 return String.format("(%d,%d)", this.x, this.y);
194 }
195
196}