-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonopoly.js
58 lines (51 loc) · 1.66 KB
/
monopoly.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
const dice = document.querySelector("#roll-die");
const reset = document.querySelector("#reset");
const dice01 = document.querySelector("#dice01");
const dice02 = document.querySelector("#dice02");
const remaining = document.querySelector("#remaining");
const cellWrapper = document.querySelector("#cell-wrapper");
const jail = document.querySelector("#jail");
let triesRemaining = 3;
/* This function returns a random number between 1 and 6 */
function diceRoll() {
return Math.floor(Math.random() * (6 - 1) + 1) + 1;
}
/* Roll the dice when the button is clicked.
If the numbers match within 3 tries, you are out of jail */
dice.addEventListener("click", () => {
if (triesRemaining >= 1) {
let roll01 = diceRoll();
let roll02 = diceRoll();
dice01.innerHTML = roll01;
dice02.innerHTML = roll02;
triesRemaining -= 1;
remaining.innerHTML = triesRemaining;
/* If dice match, exit with success */
if (roll01 === roll02) {
dice.classList = "disabled";
reset.classList = "enabled";
jail.classList = "free";
cellWrapper.classList = "freed";
exit;
} else {
/* If 3 attempts have been made, game over */
if (triesRemaining == 0) {
dice.classList = "disabled";
reset.classList = "enabled";
cellWrapper.classList = "jailed";
jail.classList = "sorry";
}
}
} // end tries > 1
});
/* Reset to the beginning */
reset.addEventListener("click", () => {
triesRemaining = 3;
remaining.innerHTML = triesRemaining;
dice01.innerHTML = "*";
dice02.innerHTML = "*";
dice.classList = "";
reset.classList = "";
cellWrapper.classList = "";
jail.classList = "";
});