-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.java
289 lines (247 loc) · 8.89 KB
/
Game.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
import java.util.ArrayList;
/**
* This class is the main class of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game. Users
* can walk around some scenery. That's all. It should really be extended
* to make it more interesting!
*
* To play this game, create an instance of this class and call the "play"
* method.
*
* This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
* @author Fredrik Ljungdahl, Michael Kölling and David J. Barnes
* @version 2013.12.19
*/
public class Game {
private Parser parser;
private Timer timer;
private ArrayList<Key> keys;
private Room currentRoom;
private Room beamerRoom;
/**
* Starts the game
*/
public static void main(String[] args) {
Game game = new Game();
game.play();
}
/**
* Create the game and initialise its internal map.
*/
public Game() {
createRooms();
timer = new Timer(60, -1, 5);
keys = new ArrayList<Key>();
parser = new Parser();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms() {
Room outside, theater, pub, lab, office, classroom;
Key officeKey = new Key("Office");
// create the rooms
outside = new Room("outside the main entrance of the university");
theater = new Room("in a lecture theater", officeKey);
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
classroom = new Room("in a plain classroom");
// initialise room exits
outside.setExit("east", theater);
outside.setExit("south", classroom);
outside.setExit("west", pub);
theater.setExit("west", outside);
theater.setExit("south", lab);
pub.setExit("east", outside);
classroom.setExit("north", outside);
classroom.setExit("east", lab);
lab.setExit("north", theater, "trapdoor");
lab.setExit("east", office, officeKey);
lab.setExit("west", classroom);
office.setExit("west", lab);
currentRoom = outside; // start game outside
}
/**
* Main play routine. Loops until end of play.
*/
public void play() {
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the game is over.
boolean finished = false;
while (!finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
/**
* Print out the opening message for the player.
*/
private void printWelcome() {
System.out.println();
System.out.println("Welcome to the World of Zuul!");
System.out.println("World of Zuul is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println("You have "+timer+"s to win.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
}
/**
* Given a command, process (that is: execute) the command.
* @param command The command to be processed.
* @return true If the command ends the game, false otherwise.
*/
private boolean processCommand(Command command) {
boolean quitGame = false;
boolean updateTimer = true;
CommandWord commandWord = command.getCommandWord();
switch (commandWord) {
case UNKNOWN:
System.out.println("Huh? I don't understand what you're talking about...");
updateTimer = false;
return false;
case BACK:
gotoWaypoint();
break;
case GO:
goRoom(command);
break;
case HELP:
printHelp();
updateTimer = false; // this is metagaming, don't bother with the timer
break;
case MARK:
setWaypoint(currentRoom);
break;
case QUIT:
quitGame = quit(command);
updateTimer = false;
break;
case TIME:
System.out.println("You have "+timer+"s left...");
break;
}
if (updateTimer) {
timer.updateTimer();
if (timer.hasExpired()) {
System.out.println("Time's up - you lost!");
quitGame = true;
} else if (timer.isLow()) {
System.out.println("Time is running low!");
System.out.println("You have "+timer+"s left...");
}
}
return quitGame;
}
// implementations of user commands:
/**
* Print out some help information.
* Here we print some stupid, cryptic message and a list of the
* command words.
*/
private void printHelp() {
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the university.");
System.out.println();
System.out.println("Your command words are:");
parser.showCommands();
}
/**
* Try to in to one direction. If there is an exit, enter the new
* room, otherwise print an error message.
*/
private void goRoom(Command command) {
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) { // there's nothing in that direction
System.out.println("There's nothing there!");
return;
}
if (currentRoom.getState(direction) == "locked") { // check if needed key is owned.
for (Key key : keys) {
if (key.toString().equals(currentRoom.getExitKey(direction))) {
currentRoom.setState(direction, "ok");
System.out.println("You unlocked the door!");
break;
}
}
}
switch (currentRoom.getState(direction)) {
case "locked":
String keyLabel = currentRoom.getExitKey(direction);
System.out.println("That door is locked! You can unlock it with a key labeled "+keyLabel+", though.");
break;
case "ok":
currentRoom = nextRoom;
getRoomInfo();
break;
case "trapdoor":
System.out.println("That way can only be taken from the other side!");
break;
default:
System.out.println("Internal error. Please file a bug report.");
break;
}
}
/**
* Retrieves room information.
*/
private void getRoomInfo() {
System.out.println(currentRoom.getLongDescription());
if (currentRoom.hasKey()) {
for (Key key : keys) {
if (key.toString().equals(currentRoom.getKeyInfo())) {
return;
}
}
System.out.println("You found a key!");
Key gotKey = currentRoom.getKey();
System.out.println("This key is labeled: " + gotKey);
gotKey.claim();
keys.add(gotKey);
}
}
/**
* Waypoint (beamer) - allows you to "remember" this place.
* This way, you can always go back to the place,
* unless you mark a new point.
*/
private void setWaypoint(Room room) {
beamerRoom = currentRoom;
System.out.println("You've put this room in your memory.");
System.out.println("Now, you can go back to this room whenever you want to with 'back'.");
}
private boolean gotoWaypoint() {
if (beamerRoom == null) {
System.out.println("You never bothered to remember any place...");
return false;
}
currentRoom = beamerRoom;
System.out.println("You went back!");
getRoomInfo();
return true;
}
/**
* "Quit" was entered. Check the rest of the command to see
* whether we really quit the game.
* @return true, if this command quits the game, false otherwise.
*/
private boolean quit(Command command) {
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
return true;
}
}