-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridMonster.java
202 lines (179 loc) · 8 KB
/
GridMonster.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
import java.awt.Color;
import java.util.ArrayList;
/**
* Creates a grid with tiles that make up a picture.
*
* @author Jin Kim
* @version 8 December 2015
*/
public class GridMonster
{
/**
* Overwatches the operation of the class GridMonster.
*
* @param args information past via the command line
*/
public static void main(String[] args)
{
System.out.println("Level 1: getting number of rows and columns");
MyBoundedGrid<String> grid = new MyBoundedGrid<String>(2, 1);
if (grid.getNumRows() != 2)
throw new RuntimeException("getNumRows is dumb");
if (grid.getNumCols() != 1)
throw new RuntimeException("getNumCols is dumb");
System.out.println("Level 2: testing if valid");
if (!grid.isValid(new Location(0, 0)))
throw new RuntimeException("isValid is dumb");
if (!grid.isValid(new Location(1, 0)))
throw new RuntimeException("isValid is dumb");
if (grid.isValid(new Location(2, 0)))
throw new RuntimeException("isValid is dumb");
if (grid.isValid(new Location(0, 1)))
throw new RuntimeException("isValid is dumb");
if (grid.isValid(new Location(0, -1)))
throw new RuntimeException("isValid is dumb");
if (grid.isValid(new Location(-1, 0)))
throw new RuntimeException("isValid is dumb");
System.out.println("Level 3: getting, putting, and removing");
Location loc = new Location(1, 0);
String firstValue = "first";
String secondValue = "second";
String value = grid.get(loc);
if (value != null)
throw new RuntimeException("get is dumb");
value = grid.put(loc, firstValue);
if (value != null)
throw new RuntimeException("put is dumb");
value = grid.get(loc);
if (value != firstValue)
throw new RuntimeException("put/get is dumb");
value = grid.put(loc, secondValue);
if (value != firstValue)
throw new RuntimeException("put is dumb");
value = grid.get(loc);
if (value != secondValue)
throw new RuntimeException("put is dumb");
value = grid.remove(loc);
if (value != secondValue)
throw new RuntimeException("remove is dumb");
value = grid.get(loc);
if (value != null)
throw new RuntimeException("remove is dumb");
value = grid.remove(loc);
if (value != null)
throw new RuntimeException("remove is dumb");
System.out.println("Level 4: getting occupied locations");
ArrayList<Location> locs = grid.getOccupiedLocations();
if (locs.size() != 0)
throw new RuntimeException("getOccupiedLocations is dumb");
grid.put(new Location(1, 0), firstValue);
locs = grid.getOccupiedLocations();
if (locs.size() != 1)
throw new RuntimeException("getOccupiedLocations is dumb");
if (!locs.get(0).equals(new Location(1, 0)))
throw new RuntimeException("getOccupiedLocations is dumb");
grid.put(new Location(0, 0), secondValue);
locs = grid.getOccupiedLocations();
if (locs.size() != 2)
throw new RuntimeException("getOccupiedLocations is dumb");
if (!(
(locs.get(0).equals(new Location(0, 0)) && locs.get(1).equals(new Location(1, 0)))
||
(locs.get(0).equals(new Location(1, 0)) && locs.get(1).equals(new Location(0, 0)))))
throw new RuntimeException("getOccupiedLocations is dumb");
System.out.println("Level 5: putting into an empty location");
Block h = new Block();
Color red = Color.RED;
h.setColor(red);
if (h.getColor() != red)
throw new RuntimeException("setColor/getColor is dumb");
MyBoundedGrid<Block> grid2 = new MyBoundedGrid<Block>(4, 5);
BlockDisplay display = new BlockDisplay(grid2);
h.putSelfInGrid(grid2, new Location(0, 2));
display.showBlocks();
if (grid2.get(new Location(0, 2)) != h)
throw new RuntimeException("putSelfInGrid is dumb");
if (h.getGrid() != grid2)
throw new RuntimeException("putSelfInGrid is dumb");
if (!h.getLocation().equals(new Location(0, 2)))
throw new RuntimeException("putSelfInGrid is dumb");
System.out.println("Level 6: removing from grid");
h.removeSelfFromGrid();
display.showBlocks();
if (grid2.get(new Location(0, 2)) != null)
throw new RuntimeException("removeSelfFromGrid is dumb");
if (h.getGrid() != null)
throw new RuntimeException("removeSelfFromGrid is dumb");
if (h.getLocation() != null)
throw new RuntimeException("removeSelfFromGrid is dumb");
System.out.println("Level 7: putting into an occupied location");
h.putSelfInGrid(grid2, new Location(0, 2));
display.showBlocks();
Block r = new Block();
r.putSelfInGrid(grid2, new Location(0, 2));
display.showBlocks();
if (h.getGrid() != null)
throw new RuntimeException("putSelfInGrid is dumb");
if (h.getLocation() != null)
throw new RuntimeException("putSelfInGrid is dumb");
if (r.getGrid() != grid2)
throw new RuntimeException("putSelfInGrid is dumb");
if (!r.getLocation().equals(new Location(0, 2)))
throw new RuntimeException("putSelfInGrid is dumb");
if (grid2.get(new Location(0, 2)) != r)
throw new RuntimeException("putSelfInGrid is dumb");
System.out.println("Level 8: moving to an empty location");
r.moveTo(new Location(0, 4));
display.showBlocks();
if (grid2.get(new Location(0, 2)) != null)
throw new RuntimeException("moveTo is dumb");
if (!r.getLocation().equals(new Location(0, 4)))
throw new RuntimeException("moveTo is dumb");
if (grid2.get(new Location(0, 4)) != r)
throw new RuntimeException("moveTo is dumb");
System.out.println("Level 9: moving to an occupied location");
h.putSelfInGrid(grid2, new Location(0, 1));
display.showBlocks();
r.moveTo(new Location(0, 1));
display.showBlocks();
if (grid2.get(new Location(0, 4)) != null)
throw new RuntimeException("moveTo is dumb");
if (h.getGrid() != null)
throw new RuntimeException("moveTo is dumb");
if (h.getLocation() != null)
throw new RuntimeException("moveTo is dumb");
if (r.getGrid() != grid2)
throw new RuntimeException("moveTo is dumb");
if (!r.getLocation().equals(new Location(0, 1)))
throw new RuntimeException("moveTo is dumb");
if (grid2.get(new Location(0, 1)) != r)
throw new RuntimeException("moveTo is dumb");
System.out.println("Level 10: moving to its own location");
r.moveTo(new Location(0, 1));
display.showBlocks();
if (r.getGrid() != grid2)
throw new RuntimeException("moveTo is dumb");
if (!r.getLocation().equals(new Location(0, 1)))
throw new RuntimeException("moveTo is dumb");
if (grid2.get(new Location(0, 1)) != r)
throw new RuntimeException("moveTo is dumb");
Block block = new Block();
block.putSelfInGrid(grid2, new Location(0, 3));
block = new Block();
block.setColor(Color.RED);
block.putSelfInGrid(grid2, new Location(2, 0));
block = new Block();
block.setColor(Color.RED);
block.putSelfInGrid(grid2, new Location(2, 4));
block = new Block();
block.setColor(Color.RED);
block.putSelfInGrid(grid2, new Location(3, 1));
block = new Block();
block.setColor(Color.RED);
block.putSelfInGrid(grid2, new Location(3, 2));
block = new Block();
block.setColor(Color.RED);
block.putSelfInGrid(grid2, new Location(3, 3));
display.showBlocks();
}
}