-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserOfMaze.java
204 lines (174 loc) · 7 KB
/
UserOfMaze.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
/**
Test Maze class.
Requires command line arguments:
o the name of a file containing a maze.
o the rank and file where an explorer is starting
For example,
java UserOfMaze mazes/4cell_treasureWest.txt -1 -1 # no explorer
*/
public class UserOfMaze {
private static Displayer displayer;
public static void main(String[] commandLine)
throws java.io.FileNotFoundException {
System.out.println();
Maze maze = new Maze( commandLine[0]
, Integer.parseInt( commandLine[1])
, Integer.parseInt( commandLine[2])
);
System.out.println( maze + System.lineSeparator());
//moveTest( maze);
//dropTest( maze);
//copyConstructTest( maze);
// // test Displayer
//displayer = new Displayer( Integer.parseInt( commandLine[3]));
//displayerTest( maze);
snapshotDemo( maze);
}
/**
Move around a maze. Check the results.
Run using a shell command like...
java UserOfMaze mazes/intersection_treasureNorth.txt 1 1
*/
// private static void moveTest( Maze maze) {
// maze.go( Maze.EAST);
// System.out.println( "go east"
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// maze.go( Maze.NORTH);
// System.out.println( "go north"
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// maze.go( Maze.WEST);
// System.out.println( "go west"
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// maze.go( Maze.SOUTH);
// System.out.println( "go south"
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
//
// // step out of the maze
// maze.go( Maze.SOUTH);
// maze.go( Maze.SOUTH);
// System.out.println( "outside"
// + ", leaving explorer \"on\" a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// }
/**
Drop maze elements. Check the results.
Run using a shell command like...
java UserOfMaze mazes/4cell_treasureWest.txt 0 1
*/
// private static void dropTest( Maze maze) {
// maze.dropA( Maze.TREASURE);
// System.out.println( "tried to drop a " + Maze.TREASURE
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// maze.dropA( Maze.WALL);
// System.out.println( "dropped a " + Maze.WALL
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// maze.dropA( Maze.STEPPING_STONE);
// System.out.println( "dropped a " + Maze.STEPPING_STONE
// + ", leaving explorer on a " + maze.explorerIsOnA()
// + System.lineSeparator()
// + maze + System.lineSeparator());
// }
/**
Copy-construct a new maze and check its independence
from the original.
Run using a shell command like...
java UserOfMaze mazes/intersection_treasureNorth.txt 1 1
*/
// private static void copyConstructTest( Maze old) {
// Maze copy = new Maze( old);
//
// // change the old
// old.go( Maze.NORTH);
// old.dropA( Maze.WALL);
// System.out.println(
// "modified old" + System.lineSeparator()
// + old + System.lineSeparator()
// + "unchanged copy?" + System.lineSeparator()
// + copy + System.lineSeparator()
// );
//
// // change the copy
// copy.go( Maze.SOUTH);
// copy.go( Maze.WEST);
// copy.dropA( Maze.STEPPING_STONE);
// System.out.println(
// "modified copy" + System.lineSeparator()
// + copy + System.lineSeparator()
// + "unchanged old?" + System.lineSeparator()
// + old + System.lineSeparator()
// );
// }
/**
Display changes to a maze.
Run by using the height of your shell window as a final argument, like...
java UserOfMaze mazes/4cell_treasureWest.txt 0 3 25
*/
// private static void displayerTest( Maze m) {
// int step = 0;
//
// displayer.atTopOfWindow( m + "step " + step++);
//
// // move past west edge, Displaying as we go
// while( step < 5) {
// m.go( Maze.WEST);
// displayer.atTopOfWindow( m + "step " + step++);
// }
// }
/**
Demo the restore-from-snapshot paradigm.
Run using a shell command like...
java UserOfMaze mazes/4cell_treasureWest.txt 0 1
*/
private static void snapshotDemo( Maze candidate) {
Maze snapshot;
snapshot = new Maze( candidate);
for (int i = 0; i < candidate.rankCount; i++) {
candidate.go(8);
}
System.out.println(
"modified candidate with no explorer"
+ System.lineSeparator()
+ candidate + System.lineSeparator()
+ "unchanged snapshot" + System.lineSeparator()
+ snapshot + System.lineSeparator()
);
/* Expecting...
modified candidate with no explorer
------
|0** |
------
unchanged snapshot
------
|0e* |
------
*/
// throw new java.lang.RuntimeException(
// "Write code to undo the go() by making @candidate refer "
// + "to an unchanged copy of the maze.");
candidate = snapshot;
System.out.println(
"restored candidate, with an explorer"
+ System.lineSeparator()
+ candidate + System.lineSeparator()
);
/* Expecting...
restored candidate, with an explorer
------
|0e* |
------
*/
}
}