cli-slot-game 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +88 -0
- package/package.json +21 -0
package/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import inquirer from "inquirer";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import figlet from "figlet";
|
|
5
|
+
|
|
6
|
+
const SYMBOLS = ["š", "š", "š", "š", "7ļøā£"];
|
|
7
|
+
let balance = 100;
|
|
8
|
+
|
|
9
|
+
function sleep(ms) {
|
|
10
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function spinReels() {
|
|
14
|
+
return [0, 1, 2].map(() => SYMBOLS[Math.floor(Math.random() * SYMBOLS.length)]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function animateSpin() {
|
|
18
|
+
let finalReels;
|
|
19
|
+
process.stdout.write("\n");
|
|
20
|
+
for (let i = 0; i < 15; i++) {
|
|
21
|
+
finalReels = spinReels();
|
|
22
|
+
process.stdout.write(`\rš° ${finalReels.join(" | ")} š°`);
|
|
23
|
+
await sleep(80);
|
|
24
|
+
}
|
|
25
|
+
process.stdout.write("\n");
|
|
26
|
+
return finalReels;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function evaluate(reels, bet) {
|
|
30
|
+
const [a, b, c] = reels;
|
|
31
|
+
if (a === b && b === c) return { win: true, amount: bet * 10, jackpot: true };
|
|
32
|
+
if (a === b || b === c || a === c) return { win: true, amount: bet * 2, jackpot: false };
|
|
33
|
+
return { win: false, amount: bet, jackpot: false };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function banner(text, color) {
|
|
37
|
+
console.log(chalk[color](figlet.textSync(text, { font: "Standard" })));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function playRound() {
|
|
41
|
+
const { bet } = await inquirer.prompt([
|
|
42
|
+
{
|
|
43
|
+
type: "number",
|
|
44
|
+
name: "bet",
|
|
45
|
+
message: `Balance: $${balance}. How much do you want to bet?`,
|
|
46
|
+
validate: (val) => (val > 0 && val <= balance ? true : "Enter a valid amount within your balance"),
|
|
47
|
+
},
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const reels = await animateSpin();
|
|
51
|
+
const result = evaluate(reels, bet);
|
|
52
|
+
|
|
53
|
+
if (result.win) {
|
|
54
|
+
balance += result.amount - bet;
|
|
55
|
+
if (result.jackpot) {
|
|
56
|
+
banner("JACKPOT!", "yellowBright");
|
|
57
|
+
}
|
|
58
|
+
console.log(chalk.green(`ā You won $${result.amount}! New balance: $${balance}`));
|
|
59
|
+
} else {
|
|
60
|
+
balance -= bet;
|
|
61
|
+
console.log(chalk.red(`ā No match. You lost $${bet}. New balance: $${balance}`));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function play() {
|
|
66
|
+
banner("Slots", "cyanBright");
|
|
67
|
+
console.log(chalk.gray("Starting balance: $100\n"));
|
|
68
|
+
|
|
69
|
+
while (balance > 0) {
|
|
70
|
+
await playRound();
|
|
71
|
+
|
|
72
|
+
if (balance <= 0) {
|
|
73
|
+
banner("BUST", "redBright");
|
|
74
|
+
console.log(chalk.red("You're out of money. Game over."));
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const { again } = await inquirer.prompt([
|
|
79
|
+
{ type: "confirm", name: "again", message: "Spin again?", default: true },
|
|
80
|
+
]);
|
|
81
|
+
if (!again) {
|
|
82
|
+
console.log(chalk.cyan(`\nCashing out with $${balance}. Nice run.`));
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
play();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cli-slot-game",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"cli-slot-game": "./index.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"chalk": "^5.6.2",
|
|
18
|
+
"figlet": "^1.11.0",
|
|
19
|
+
"inquirer": "^14.0.2"
|
|
20
|
+
}
|
|
21
|
+
}
|