-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.java
More file actions
279 lines (241 loc) · 8.42 KB
/
Game.java
File metadata and controls
279 lines (241 loc) · 8.42 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
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
import java.util.Stack;
/**
* 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 Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Game
{
private Parser parser;
private Room currentRoom;
private Stack <Room> history;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
history = new Stack<>();
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room cubby, workingArea, toiletRoom, cafeteria, kitchen, lobby,
directorsRoom, meetingRoom, reception, exit;
// create the rooms
kitchen = new Room("in the kitchen");
cafeteria = new Room("in the cafeteria");
workingArea = new Room("in a giant working area of an office");
cubby = new Room("in a cubby.");
toiletRoom = new Room("in the toilet room of the office");
lobby = new Room ("in the lobby");
directorsRoom = new Room ("in the director's Room");
meetingRoom = new Room ("in the meeting Room");
reception = new Room ("in the office's reception");
exit = new Room ("at the exit", true);
// initialise room exits
kitchen.setExits("north", cafeteria);
cafeteria.setExits("south", kitchen);
cafeteria.setExits("north", workingArea);
workingArea.setExits("south", cafeteria);
workingArea.setExits("west", toiletRoom);
workingArea.setExits("east", cubby);
workingArea.setExits("north", lobby);
cubby.setExits("west", workingArea);
toiletRoom.setExits("east", workingArea);
lobby.setExits("south", workingArea);
lobby.setExits("west", directorsRoom);
lobby.setExits("east", meetingRoom);
lobby.setExits("north", reception);
directorsRoom.setExits("west", lobby);
meetingRoom.setExits("east", lobby);
reception.setExits("south", lobby);
reception.setExits("north", exit);
// Set up Items
cafeteria.addItem(new Item("coffee", "A tasty cup of coffee that restores your health", 0));
kitchen.addItem(new Item("spoon", "A shiny silver spoon", 150));
workingArea.addItem(new Item("stapler", "A stapler (sponsored by Staples)", 200));
directorsRoom.addItem(new Item("bat", "A wooden baseball bat signed by Derek Jeter", 2000));
directorsRoom.addItem(new Item("printer", "A laser printer", 15000));
meetingRoom.addItem(new Item("shredder", "A shredder", 2000));
toiletRoom.addItem(new Item("window", "A very small, very dirty window", -1));
reception.addItem(new Item("pencil", "A very pointy-looking pencil", 100));
currentRoom = cubby; // start game in a cubby
}
/**
* 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();
printLocationInfo();
}
/**
* 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 wantToQuit = false;
if(command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help")) {
printHelp();
}
else if (commandWord.equals("go")) {
wantToQuit = goRoom(command);
}
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
else if (commandWord.equals("look")) {
look();
}
else if (commandWord.equals("drink")) {
drink();
}
else if (commandWord.equals("scream")) {
scream();
}
else if (commandWord.equals("info")) {
info(command);
}
else if (commandWord.equals("back")) {
goBack();
}
return wantToQuit;
}
// 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 go in one direction. If there is an exit, enter
* the new room, otherwise print an error message.
*/
private boolean 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 false;
}
String direction = command.getSecondWord();
// Try to leave current room.
Room nextRoom = null;
nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
history.push(currentRoom);
currentRoom = nextRoom;
printLocationInfo();
}
return currentRoom.isExit();
}
/**
* "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;
}
else {
return true; // signal that we want to quit
}
}
private void look ()
{
System.out.println(currentRoom.getLongDescription());
}
private void drink ()
{
int bottle = 1;
if(bottle > 0) {
System.out.println("You drank your last bottle of water. Hurry up now!");
bottle--;
}
}
private void scream ()
{
System.out.println("Your voice won't be heard");
}
private void printLocationInfo ()
{
System.out.println(currentRoom.getLongDescription());
}
private void info(Command command){
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Which item?");
return;
}
String itemName = command.getSecondWord();
Item item = currentRoom.getItem(itemName);
if(item != null){
System.out.println(item.getDescription());
}else{
System.out.println("There is no such item here!");
}
}
private void goBack(){
if(history.size() == 0){
System.out.println("You are at start!");
return;
}
currentRoom = history.pop();
printLocationInfo();
}
}