-
Notifications
You must be signed in to change notification settings - Fork 1
/
MysteryMansionBot.java
303 lines (255 loc) · 8.12 KB
/
MysteryMansionBot.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
/**
* KXO151 Assignment 3, 2024
*
* Mystery Mansion -- Library class
*
* MysteryMansionBot.java
*
* Notes: Students should NOT change this program
*
* Contributors: Chengwei Yan
* Student ID: 694659
* Email: [email protected]
* Wentao Su
* Student ID: 694641
* Email: [email protected]
*
* Statement of Purpose: This program is designed to play the Mystery Mansion Game.
*/
import java.util.Random;
public class MysteryMansionBot
{
// final instance variables
public final int IMPOSSIBLE = -1; // operation unsuccessful because it was not allowed
public final int SUCCESS = 0; // operation successful
public final int EATEN = 1; // operation unsuccessful because player found by ghost
public final int FELL = 2; // operation unsuccessful because player fell in pit
public final int FAILURE = 3; // operation unsuccessful for reason other than above two reasons
private final int NUM_ROOM = 8; // number of ROOMS
private final int[] RIGHT_ROOM = {3,0,1,2,5,6,7,4}; // ROOMS to right
private final int[] LEFT_ROOM = {1,2,3,0,7,4,5,6}; // ROOMS to left
private final int[] MID_ROOM = {4,5,6,7,0,1,2,3}; // ROOMS to front
//non-final instance variables
private int currRoom; // current location of player
private int ghostPos; // room where the ghost lives
private int pitPos; // room where the pit is found
private Random generator; // to use for random placement in rooms
private boolean tracing; // switch for tracing messages
/**
* creates a bot for a mystery mansion game
*/
public MysteryMansionBot()
{
generator = new Random();
tracing = true; // assume program is being debugged
generator.setSeed(101); // seed RNG for tracing purposes
}
/**
* provide the number of rooms in the room system
* @return indicates how many rooms in the system
*/
public int getNumRooms()
{
return NUM_ROOM;
}
/**
* randomly determines unique locations of start (currentCave), pit, ghost
* @return indicates the starting location (room)
*/
public int newGame()
{
int pos;
// determine player's position
pos = generator.nextInt(NUM_ROOM);
currRoom = pos;
trace("player starts at " + currRoom);
// determine ghost' position
pos = generator.nextInt(NUM_ROOM);
while (pos == currRoom)
{
pos = generator.nextInt(NUM_ROOM);
}
ghostPos = pos;
trace("Ghost is at " + ghostPos);
// determine position of pit
pos = generator.nextInt(NUM_ROOM);
while ((pos == currRoom) || (pos == ghostPos))
{
pos = generator.nextInt(NUM_ROOM);
}
pitPos = pos;
trace("pit is at " + pitPos);
return currRoom;
}
/**
* determine if Ghost is in adjacent room
* @return whether Ghost location is in a room connected to current
*/
public boolean ghostNear()
{
boolean isClose = false;
if ((RIGHT_ROOM[currRoom ] == ghostPos) ||
(MID_ROOM[currRoom ] == ghostPos) ||
(LEFT_ROOM[currRoom ] == ghostPos) )
{
isClose = true;
trace("Ghost is close ");
}
else
{
trace("Ghost is not close");
}
return isClose;
}
/**
* determine if pit is in adjacent room
* @return whether pit location is in a room connected to current
*/
public boolean pitNear()
{
boolean isClose = false;
if ((RIGHT_ROOM[currRoom ] == pitPos) ||
(MID_ROOM[currRoom ] == pitPos) ||
(LEFT_ROOM[currRoom ] == pitPos) )
{
isClose = true;
trace("pit is close");
}
else
{
trace("pit is not close");
}
return isClose;
}
/**
* try to move the player to another room
* @param into indicates the room to try to move into
* @return status of movement
* SUCCESS: move was successful (current position changed)
* IMPOSSIBLE: move was impossible (current position not changed)
* EATEN: moved and was eaten
* FELL: moved and fell in pit
*/
public int tryWalk(int into)
{
int result;
// check direction
if ((RIGHT_ROOM[currRoom ] == into) ||
(MID_ROOM[currRoom ] == into) ||
(LEFT_ROOM[currRoom ] == into))
{
trace("move into Room " + into );
currRoom = into;
if (currRoom == ghostPos)
{
result = EATEN;
}
else
{
if (currRoom == pitPos)
{
result = FELL;
}
else
{
result = SUCCESS;
}
}
}
else
{
result = IMPOSSIBLE;
}
trace("result of attempt to move: " + result);
return result;
}
/**
* try to shoot into another room
* @param into indicates the room to try to shoot into
* @return status of shooting
* SUCCESS: shot was successful (ghost killed)
* IMPOSSIBLE: shot was impossible (no arrow used)
* FAILURE: shot was unsuccessful (ghost not present)
*/
public int tryShoot(int into)
{
int result;
if((RIGHT_ROOM[currRoom ] == into) ||
(MID_ROOM[currRoom ] == into) ||
(LEFT_ROOM[currRoom ] == into))
{
trace("shoot into Room " + into );
trace("Ghost is at " + ghostPos);
if (into == ghostPos)
{
result = SUCCESS;
}
else
{
result = FAILURE;
}
}
else
{
result = IMPOSSIBLE;
}
trace("result of attempt to shoot: " + result);
return result;
}
/**
* provide the number of the current room
* @return which room number player is within
*/
public int getCurrent()
{
trace("in Room number " + currRoom);
return currRoom;
}
/**
* determine number of adjacent room given its direction
* @param char indicates the direction required (l - left, r - right, a - ahead) * @return status of movement
* @return number of room in that direction or IMPOSSIBLE if invalid parameter
*/
public int nextRoom(char direction)
{
final char LEFT = 'l'; // left
final char AHEAD = 'a'; // ahead
final char RIGHT = 'r'; // right
int nextIs;
switch (direction)
{
case LEFT: nextIs = LEFT_ROOM[currRoom];
break;
case AHEAD: nextIs = MID_ROOM[currRoom];
break;
case RIGHT: nextIs = RIGHT_ROOM[currRoom];
break;
default: nextIs = IMPOSSIBLE;
}
trace("next Room on " + direction + " is " + nextIs);
return nextIs;
}
/**
* turn tracing messages on or off (if off it is assumed that debugging is not occurring and so a new (unseeded) RNG is provided
* @param onOff indicates the required state of messages (true on, false off)
*/
public void setTracing(boolean onOff)
{
if (! onOff) // not tracing so get an unseeded RNG
{
generator=new Random();
}
tracing = onOff;
}
/**
* displays tracing messages
* @param message the message to be displayed if instance variable tracing is true
*/
public void trace(String message)
{
if (tracing)
{
System.out.println("GhostBot: " + message);
}
}
}