-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
123 lines (110 loc) · 4.9 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
let squareTiles = document.querySelectorAll(".box");
let playButton = document.querySelector("#play-button");
let cashOutButton = document.querySelector("#cashOut-button");
let riskSelect = document.querySelector("#risk");
let betInput = document.querySelector("#amountInput");
let betMoney;
let accountMoney = document.querySelector("#money");
let moneyTrack = 0;
let probHappening = 1;
let cashOut = 0;
let edge = 0.13;
let winCount = 0;
let bombsAmount = 10;
let isPlaying = false;
let tilesAmounts = squareTiles.length;
riskSelect.addEventListener("change", () => {
bombsAmount = riskSelect.value;
console.log(bombsAmount);
});
squareTiles.forEach((squareTile) => {
squareTile.addEventListener("click", () => {
if (isPlaying) {
let currentWinChance = (1 - bombsAmount / tilesAmounts);
console.log(`WIN CHANCE : ${currentWinChance}, LOSE CHANCE : ${1 - currentWinChance} , TA : ${tilesAmounts}, BA : ${bombsAmount} `);
squareTile.style["backgroundColor"] = "rgb(87, 87, 87)";
if (!squareTile.classList.contains("clicked")) {
squareTile.classList.add("clicked");
if (Math.random() < bombsAmount / tilesAmounts) {
isPlaying = false;
playButton.disabled = false;
cashOutButton.disabled = true;
cashOutButton.textContent = "Cash Out";
// LOSE
squareTile.innerHTML = `<img src="bombedit.png" alt="" srcset="">`;
// squareTile.style["outline"] = "3px solid red";
let remainingSquareTiles = document.querySelectorAll(".box:not(.clicked)");
let tempBombAmount = bombsAmount - 1;
let tempSquareTiles = remainingSquareTiles.length;
console.log(`Bomb Left : ${tempBombAmount}, Tiles Left : ${tempSquareTiles}`);
remainingSquareTiles.forEach((ele) => {
ele.style["background-color"] = "rgb(87, 87, 87)";
if (Math.random() < tempBombAmount / Math.max(tempSquareTiles, 1)) {
// ele.style["outline"] = "3px solid red";
ele.innerHTML = `<img src="bombedit.png" alt="">`;
tempBombAmount--;
}
else {
// ele.style["outline"] = "3px solid blue";
ele.innerHTML = `<img src="diamondedit.png" alt="">`;
}
tempSquareTiles--;
})
} else {
// WIN
// squareTile.style["outline"] = "3px solid blue";
squareTile.innerHTML = `<img src="diamondedit.png" alt="">`;
winCount++;
probHappening = probHappening * currentWinChance;
cashOut = (1 / probHappening) - (edge * winCount);
console.log("Probability Happening : " + probHappening);
console.log("Cashout : " + cashOut);
cashOutButton.textContent = (cashOut * betMoney).toFixed(2);
}
} else {
console.log("ALREADY CLICKED");
}
tilesAmounts--;
} else {
console.log("Press Play Button to Start");
}
});
});
playButton.addEventListener("click", () => {
tilesAmounts = squareTiles.length;
probHappening = 1;
winCount = 0;
cashOut = 0;
console.log("TA : " + tilesAmounts + " BA : " + bombsAmount);
if (betInput.value < 0 || betInput.value == "") {
console.log("Invalid Bet");
} else {
betMoney = betInput.value;
console.log("AM : " + parseFloat(accountMoney.textContent) + " BM : " + parseFloat(betMoney));
moneyTrack = (moneyTrack - parseFloat(betMoney));
accountMoney.textContent = moneyTrack;
playButton.disabled = true;
cashOutButton.disabled = false;
squareTiles.forEach((squareTile) => {
squareTile.classList.remove("clicked");
squareTile.textContent = "";
squareTile.style["backgroundColor"] = "grey";
});
isPlaying = true;
}
});
cashOutButton.addEventListener("click", () => {
moneyTrack = parseFloat(moneyTrack) + (cashOut * betMoney);
console.log("Money Track 1 : " + moneyTrack);
accountMoney.textContent = moneyTrack.toFixed(2).toLocaleString('en');
isPlaying = false;
cashOutButton.disabled = true;
playButton.disabled = false;
cashOutButton.textContent = "Cash Out";
squareTiles.forEach((squareTile) => {
squareTile.classList.remove("clicked");
squareTile.textContent = "";
squareTile.style["backgroundColor"] = "grey";
squareTile.style["outline"] = "none";
});
});