-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
160 lines (130 loc) · 3.64 KB
/
project.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
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
const prompt = require("prompt-sync")();
const Rows=3;
const Cols=3;
const symbols_count = {
"A" : 2,
"B" : 4,
"C" : 6,
"D" : 8
}
const symbols_values = {
"A" : 5,
"B" : 4,
"C" : 3,
"D" : 2
}
const deposit = () => {
while(true){
const balance = prompt("Amount of deposit: ");
const numberdepositamount = parseFloat(balance);
if(isNaN(numberdepositamount) || numberdepositamount <= 0){
console.log("please try again ");
}else{
return numberdepositamount;
}
}
};
// console.log(deposit());
const getnumberoflines = () => {
while(true){
const lines = prompt("Number of lines you wanna play(1-3) : ");
const numberoflines = parseFloat(lines);
if(isNaN(numberoflines) || numberoflines <= 0 || numberoflines >3){
console.log("please try again ");
}else{
return numberoflines;
}
}
};
// console.log(getnumberoflines());
const getbet = (balance,lines) => {
while(true){
const bet = prompt("How much do you wanna bet per line? : ");
const numberbet = parseFloat(bet);
if(isNaN(numberbet) || numberbet <= 0 || numberbet > balance/lines ){
console.log("Invalid bet, please try again ");
}else{
return numberbet;
}
}
};
const spin = () =>{
const symbols=[];
for(const[symbol,count] of Object.entries(symbols_count)){
for(let i=0; i <count ; i++){
symbols.push(symbol);
}
}
const reel = [];
for(let i=0; i < Cols ;i++ ){
reel.push([]);
const reelsymbols = [...symbols];
for(let j=0; j <Rows; j++){
const randomindex = Math.floor(Math.random()*reelsymbols.length);
reel[i].push(reelsymbols[randomindex]);
reelsymbols.splice(randomindex,1);
}
}
return reel;
};
const transpose = (reel)=>{
const rows = [];
for(let i=0; i< Rows; i++){
rows.push([]);
for(let j=0; j< Cols; j++){
rows[i].push(reel[j][i]);
}
}
return rows;
};
const printrow = (rows)=>{
for(const row of rows){
let rowstring = "";
for(const [i,symbol] of row.entries()){
rowstring += symbol;
if(i != row.length - 1){
rowstring += " | ";
}
}
console.log(rowstring);
}
};
const getwinnings = (rows, bet, lines)=>{
let winnings = 0;
for(let row=0; row < lines; row++){
const symbols = rows[row];
let allsame = true;
for( const symbol of symbols){
if(symbol != symbols[0]){
allsame = false;
break;
}
}
if(allsame){
winnings += bet*symbols_values[symbols[0]];
}
}
return winnings;
};
const game = ()=>{
let balance= deposit();
while(true){
console.log("you have a balance of $"+balance);
const numberoflines= getnumberoflines();
const bet = getbet(balance, numberoflines);
balance -= bet * numberoflines;
const reels = spin();
const rows = transpose(reels);
printrow(rows);
const winnings = getwinnings(rows, bet, numberoflines);
balance += winnings;
console.log("You won, $" + winnings.toString());
if (balance <= 0) {
console.log("You ran out of money!");
break;
}
const playAgain = prompt("Do you want to play again (y/n)? ");
if (playAgain != "y"){ break;}
}
};
game();