-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
137 lines (120 loc) · 3.37 KB
/
main.ts
File metadata and controls
137 lines (120 loc) · 3.37 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
const input = require("prompt-sync")();
const ROWS = 3;
const COLS = 3;
const SYMBOLS_COUNT: { [key: string]: number } = {
A: 4,
B: 6,
C: 8,
D: 10,
};
const SYMBOLS_VALUE: { [key: string]: number } = {
A: 5,
B: 4,
C: 3,
D: 2,
};
const deposit = (): number => {
let deposit: number;
do {
deposit = parseFloat(input("Enter a deposit amount: "));
if (Number.isNaN(deposit) || deposit <= 0) {
console.log(
"Please enter a valid positive number for your deposit."
);
}
} while (Number.isNaN(deposit) || deposit <= 0);
return deposit;
};
const getNumberOfLines = (): number => {
const MIN_LINES = 1;
const MAX_LINES = 3;
while (true) {
const lines = parseFloat(input( `Enter number of lines you want to bet on (${MIN_LINES}-${MAX_LINES}): `));
if (Number.isNaN(lines) || lines < MIN_LINES || lines > MAX_LINES) {
console.log("Invalid number of lines");
} else {
return lines;
}
}
};
const getBetAmount = (balance: number, lines: number): number => {
while (true) {
const bet = parseFloat(input("Enter a bet amount for per line: "));
if (Number.isNaN(bet) || bet <= 0) {
console.log("Invalid bet amount");
} else if (bet * lines > balance) {
console.log("Bet amount exceeds your balance");
} else {
return bet;
}
}
};
// spin and transpose matrix array
const spinAndTranspose = (symbols: string[]): string[][] => {
/**
* Reels is a nested arrow contains 3 symbols each ( total of 9 )
* @example
* [['A','B','A'],['B','C','A'],['C','A','B']]
* */
const reels: string[][] = [];
for (let i = 0; i < COLS; i++) {
const reelSymbols = [...symbols];
const reel: string[] = [];
for (let j = 0; j < ROWS; j++) {
const randomIndex = Math.floor(Math.random() * reelSymbols.length);
reel.push(reelSymbols[randomIndex]);
// removes pushed symbol from array
reelSymbols.splice(randomIndex, 1);
}
reels.push(reel);
}
return reels[0].map((_, i) => reels.map((row) => row[i]));
};
// output reels in a fancy way
const consoleReels = (rows: string[][]): void => {
for (const row of rows) {
const rowString = row.join(" | ");
console.log(rowString);
}
};
const getWinnings = (lines: number, rows: string[][], bet: number): number => {
let winnings = 0;
for (let row = 0; row < lines; row++) {
const symbols = rows[row];
const firstSymbol = symbols[0];
if (symbols.every((sybmol) => sybmol === firstSymbol)) {
winnings += bet * SYMBOLS_VALUE[firstSymbol];
}
}
return winnings;
};
// game controller
const playGame = (): void => {
let balance = deposit();
while (true) {
console.log(`You have a balance of $${balance} to bet`);
const numberOfLines = getNumberOfLines();
const betAmount = getBetAmount(balance, numberOfLines);
balance -= betAmount * numberOfLines;
// gets all possible sybmols to array
const symbols: string[] = Object.entries(SYMBOLS_COUNT).flatMap(
([symbol, count]) => Array(count).fill(symbol)
);
const reels = spinAndTranspose(symbols);
consoleReels(reels);
// determine winning amount
const winnings = getWinnings(numberOfLines, reels, betAmount);
balance += winnings;
console.log(`You won $${winnings}`);
console.log(`Your balance is $${balance}`);
if (balance <= 0) {
console.log(
"You not have enough money to bet, deposit again to continue"
);
break;
}
const playAgain = input("Want to bet again? (y/n) ");
if (playAgain !== "y") break;
}
};
playGame();