-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.java
485 lines (376 loc) · 12.3 KB
/
World.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class World {
String newline = System.getProperty("line.separator");
Tile[][] grid;
int n; //row (x-axis)
int m; //col (y-axis)
int numFurniture; //number of pieces of furniture
int numDirt; //number of dirt piles
Agent[] agents; //array of agents
//agents[0] is first agent
//agents[1] is 2nd agent
/**
* method that "builds" the grid
*
* @param: String nameOfInputFile: name of the input file for configuration
*
* @post: grid is created with the appropriate tiles and what is in them
* numFurniture and numDirt is updated
*
* @throws: IOException if file not found etc
*
**/
public void buildGrid(String nameOfInputFile) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(nameOfInputFile));
Scanner scanner = new Scanner(System.in);
String line = null;
line = br.readLine(); //1st line
String[] tokens = line.split(" ");
this.n = Integer.parseInt(tokens[0]);
this.m = Integer.parseInt(tokens[1]);
this.numFurniture = Integer.parseInt(tokens[2]);
this.numDirt = Integer.parseInt(tokens[3]);
grid = new Tile[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = new Tile();
}
}
grid[0][0].isHome = true;
line = br.readLine(); //2nd line --> goal location(s)
tokens = line.split(" ");
grid[Integer.parseInt(tokens[1])][Integer.parseInt(tokens[0])].isGoal = true;
if (tokens.length == 4) {
grid[Integer.parseInt(tokens[3])][Integer.parseInt(tokens[2])].isGoal = true;
}
line = br.readLine(); //3rd line --> furniture location(s)
tokens = line.split(" ");
for (int i = 0; i < 2 * numFurniture; i = i + 2) {
grid[Integer.parseInt(tokens[i + 1])][Integer.parseInt(tokens[i])].isFurniture = true;
}
line = br.readLine(); //4th line --> dirt location(s)
tokens = line.split(" ");
for (int i = 0; i < 2 * numDirt; i = i + 2) {
grid[Integer.parseInt(tokens[i + 1])][Integer.parseInt(tokens[i])].isDirt = true;
}
}
/**
* method to add an agent
*
* @post: if there are no agents already:
* init agents and construct put an agent on [0][0]
* if there is already one agent:
* construct and put an agent on [m-1][n-1]
*
* */
public void addAgent(){
if(agents==null){
agents = new Agent[2];
agents[0] = new Agent(0, this);
grid[0][0].isAgent = true;
}else{
agents[1] = new Agent(1, this);
grid[m-1][n-1].isAgent = true;
}
}
/**
* method to generate the appropriate perception vector for an agent at a time step
*
* @param: int label: label of the agent for which to get perceptVector
*
* @returns: int[11]: which is the perception vector
* */
public int[] getPerceptVector(int label){
int[] ret = new int[11];
int y = agents[label].location[0];
int x = agents[label].location[1];
int direction = agents[label].direction;
//facing the front
if(direction == 0) {
if (grid[y][x].isDirt) {
ret[1] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isDirt)
ret[2] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isDirt)
ret[3] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isDirt)
ret[4] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isDirt)
ret[5] = 1;
}
if (grid[y][x].isGoal) {
ret[6] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isGoal)
ret[7] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isGoal)
ret[8] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isGoal)
ret[9] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isGoal)
ret[10] = 1;
}
}
//facing the right
if(direction == 1) {
if (grid[y][x].isDirt) {
ret[1] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isDirt)
ret[4] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isDirt)
ret[5] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isDirt)
ret[3] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isDirt)
ret[2] = 1;
}
if (grid[y][x].isGoal) {
ret[6] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isGoal)
ret[9] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isGoal)
ret[10] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isGoal)
ret[8] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isGoal)
ret[7] = 1;
}
}
//facing south
if(direction == 2) {
if (grid[y][x].isDirt) {
ret[1] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isDirt)
ret[3] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isDirt)
ret[2] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isDirt)
ret[5] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isDirt)
ret[4] = 1;
}
if (grid[y][x].isGoal) {
ret[6] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isGoal)
ret[8] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isGoal)
ret[7] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isGoal)
ret[10] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isGoal)
ret[9] = 1;
}
}
//facing the left
if(direction == 3) {
if (grid[y][x].isDirt) {
ret[1] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isDirt)
ret[5] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isDirt)
ret[4] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isDirt)
ret[2] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isDirt)
ret[3] = 1;
}
if (grid[y][x].isGoal) {
ret[6] = 1;
}
if (y < m - 1) {
if (grid[y + 1][x].isGoal)
ret[10] = 1;
}
if (y > 0) {
if (grid[y - 1][x].isGoal)
ret[9] = 1;
}
if (x > 0) {
if (grid[y][x - 1].isGoal)
ret[7] = 1;
}
if (x < n - 1) {
if (grid[y][x + 1].isGoal)
ret[8] = 1;
}
}
return ret;
}
/**
* moves an agent forward if there is nothing in front of him
* if there is, then agent stays at his place and perceptVector is updated
*
* @param: int label: label of the agent to move
*
* @post: agent is moved forward if no obstacles
* or agent stays in place
* perceptVector is updated in both cases
* */
public void moveAgent(int label){
int x = agents[label].location[1];
int y = agents[label].location[0];
int direction = agents[label].direction;
if(direction == 0){ //Facing North
y = y+1;
}
if(direction == 1){ //Facing East
x = x+1;
}
if(direction == 2){ //Facing South
y = y-1;
}
if(direction == 3){ //Facing West
x = x-1;
}
if(y>=m|| y<0 || x>=n || x<0||grid[y][x].isFurniture||grid[y][x].isAgent){
agents[label].perceptVector = getPerceptVector(label);
agents[label].perceptVector[0] = 1;
}else{
grid[agents[label].location[0]][agents[label].location[1]].isAgent = false;
agents[label].location[0] = y;
agents[label].location[1] = x;
grid[y][x].isAgent = true;
agents[label].perceptVector = getPerceptVector(label);
}
}
/**
* turn the agent off, and update the tile so as to not make the goal visible
* if it is a goal.
*
* @param: int label: label of the agent to turn off
*
* @post: agent is turned off
* if tile at which agent is turned off is goal, tile is not goal anymore
*
* */
public void turnAgentOff(int label){
int y = agents[label].location[0];
int x = agents[label].location[1];
agents[label].isOn = false;
if(grid[y][x].isGoal){
grid[y][x].isGoal = false;
}
}
/**
* method for the agent to vacuum
*
* @param: int label: label of agent to vacuum
*
* @post: tile where the agent is, is no longer dirt
* : perceptVector is updated
* */
public void vacuum(int label){
int y = agents[label].location[0];
int x = agents[label].location[1];
grid[y][x].isDirt= false;
agents[label].perceptVector = getPerceptVector(label);
}
public String toString(){
String ret = "";
String border = "+";
for(int i = 0; i<n;i++){
border = border + "-+";
}
ret = border;
for(int i = m-1; i>=0; i--){
ret = ret + newline + "|";
for(int j = 0; j<n; j++){
if(grid[i][j].isAgent){
if(i==agents[0].location[0]&&j==agents[0].location[1]){
ret = ret + agents[0].toString();
}else{
ret = ret + agents[1].toString();
}
}else {
ret = ret + grid[i][j];
}
if(j==n-1) {
ret = ret +"|";
}else{
ret = ret+ ":";
}
}
}
ret = ret + newline + border + newline + newline;
if(agents[1]==null){
ret = ret + "T DU DF DB DL DR GU GF GB GL GR"+ newline;
for(int i: agents[0].perceptVector){
ret = ret + i + " ";
}
ret = ret + newline + "Power Level: "+agents[0].powerLevel+newline;
ret = ret+ "Performance Level: " + agents[0].performanceLevel;
}else{
ret = ret + "Agent #1:" +newline+"T DU DF DB DL DR GU GF GB GL GR"+ newline;
for(int i: agents[0].perceptVector){
ret = ret + i + " ";
}
ret = ret + newline + "Power Level: "+agents[0].powerLevel+newline;
ret = ret+ "Performance Level: " + agents[0].performanceLevel;
ret = ret+newline+newline+"Agent #2:" + newline + "T DU DF DB DL DR GU GF GB GL GR"+ newline;
for(int i: agents[1].perceptVector){
ret = ret + i + " ";
}
ret = ret + newline + "Power Level: "+agents[1].powerLevel+newline;
ret = ret + "Performance Level: " + agents[1].performanceLevel;
}
return ret + newline;
}
}