-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorld.java
More file actions
138 lines (124 loc) · 4.62 KB
/
World.java
File metadata and controls
138 lines (124 loc) · 4.62 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
public class World {
protected int arrowCount;
private int size;
private int x;
private int y;
protected int direction = 0;
private int[][] perceptMap;
final int NORTH = 1;
final int EAST = 2;
final int SOUTH = 3;
final int WEST = 4;
private final int BREEZE = 1;
private final int STENTCH = 2;
private final int BUMP = 4;
private final int GLITTER = 8;
private final int DEATH_BY_WUMPUS = 16;
private final int DEATH_BY_PIT = 32;
private final int SCREAM = 64;
final int GRAB = 1;
final int MOVE = 2;
final int TURN_LEFT = 3;
final int TURN_RIGHT = 4;
final int SHOOT = 5;
final int END = 6;//needed?
public World(String fileName) {
x = 0;
y = 0;
//read in file
}
public int[] getLocation() {
int[] location = {x, y};
return location;
}
public int getPercepts() {
return perceptMap[x][y];
}
public int action(int action) {
switch (action) {
case GRAB:
if ((perceptMap[x][y] & GLITTER) != 0)
perceptMap[x][y] -= GLITTER;
break;
case MOVE:
if (direction == NORTH) {
if (y + 1 < size) {
y = y + 1;
return perceptMap[x][y];
} else return BUMP;
} else if (direction == EAST) {
if (x + 1 < size) {
x = x + 1;
return perceptMap[x][y];
} else return BUMP;
} else if (direction == SOUTH) {
if (y - 1 > 0) {
y -= 1;
return perceptMap[x][y];
} else return BUMP;
} else if (direction == WEST) {
if (x - 1 > 0) {
x -= 1;
return perceptMap[x][y];
} else return BUMP;
}
break;
case TURN_LEFT:
direction = (direction + 3) % 4 + 1;
return perceptMap[x][y];
case TURN_RIGHT:
direction = (direction + 1) % 4 + 1;
return perceptMap[x][y];
case SHOOT:
//shoot logic
if (arrowCount < 1) {
return -1; //out of arrows, which shouldn't be possible
}
arrowCount--;
switch (direction) {
case 1: //shoot north
for (int i = y; i < perceptMap.length; i++) {
if (perceptMap[x][i] == 16) { //hits Wumpus
return SCREAM;
} else if (perceptMap[x][i] == 4) { //hits Obstacle
return perceptMap[x][y];
}
}
return perceptMap[x][y];
case 2: //shoot east
for (int i = y; i < perceptMap.length; i++) {
if (perceptMap[i][y] == 16) { //hits Wumpus
return SCREAM;
} else if (perceptMap[i][y] == 4) { //hits Obstacle
return perceptMap[x][y];
}
}
return perceptMap[x][y];
case 3: //shoot south
for (int i = y; i > 0; i--) {
if (perceptMap[x][i] == 16) { //hits Wumpus
return SCREAM;
} else if (perceptMap[x][i] == 4) { //hits Obstacle
return perceptMap[x][y];
}
}
return perceptMap[x][y];
case 4: //shoot west
for (int i = y; i > 0; i--) {
if (perceptMap[i][y] == 16) { //hits Wumpus
return SCREAM;
} else if (perceptMap[i][y] == 4) { //hits Obstacle
return perceptMap[x][y];
}
}
return perceptMap[x][y];
default:
System.out.println("Error in shooting logic.");
}
case END:
//needed?
}
System.out.println("Error shouldn't ever get here");
return -1;
}
}