-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-002.js
80 lines (71 loc) · 2.01 KB
/
generation-002.js
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
/**
* CHANGELOG:
* Generation 0: implement base script
* Generation 1: added basic game loop
* Generation 2: added player object and inventory system
*/
import { generations, evolve } from "./base.js";
import readline from "readline";
const generation = 2;
console.log(`Generation ${generation} spawned`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const player = {
name: "",
inventory: [],
};
async function output() {
console.log("Welcome to the Text Based RPG!");
console.log("Type 'help' for a list of commands.");
}
async function gameLoop() {
while (true) {
const input = await askQuestion("What would you like to do? ");
if (input.toLowerCase() === "help") {
console.log("Available commands:");
console.log("- help: display this help message");
console.log("- quit: exit the game");
console.log("- name: set your player name");
console.log("- inventory: show your inventory");
} else if (input.toLowerCase() === "quit") {
console.log("Thanks for playing!");
break;
} else if (input.toLowerCase() === "name") {
const name = await askQuestion("What is your name? ");
player.name = name;
console.log(`Your name is now ${player.name}.`);
} else if (input.toLowerCase() === "inventory") {
if (player.inventory.length === 0) {
console.log("Your inventory is empty.");
} else {
console.log("Your inventory:");
player.inventory.forEach((item) => console.log(`- ${item}`));
}
} else {
console.log("Invalid command. Type 'help' for a list of commands.");
}
}
}
function askQuestion(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
async function startGame() {
await output();
await gameLoop();
}
startGame();
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
console.log("GOAL achieved!");
}