-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlither.java
More file actions
418 lines (414 loc) · 10.5 KB
/
Copy pathSlither.java
File metadata and controls
418 lines (414 loc) · 10.5 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import java.util.*;
import java.io.*;
public class Slither {
public static void main (String [] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println(introduction());
String fileName = sc.next();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String[] line = br.readLine().split(" ");
int r = Integer.parseInt(line[0]);
int c = Integer.parseInt(line[1]);
Board board = new Board(r, c);
for (int i = 0; i < r; i++) {
line = br.readLine().split(" ");
for (int j = 0; j < c; j++) {
board.lineReqs[i][j] = new LineReq(Integer.parseInt(line[j]));
}
}
br.close();
slither(sc, board);
}
static void slither(Scanner sc, Board board) {
System.out.println(instructions());
BoardLogic boardLogic = new BoardLogic(board);
while (true) {
System.out.print(board);
if (boardLogic.solved()) {
System.out.println("Congratulations! You solved it!");
break;
}
int r = sc.nextInt() - 1;
int c = sc.nextInt() - 1;
Side side = Side.translate(sc.next().charAt(0));
board.mark(r, c, side);
}
}
static String instructions() {
return
"\n<Instructions>\n" +
"Type \"N N L\", where the first two Ns represent the row and column " +
"of the cell whose adjacent wall you wish to fill in and L represents the side " +
"of the cell where you'd like to put the wall " +
"(T for top, B for bottom, L for left, and R for right).\n" +
"</Instructions>\n";
}
static String introduction() {
String s =
"Welcome to Slither!\n\nSlither is a puzzle game. " +
"If you can create a continuous loop that adheres to " +
"the numbered requirement squares seen on the board, " +
"you win!\n\n";
s +=
"For instance, the following is a winning solution because it has " +
"as many lines around each square as the number specified " +
"in the center of that square.\n\n" +
"It also contains exactly one continuous loop.\n\n";
s +=
" 1 2 \n" +
" \n" +
" +-+-+\n" +
"1 |3 3|\n" +
" +-+-+\n" +
"2 \n" +
" + + +\n\n";
s += "Please enter the name of the input file (e.g., \"Slither.in\").";
return s;
}
}
// This class represents the numerical requirement for the number of lines
// to border a particular cell.
class LineReq {
private int value;
public LineReq(int v) {
value = v;
}
public boolean hasValue() {
return value != -1;
}
public int getValue() {
return value;
}
}
class BoardLogic {
Board board;
int r, c;
BoardLogic(Board board) {
this.board = board;
this.r = board.r;
this.c = board.c;
}
/*
There are two requirements that a solved puzzle must satisfy.
1) A single looped path has been generated, with no crosses or branches
2) Each line requirement must be met.
My solution for (1) is:
for each end of a 1-unit line segment,
add both corners of the segment to a list of corners.
A list in which each corner appears 0 or 2 times guarantees
that we have a connected loop without crosses or branches.
*/
public boolean solved() {
return rightNumberOfCorners() && metLineRequirements() && justOneLoop();
}
private boolean rightNumberOfCorners() {
int[][] corners = new int[r+1][c+1];
// The value at each cell corresponds to the number of times the top-left
// corner of that cell layed at the end of a 1-unit line segment.
for (int i = 0; i <= r; i++) {
for (int j = 0; j < c; j++) {
if (board.isMarked(i, j, Side.TOP)) {
corners[i][j]++;
corners[i][j+1]++;
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j <= c; j++) {
if (board.isMarked(i, j, Side.LEFT)) {
corners[i][j]++;
corners[i+1][j]++;
}
}
}
// Now let's count our corners.
for (int i = 0; i <= r; i++) {
for (int j = 0; j <= c; j++) {
if (corners[i][j] != 0 && corners[i][j] != 2)
return false;
}
}
return true;
}
private boolean metLineRequirements() {
// Now let's make sure we've met the line requirements.
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (!board.lineReqs[i][j].hasValue())
continue;
int target = board.lineReqs[i][j].getValue();
int count = 0;
for (Side side : Side.values()) {
if (board.isMarked(i, j, side))
count++;
}
if (target != count)
return false;
}
}
return true;
}
private boolean justOneLoop() {
Trio t = null;
SEARCH:
for (int i = 0; i <= board.r; i++) {
for (int j = 0; j <= board.c; j++) {
for (Side s : Side.RELEVANT_SIDES) {
if (board.isMarked(i, j, s)) {
t = new Trio(i, j, s);
break SEARCH;
}
}
}
}
if (t == null) {
return false;
}
HashSet<Trio> hs = new HashSet<Trio>();
LinkedList<Trio> q = new LinkedList<Trio>();
q.offer(t);
hs.add(t);
while(!q.isEmpty()) {
Trio u = q.poll();
Iterator<Trio> it = board.getTouchingWalls(u).iterator();
while (it.hasNext()) {
Trio v = it.next();
if (!hs.contains(v)) {
hs.add(v);
q.add(v);
}
}
}
for (int i = 0; i <= board.r; i++) {
for (int j = 0; j <= board.c; j++) {
for (Side s : Side.RELEVANT_SIDES) {
if (board.isMarked(i, j, s) && !hs.contains(new Trio(i, j, s))) {
return false;
}
}
}
}
return true; // no line was found that wasn't touching the main loop
}
}
class Trio {
int i;
int j;
Side side;
@Override
public int hashCode() {
return i + (j * 26) + (side.ordinal() * 806); //i <= 25, j <= 30
}
public Trio (int a, int b, Side c) {
i = a;
j = b;
side = c;
}
public String toString() {
return "i: " + i + "j: " + j + "side: " + side;
}
@Override
public boolean equals(Object o) {
return hashCode() == o.hashCode();
}
}
class Board {
int r, c;
private Wall[][] walls;
LineReq[][] lineReqs;
public Board (int r, int c) {
this.r = r;
this.c = c;
walls = new Wall[r][c];
lineReqs = new LineReq[r][c];
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
walls[i][j] = new Wall();
}
public boolean isMarked(int i, int j, Side side) {
Trio equivWall = getEquivalentWall(i, j, side);
int equivI = equivWall.i;
int equivJ = equivWall.j;
Side correspondingSide = equivWall.side;
if (rowInBounds(i) && columnInBounds(j))
return walls[i][j].isMarked(side);
if (rowInBounds(equivI) && columnInBounds(equivJ)) // if they specify the left of the rightmost wall
return walls[equivI][equivJ].isMarked(correspondingSide);
return false;
}
public List<Trio> getTouchingWalls(Trio t) {
List<Trio> touchingWalls = new ArrayList<Trio>();
Trio base;
if (t.side == Side.BOTTOM || t.side == Side.RIGHT) {
base = getEquivalentWall(t.i, t.j, t.side);
} else {
base = t;
}
switch (base.side) {
case TOP:
for (int c = base.j - 1; c <= base.j + 1; c++) {
if (isMarked(base.i, c, Side.TOP)) {
touchingWalls.add(new Trio(base.i, c, Side.TOP));
}
}
for (int r = base.i - 1; r <= base.i; r++) {
for (int c = base.j; c <= base.j + 1; c++) {
if (isMarked(r, c, Side.LEFT)) {
touchingWalls.add(new Trio(r, c, Side.LEFT));
}
}
}
break;
case LEFT:
for (int r = base.i - 1; r <= base.i+1; r++) {
if (isMarked(r, base.j, Side.LEFT)) {
touchingWalls.add(new Trio(r, base.j, Side.LEFT));
}
}
for (int r = base.i; r <= base.i + 1; r++) {
for (int c = base.j - 1; c <= base.j; c++) {
if (isMarked(r, c, Side.TOP)) {
touchingWalls.add(new Trio(r, c, Side.TOP));
}
}
}
break;
default:
throw new RuntimeException("GetTouchingWalls issues");
}
return touchingWalls;
}
public Trio getEquivalentWall(int i, int j, Side side) {
int equivI, equivJ;
Side correspondingSide;
if (side == Side.TOP) {
equivI = i - 1;
equivJ = j;
correspondingSide = Side.BOTTOM;
} else if (side == Side.RIGHT) {
equivI = i;
equivJ = j + 1;
correspondingSide = Side.LEFT;
} else if (side == Side.BOTTOM) {
equivI = i + 1;
equivJ = j;
correspondingSide = Side.TOP;
} else { // left
equivI = i;
equivJ = j - 1;
correspondingSide = Side.RIGHT;
}
return new Trio(equivI, equivJ, correspondingSide);
}
// Invariant: mark(i, j, side) must be reflected on both sides of the wall
public void mark(int i, int j, Side side) {
Trio equivWall = getEquivalentWall(i, j, side);
int equivI = equivWall.i;
int equivJ = equivWall.j;
Side correspondingSide = equivWall.side;
if (rowInBounds(i) && columnInBounds(j)) {
walls[i][j].mark(side);
}
if (rowInBounds(equivI) && columnInBounds(equivJ)) {
walls[equivI][equivJ].mark(correspondingSide);
}
}
public boolean rowInBounds(int i) {
return (i >= 0) && (i < r);
}
public boolean columnInBounds(int j) {
return (j >= 0) && (j < c);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" ");
for (int j = 1; j <= c; j++) {
sb.append(" " + (j % 10));
}
sb.append("\n\n");
sb.append(" ");
addWallsToString(sb, 0, Side.TOP);
for (int i = 0; i < r; i++) {
sb.append((i+1) + " ");
if (i+1 < 10)
sb.append(" ");
addContentsToString(sb, i);
sb.append(" ");
addWallsToString(sb, i, Side.BOTTOM);
}
return sb.toString();
}
private void addContentsToString(StringBuilder sb, int row) {
if (isMarked(row, 0, Side.LEFT)) {
sb.append("|");
} else {
sb.append(" ");
}
for (int j = 0; j < c; j++) {
// square marking
if (lineReqs[row][j].hasValue()) {
sb.append(lineReqs[row][j].getValue());
} else {
sb.append(" ");
}
// wall marking
if (isMarked(row, j, Side.RIGHT)) {
sb.append("|");
} else {
sb.append(" ");
}
}
sb.append("\n");
return;
}
private void addWallsToString(StringBuilder sb, int row, Side side) {
for (int j = 0; j < c; j++) {
sb.append("+");
if (isMarked(row, j, side)) {
sb.append("-");
} else {
sb.append(" ");
}
}
sb.append("+\n");
}
private class Wall {
int value; // represented by a 4-digit base 2 value
public Wall() {
value = 0;
}
public void mark (Side side) {
if (!isMarked(side)) {
value += (1 << side.ordinal());
}
}
public boolean isMarked (Side side) {
return (value & (1 << side.ordinal())) != 0;
}
}
}
enum Side {
TOP, RIGHT, BOTTOM, LEFT;
public static final Side[] RELEVANT_SIDES = {Side.TOP, Side.LEFT};
static Side translate (char c) {
switch (c) {
case 't':
case 'T':
return Side.TOP;
case 'r':
case 'R':
return Side.RIGHT;
case 'b':
case 'B':
return Side.BOTTOM;
case 'l':
case 'L':
return Side.LEFT;
default:
throw new IllegalArgumentException(
"You incorrectly specified the side that you wanted to mark!"
);
}
}
}