-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
318 lines (275 loc) · 9.98 KB
/
script.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Element Selector
const dice1 = document.getElementById("dice-1");
const dice2 = document.getElementById("dice-2");
const rollButton = document.getElementById("rollButton");
// const totalElement = document.getElementById("total");
const historyContainer = document.getElementById("hiscon");
var totalValue;
// Execute Dice Roll
const rollDice = (dice1Val, dice2Val) => {
if(totalValue)
{
getNormal(totalValue)
}
// Animate Dices to Roll
if (!dice1.classList.contains("rolling")) dice1.classList.add("rolling");
if (!dice2.classList.contains("rolling")) dice2.classList.add("rolling");
setTimeout(() => {
// Set dices to values after a certain duration of rolling
dice1.dataset.face = dice1Val;
dice2.dataset.face = dice2Val;
// Remove rolling animation
if (dice1.classList.contains("rolling")) dice1.classList.remove("rolling");
if (dice2.classList.contains("rolling")) dice2.classList.remove("rolling");
// Calculate and display the total value with color coding
totalValue = dice1Val + dice2Val;
// totalElement.innerHTML = "Your roll is " + totalValue;
// // Apply color coding based on the specified ranges
// if (totalValue >= 1 && totalValue <= 6) {
// totalElement.style.color = "red";
// } else if (totalValue >= 8 && totalValue <= 12) {
// totalElement.style.color = "green";
// } else {
// totalElement.style.color = "blue";
// }
// Add the total value to the history container with color coding
// addToHistory(totalValue);
checkGuess(totalValue);
const history = document.createElement('div');
history.innerText = totalValue;
history.classList.add('container')
history.classList.add('border')
history.classList.add('border-light')
history.classList.add('text-light')
history.classList.add('text-center')
history.classList.add('mx-2')
history.classList.add('px-3')
history.style.borderRadius = "15px"
history.style.margin = "13px 0px 0px 0px"
historyContainer.prepend(history);
}
, 3000
);
};
// Trigger Dices to Roll when clicking at different sections of the rollButton
rollButton.addEventListener("click", function (e) {
const buttonRect = rollButton.getBoundingClientRect();
const clickX = e.clientX - buttonRect.left;
if (clickX < buttonRect.width / 3) {
// Left section: Get random numbers between 2 and 6
const dice1Val = Math.floor(Math.random() * 3) + 1;
const dice2Val = Math.floor(Math.random() * 3) + 1;
// const dice1Val = 2;
// const dice2Val = 2;
e.preventDefault();
rollDice(dice1Val, dice2Val);
} else if (clickX < (2 * buttonRect.width) / 3) {
// Center section: Fixed values for a total of seven
const dice1Val = Math.floor(Math.random() * 6) + 1;
const dice2Val = 7 - dice1Val;
e.preventDefault();
rollDice(dice1Val, dice2Val);
} else {
// Right section: Get random numbers between 8 and 12
const dice1Val = Math.floor(Math.random() * 3) + 4;
const dice2Val = Math.floor(Math.random() * 3) + 4;
// const dice1Val = 5;
// const dice2Val = 5;
e.preventDefault();
rollDice(dice1Val, dice2Val);
}
});
// Trigger Dices to Roll
// rollButton.addEventListener("click", function (e) {
// e.preventDefault();
// rollDice();
// });
let balance = 500;
let bettingAmounts = {
1: 0,
2: 0,
3: 0,
};
// let bettingdone = {
// 1: false,
// 2: false,
// 3: false,
// };
let selectedGuess = 0;
function addMoney(amount) {
if (selectedGuess !== 0) {
if (balance >= amount) {
balance -= amount;
bettingAmounts[selectedGuess] += amount;
updateDisplay();
} else {
alert("Insufficient balance!");
}
} else {
alert("Please select a guess section first.");
}
}
function makeGuess(guess) {
selectedGuess = guess;
updateDisplay();
}
// function checkGuess(totalValue) {
// if (
// (selectedGuess == 1 && totalValue <= 6) ||
// (selectedGuess == 2 && totalValue === 7) ||
// (selectedGuess == 3 && totalValue >= 8)
// ) {
// // Guess is correct
// balance += bettingAmounts[selectedGuess] * 2;
// bettingAmounts[selectedGuess] *= 2;
// updateDisplay();
// if (selectedGuess == 1)
// alert(`Congratulations! Your guess 1-6 is correct. You won $${bettingAmounts[selectedGuess] * 2}`);
// else if (selectedGuess == 2)
// alert(`Congratulations! Your guess 7 is correct. You won $${bettingAmounts[selectedGuess] * 2}`);
// else if (selectedGuess == 3)
// alert(`Congratulations! Your guess 8-12 is correct. You won $${bettingAmounts[selectedGuess] * 2}`);
// } else {
// // Guess is incorrect
// bettingAmounts[selectedGuess] = 0;
// updateDisplay();
// if (selectedGuess == 1)
// alert(`Oops! Your guess 1-6 is incorrect. You lost $${bettingAmounts[selectedGuess]}`);
// else if (selectedGuess == 2)
// alert(`Oops! Your guess 7 is incorrect. You lost $${bettingAmounts[selectedGuess]}`);
// else if (selectedGuess == 3)
// alert(`Oops! Your guess 8-12 is incorrect. You lost $${bettingAmounts[selectedGuess]}`);
// }
// }
function checkGuess(totalValue) {
if (totalValue <= 6) {
bettingAmounts[1] *= 2;
bettingAmounts[2] = 0;
bettingAmounts[3] = 0;
// if (selectedGuess == 1) {
// balance += bettingAmounts[1];
// }
updateDisplay();
let list = document.getElementById("1-6-outer");
list.classList.remove("bg-transparent");
list.classList.add("bg-light");
let list2 = document.getElementById("1-6");
list2.classList.remove("text-light");
list2.classList.add("text-primary");
const win = document.createElement("div");
win.innerText = "Winner";
win.classList.add("text-primary");
win.classList.add("fs-1");
win.classList.add("bg-transparent");
win.style.margin = "20px 0px 0px 110px";
document.getElementById("1-6-win").appendChild(win);
} else if (totalValue == 7) {
bettingAmounts[1] = 0;
bettingAmounts[2] *= 3;
bettingAmounts[3] = 0;
// if (selectedGuess == 2) {
// balance += bettingAmounts[2];
// }
updateDisplay();
let list = document.getElementById("777-outer");
list.classList.remove("bg-transparent");
list.classList.add("bg-light");
let list2 = document.getElementById("777");
list2.classList.remove("text-light");
list2.classList.add("text-primary");
const win = document.createElement("div");
win.innerText = "Winner";
win.classList.add("text-primary");
win.classList.add("fs-2");
win.classList.add("bg-transparent");
win.style.margin = "0px 0px 0px 120px";
document.getElementById("777-win").prepend(win);
} else if (totalValue >= 8) {
bettingAmounts[1] = 0;
bettingAmounts[2] = 0;
bettingAmounts[3] *= 2;
// if (selectedGuess == 3) {
// balance += bettingAmounts[3];
// }
updateDisplay();
let list = document.getElementById("8-12-outer");
list.classList.remove("bg-transparent");
list.classList.add("bg-light");
let list2 = document.getElementById("8-12");
list2.classList.remove("text-light");
list2.classList.add("text-primary");
const win = document.createElement("div");
win.innerText = "Winner";
win.classList.add("text-primary");
win.classList.add("fs-1");
win.classList.add("bg-transparent");
win.style.margin = "20px 0px 0px 110px";
document.getElementById("8-12-win").appendChild(win);
}
}
function rollDice2(totalValue) {
// const dice1 = Math.floor(Math.random() * 6) + 1;
// const dice2 = Math.floor(Math.random() * 6) + 1;
// const totalValue = dice1 + dice2;
checkGuess(totalValue);
// Display total value and update history
// document.getElementById("totalValue").textContent = totalValue;
// document.getElementById("history").textContent += totalValue + ' ';
}
function updateDisplay() {
document.getElementById("balanceAmount").textContent = balance;
document.getElementById(
"bettingAmount1"
).textContent = `₹${bettingAmounts[1]}`;
document.getElementById(
"bettingAmount2"
).textContent = `₹${bettingAmounts[2]}`;
document.getElementById(
"bettingAmount3"
).textContent = `₹${bettingAmounts[3]}`;
// Remove the 'selected' class from all buttons
const buttons = document.querySelectorAll(".guess-button");
buttons.forEach((button) => button.classList.remove("selected"));
// Add the 'selected' class to the currently selected button
if (selectedGuess !== 0) {
const selectedButton = document.getElementById(`button${selectedGuess}`);
if (selectedButton) {
selectedButton.classList.add("selected");
}
}
}
function getNormal(totalValue)
{
if(totalValue <= 6)
{
let list = document.getElementById("1-6-outer");
list.classList.add("bg-transparent");
list.classList.remove("bg-light");
let list2 = document.getElementById("1-6");
list2.classList.add("text-light");
list2.classList.remove("text-primary");
const win = document.getElementById("1-6-win")
win.removeChild(win.lastElementChild);
}
else if(totalValue == 7)
{
let list = document.getElementById("777-outer");
list.classList.add("bg-transparent");
list.classList.remove("bg-light");
let list2 = document.getElementById("777");
list2.classList.add("text-light");
list2.classList.remove("text-primary");
const win = document.getElementById("777-win")
win.removeChild(win.lastElementChild.previousElementSibling);
}
else if (totalValue >= 8){
let list = document.getElementById("8-12-outer");
list.classList.add("bg-transparent");
list.classList.remove("bg-light");
let list2 = document.getElementById("8-12");
list2.classList.add("text-light");
list2.classList.remove("text-primary");
const win = document.getElementById("8-12-win")
win.removeChild(win.lastElementChild);
}
}