-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-001.js
61 lines (53 loc) · 1.35 KB
/
generation-001.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
/**
* CHANGELOG:
* Generation 0: implement base script
* Generation 1: added basic game loop
*/
import { generations, evolve } from "./base.js";
import readline from "readline";
const generation = 1;
console.log(`Generation ${generation} spawned`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
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");
} else if (input.toLowerCase() === "quit") {
console.log("Thanks for playing!");
break;
} 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!");
}