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
|
const readline = require('readline');
// Define a room function that represents a room in the game
// A room has a name, description, and exits to other rooms
// A room can be locked or unlocked
// A room can contain items
// A room can contain a monster to talk with or fight
function createRoom(name, description, exits, locked, items, monster) {
return {
name,
description,
exits,
locked,
items,
monster
};
}
// Define a monster function that represents a monster in the game
// A monster has a name, description, and a list of items it can drop
// A monster can be talked to or fought
function createMonster(name, description, items) {
return {
name,
description,
items
};
}
// Define an item function that represents an item in the game
// An item has a name, description, and a value
// An item can be picked up and dropped
function createItem(name, description, value) {
return {
name,
description,
value
};
}
// Define a player function that represents the player in the game
// A player has a name, description, and a list of items
// A player can pick up and drop items
// A player can talk to monsters
// A player can fight monsters
// A player has a current location
// A player has a history of all past locations where they've been
// A player can move to a different room
function createPlayer(name, description, items, location, history) {
return {
name,
description,
items,
location,
history
};
}
// Define a game function that represents the game
// A game has a player and a list of rooms
// A game can be started
// A game can be saved
// A game can be loaded
// A game can be ended
function createGame(player, rooms) {
return {
player,
rooms
};
}
// Define a function that starts the game
function startGame() {
// Create the game
const game = createGame(
// Create the player
createPlayer(
"Player 1",
"This is you",
[],
"room1",
[]
),
// Create the rooms
[
createRoom(
"room1",
"This is room 1",
{
north: "room2"
},
false,
[],
null
),
createRoom(
"room2",
"This is room 2",
{
south: "room1"
},
false,
[],
null
)
]
);
// Start the game loop
gameLoop(game);
}
// Define a function that loops the game
// Define a function that displays the current location
function displayLocation(game) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Display the current room
console.log(room.description);
}
// Define a function that displays the current inventory
function displayInventory(game) {
// Get the current inventory
const inventory = game.player.items;
// Display the current inventory
console.log(`You are carrying: ${inventory.join(", ")}`);
}
// Define a function that displays the current history
function displayHistory(game) {
// Get the current history
const history = game.player.history;
// Display the current history
console.log(`History: ${history.join(", ")}`);
}
// Define a function that processes the user input
function processInput(game, input) {
// Process the user input here
// ...
}
// Define a function that loops the game
function gameLoop(game) {
// Display the current location
displayLocation(game);
// Display the current inventory
displayInventory(game);
// Display the current history
displayHistory(game);
// Create a readline interface for reading user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Prompt the user for input
rl.question("What would you like to do? ", (input) => {
// Close the readline interface
rl.close();
// Process the user input
processInput(game, input);
});
}
// Start the game
startGame();
// Define a function that displays the current location
function displayLocation(game) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Display the current room
console.log(room.description);
}
// Define a function that displays the current inventory
function displayInventory(game) {
// Get the current inventory
const inventory = game.player.items;
// Display the current inventory
console.log(`You are carrying: ${inventory.join(", ")}`);
}
// Define a function that displays the current history
function displayHistory(game) {
// Get the current history
const history = game.player.history;
// Display the current history
console.log(`You have been to: ${history.join(", ")}`);
}
// Define a function that processes the user input
function processInput(game, input) {
// Split the input into a command and a target
const [command, target] = input.split(" ");
// Process the command
switch (command) {
case "go":
// Process the go command
processGoCommand(game, target);
break;
case "get":
// Process the get command
processGetCommand(game, target);
break;
case "drop":
// Process the drop command
processDropCommand(game, target);
break;
case "talk":
// Process the talk command
processTalkCommand(game, target);
break;
case "fight":
// Process the fight command
processFightCommand(game, target);
break;
case "save":
// Process the save command
processSaveCommand(game);
break;
case "load":
// Process the load command
processLoadCommand(game);
break;
case "quit":
// Process the quit command
processQuitCommand();
break;
default:
// Process the unknown command
processUnknownCommand(game);
break;
}
}
// Define a function that processes the go command
function processGoCommand(game, target) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Check if the target is a valid exit
if (room.exits[target]) {
// Update the player's location
game.player.location = room.exits[target];
// Update the player's history
game.player.history.push(room.exits[target]);
// Display the current location
displayLocation(game);
} else {
// Display an error message
console.log("You can't go that way!");
}
// Loop the game
gameLoop(game);
}
// Define a function that processes the get command
function processGetCommand(game, target) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Check if the target is a valid item
if (room.items.includes(target)) {
// Remove the item from the room
room.items.splice(room.items.indexOf(target), 1);
// Add the item to the player's inventory
game.player.items.push(target);
// Display the current inventory
displayInventory(game);
} else {
// Display an error message
console.log("You can't get that!");
}
// Loop the game
gameLoop(game);
}
// Define a function that processes the drop command
function processDropCommand(game, target) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Check if the target is a valid item
if (game.player.items.includes(target)) {
// Remove the item from the player's inventory
game.player.items.splice(game.player.items.indexOf(target), 1);
// Add the item to the room
room.items.push(target);
// Display the current inventory
displayInventory(game);
} else {
// Display an error message
console.log("You can't drop that!");
}
// Loop the game
gameLoop(game);
}
// Define a function that processes the talk command
function processTalkCommand(game, target) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Check if the room has a monster
if (room.monster) {
// Display the monster's description
console.log(room.monster.description);
} else {
// Display an error message
console.log("There is no one to talk to!");
}
// Loop the game
gameLoop(game);
}
// Define a function that processes the fight command
function processFightCommand(game, target) {
// Get the current location
const location = game.player.location;
// Get the current room
const room = game.rooms.find(room => room.name === location);
// Check if the room has a monster
if (room.monster) {
// Display the monster's description
console.log(room.monster.description);
} else {
// Display an error message
console.log("There is no one to fight!");
}
// Loop the game
gameLoop(game);
}
// Define a function that processes the save command
function processSaveCommand(game) {
// Save the game
localStorage.setItem("game", JSON.stringify(game));
// Display a success message
console.log("Game saved!");
// Loop the game
gameLoop(game);
}
// Define a function that processes the load command
function processLoadCommand(game) {
// Load the game
const savedGame = JSON.parse(localStorage.getItem("game"));
// Check if a saved game was found
if (savedGame) {
// Display a success message
console.log("Game loaded!");
// Loop the game
gameLoop(savedGame);
} else {
// Display an error message
console.log("No saved game found!");
// Loop the game
gameLoop(game);
}
}
// Define a function that processes the quit command
function processQuitCommand() {
// Display a goodbye message
console.log("Goodbye!");
// Quit the game
return;
}
// Define a function that processes the unknown command
function processUnknownCommand(game) {
// Display an error message
console.log("Unknown command!");
// Loop the game
gameLoop(game);
}
// Start the game
startGame();
// End of game.js
|