-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
147 lines (138 loc) · 5.85 KB
/
app.js
File metadata and controls
147 lines (138 loc) · 5.85 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
138
139
140
141
142
143
144
145
146
147
import { createBoard } from "./script.js";
const boardElement = document.querySelector(".board");
const text_1 = document.querySelector('.txt-1')
const text_2 = document.querySelector('.txt-2')
const betBtn = document.getElementById('bet-btn');
const withdrawBtn = document.getElementById('withdraw-btn');
const mineInput = document.querySelector(".mineNum")
let MINE_COUNT = 0;
const currScore = document.querySelector(".curr-score");
let score = 0;
const BOARD_SIZE = 5;
const money = document.querySelector('.wallet-money');
const wallet = document.getElementById('wallet-btn');
const amountInput = document.querySelector(".amount");
let betAmount = 0;
window.onload = () => {
amountInput.max = parseInt(money.innerText);
}
function checkInput(){
betAmount = parseInt(amountInput.value);
const amountFilled = (amountInput.value.trim() !== "" && amountInput.value > 0 && amountInput.value <= parseInt(money.innerText));
const mineFilled = MINE_COUNT > 0 && MINE_COUNT<25;
betBtn.disabled = !(amountFilled && mineFilled);
}
mineInput.addEventListener("input", (e) => {
MINE_COUNT = parseInt(e.target.value);
});
amountInput.addEventListener("input", checkInput);
mineInput.addEventListener("input", checkInput);
betBtn.addEventListener('click', () => {
text_1.innerText = 'SCORE';
text_2.innerText = '0';
const board = createBoard(BOARD_SIZE, MINE_COUNT);
boardElement.innerHTML = "";
boardElement.style.setProperty("--size", BOARD_SIZE);
board.forEach(row => {
row.forEach(cell => {
boardElement.appendChild(cell.element);
cell.element.addEventListener("click", () => {
if(cell.status === "hidden"){
if(cell.mine){
cell.status = "mine";
cell.element.classList.add("mine");
score = 0;
currScore.innerText = score;
withdrawBtn.disabled = true;
withdrawBtn.classList.add('hide');
amountInput.value = "";
mineInput.value = "";
setTimeout(() => {
alert("Game Over! You hit a mine!");
}, 800);
setTimeout(() => {
board.forEach(row => {
row.forEach(cell => {
if(cell.mine){
cell.status = "mine";
cell.element.classList.add("mine");
} else{
cell.status = "safe";
cell.element.classList.add("safe");
}
});
});
setTimeout(() => {
boardElement.innerHTML = "";
boardElement.style.setProperty("--size", 0);
}, 2000);
}, 1000);
money.innerText = (Math.max(0, parseInt(money.innerText) - betAmount)).toFixed(2);
updateMoney(money.innerText);
} else{
cell.status = "safe";
cell.element.classList.add("safe");
score += 1;
if(score > 0){
withdrawBtn.disabled = false;
withdrawBtn.classList.remove('hide');
}
currScore.innerText = score;
if(score === (BOARD_SIZE * BOARD_SIZE) - MINE_COUNT){
alert(`You win ${calculateReward()}$! You cleared the board!`);
money.innerText = (parseInt(money.innerText) + calculateReward()).toFixed(2);
updateMoney(money.innerText);
score = 0;
currScore.innerText = score;
boardElement.innerHTML = "";
betBtn.disabled = false;
withdrawBtn.disabled = true;
withdrawBtn.classList.add('hide');
}
}
}
});
});
});
betBtn.disabled = true;
});
withdrawBtn.addEventListener('click', () => {
if(score > 0){
const totalAmount = calculateReward();
score = 0;
currScore.innerText = 0;
betBtn.disabled = false;
withdrawBtn.disabled = true;
withdrawBtn.classList.add('hide');
alert(`You have withdrawn ${totalAmount}$!`);
money.innerText = (totalAmount + parseInt(money.innerText)).toFixed(2);
updateMoney(money.innerText);
boardElement.innerHTML = "";
boardElement.style.setProperty("--size", 0);
} else{
alert("You have no score to withdraw!");
}
});
function calculateReward(){
const safeCells = 25 - MINE_COUNT;
const baseMultiplier = 1 + (MINE_COUNT / 24);
let currentMultiplier = baseMultiplier;
for(let i=1; i <= score; i++){
const progress = i/safeCells;
currentMultiplier *= (1 + progress * 0.1);
}
return +(betAmount * currentMultiplier).toFixed(2);
}
let savedMoney = localStorage.getItem('moneyLeft');
if(savedMoney != null){
money.innerText = savedMoney;
}
function updateMoney(newAmount){
amountInput.max = newAmount;
localStorage.setItem('moneyLeft', newAmount);
}
wallet.addEventListener('click', () => {
localStorage.clear();
money.innerText = 100;
updateMoney(money.innerText);
});