-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.java
288 lines (247 loc) · 9.01 KB
/
Grid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* The coordinate are measured as usual:
* +-------+-------+
* | (0,1) | (0,1) |
* |-------+-------+
* | (1,0) | (1,1) |
* +-------+-------+
*/
public class Grid implements Cloneable {
public static final int UP = 0;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
private static final char VERTICAL_WALL = '|';
private static final char HORIZONTAL_WALL = '-';
private static final char CORNER = '+';
/** The Strings printed in the cells */
private String[][] content;
/** The array of "activation" of cells */
private boolean[][] on;
/** The height and the width of the grid (redundant) */
private int height, width;
/** The right wall character of the cells */
private char[][] rightWalls;
/** Leftmosts walls characters */
private char[] leftWalls;
/** The down wall character of the cells */
private char[][] downWalls;
/** Upper walls characters */
private char[] upWalls;
public Grid(boolean[][] on) throws GridException {
this.on = on;
height = on.length;
if (height == 0)
throw new GridException("Height cannot be zero");
width = on[0].length;
if (width == 0)
throw new GridException("Width cannot be zero");
content = new String[height][];
rightWalls = new char[height][];
leftWalls = new char[height];
downWalls = new char[height][];
upWalls = new char[width];
for (int i = 0; i < height; i++) {
content[i] = new String[width];
rightWalls[i] = new char[width];
downWalls[i] = new char[width];
for (int j = 0; j < width; j++) {
content[i][j] = "";
rightWalls[i][j] = VERTICAL_WALL;
downWalls[i][j] = HORIZONTAL_WALL;
}
leftWalls[i] = VERTICAL_WALL;
}
for (int j = 0; j < width; j++)
upWalls[j] = HORIZONTAL_WALL;
}
/**
* Sets the content of a cell
* @param i the row of the cell
* @param j the column of the cell
* @param s the content of the cell
* @throws GridException
*/
public void setContent(int i, int j, String s) throws GridException {
check(i, j);
if (s == null)
throw new GridException("The content of a cell cannot be null");
content[i][j] = s;
}
/**
* Sets a given wall to some character
* @param i the row of the cell
* @param j the column of the cell
* @param orientation the direction of the wall we want to set, is one of
* Grid.UP, Grid.DOWN, Grid.LEFT, Grid.RIGHT
* @param c the character to set
* @throws GridException
*/
public void setWall(int i, int j, int orientation, char c)
throws GridException {
check(i, j);
switch (orientation) {
case UP:
if (i == 0)
upWalls[j] = c;
else
downWalls[i - 1][j] = c;
break;
case DOWN:
downWalls[i][j] = c;
break;
case LEFT:
if (j == 0)
leftWalls[i] = c;
else
rightWalls[i][j - 1] = c;
break;
case RIGHT:
rightWalls[i][j] = c;
break;
}
}
/**
* Sets the activation of a cell
* @param i the row of the cell
* @param j the column of the cell
* @param activated wether or not the cell is activated
* @throws GridException
*/
public void setActivated(int i, int j, boolean activated)
throws GridException {
if (i < 0 || i >= height)
throw new GridException("Height index out of bounds");
if (j < 0 || j >= width)
throw new GridException("Width index out of bounds");
on[i][j] = activated;
}
/**
* Checks if cell positions match the grid
* @param i the tow index
* @param j the column index
* @throws GridException
*/
private void check(int i, int j) throws GridException {
if (i < 0 || i >= height)
throw new GridException("Height index out of bounds");
if (j < 0 || j >= width)
throw new GridException("Width index out of bounds");
if (!on[i][j])
throw new GridException("This cell is not active");
}
public String toString() {
// recompute columns width each time.
// change ?
int[] colWidths = new int[width];
for (int j = 0; j < width; j++) {
colWidths[j] = 1;
for (int i = 0; i < height; i++) {
int l = content[i][j].length();
if (on[i][j] && l > colWidths[j])
colWidths[j] = l;
}
}
String s = buildUpperLine(colWidths) + "\n";
for (int i = 0; i < height - 1; i++) {
s += buildDataLine(i, colWidths) + "\n";
s += buildDownLine(i, colWidths) + "\n";
}
s += buildDataLine(height-1, colWidths) + "\n";
s += buildDownLine(height-1, colWidths);
return s;
}
/**
* Constructs the upper line of the grid
* @param colWidths the array containing the widths of all columns
* @return the line
*/
private String buildUpperLine(int[] colWidths) {
String s = "";
s += on[0][0] ? CORNER : " ";
for (int j = 0; j < width; j++) {
if (on[0][j]) {
s += "" + HORIZONTAL_WALL + upWalls[j];
s = dumbReapeat(s, HORIZONTAL_WALL, colWidths[j]);
s += CORNER;
} else if (j + 1 < width && on[0][j+1]) {
s = dumbReapeat(s, ' ', colWidths[j] + 2);
s += CORNER;
} else
s = dumbReapeat(s, ' ', colWidths[j] + 3);
}
return s;
}
/**
* Constructs the i-th "data line", the line that contains the actual info
* @param i the index of line to be built
* @param colWidths the array containing the widths of all columns
* @return the line
*/
private String buildDataLine(int i, int[] colWidths) {
String s = "";
s += on[i][0] ? leftWalls[i] : " ";
for (int j = 0; j < width; j++) {
if (on[i][j]) {
s += " " + content[i][j];
s = dumbReapeat(s, ' ', colWidths[j] - content[i][j].length() +1);
s += rightWalls[i][j];
} else if (j + 1 < width && on[i][j+1]) {
s = dumbReapeat(s, ' ', colWidths[j] + 2);
s += rightWalls[i][j];
} else
s = dumbReapeat(s, ' ', colWidths[j] + 3);
}
return s;
}
/**
* Constructs the i-th "data line", the line that contains the actual info
* @param i the index of line to be built
* @param colWidths the array containing the widths of all columns
* @return the line
*/
private String buildDownLine(int i, int[] colWidths) {
String s = "";
s += on[i][0] || (i + 1 < height && on[i+1][0]) ? CORNER : " ";
for (int j = 0; j < width; j++) {
boolean downIsOn = i + 1 < height && on[i + 1][j];
boolean rightIsOn = j + 1 < width && on[i][j + 1];
boolean downRightIsOn = i + 1 < height && j + 1 < width &&
on[i + 1][j + 1];
if (on[i][j] || downIsOn) {
s += "" + HORIZONTAL_WALL + downWalls[i][j];
s = dumbReapeat(s, HORIZONTAL_WALL, colWidths[j]);
s += CORNER;
} else if (rightIsOn || downRightIsOn) {
s = dumbReapeat(s, ' ', colWidths[j] + 2);
s += CORNER;
} else
s = dumbReapeat(s, ' ', colWidths[j] + 3);
}
return s;
}
private static String dumbReapeat(String s, char c, int n) {
for (int k = 0; k < n; k++)
s += c;
return s;
}
public Grid clone() {
Grid g = null;
try {
g = (Grid) super.clone();
g.content = new String[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
g.content[i][j] = this.content[i][j];
// do not need deep copy:
g.on = (boolean[][]) this.on.clone();
g.downWalls = (char[][]) this.downWalls.clone();
g.upWalls = (char[]) this.upWalls.clone();
g.leftWalls = (char[]) this.leftWalls.clone();
g.rightWalls = (char[][]) this.rightWalls.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError("Could not clone Grid");
}
return g;
}
}