-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
192 lines (168 loc) · 5.21 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
document.addEventListener("DOMContentLoaded", () => {
rollDice();
showInfoMsg("Click on Roll to begin the game");
});
const rollBtn = document.getElementById("roll-btn");
const turnOverBtn = document.getElementById("turn-over-btn");
const die1 = document.getElementById("die1");
const die2 = document.getElementById("die2");
const dice = document.querySelectorAll(".dice");
const turnPronounElement = document.querySelector("#turn > span");
const turnScoreElement = document.querySelector("#turn-score > div > span");
const playerScoreElement = document.querySelector("#your-score > p");
const computerScoreElement = document.querySelector("#computer-score > p");
const winningScore = 100;
const overlay = document.getElementById("overlay");
const startBtn = document.getElementById("start-btn");
startBtn.addEventListener("click", () => {
overlay.style.display = "none";
});
let user = {
playerScore: 0,
pronoun: "You",
possessivePronoun: "Your",
set score(value) {
this.playerScore = value;
playerScoreElement.textContent = this.playerScore;
},
get score() {
return this.playerScore;
},
};
let computer = {
playerScore: 0,
pronoun: "Computer",
possessivePronoun: "Computer's",
set score(value) {
this.playerScore = value;
computerScoreElement.textContent = this.playerScore;
},
get score() {
return this.playerScore;
},
};
const currentTurn = {
turnScore: 0,
currentPlayer: user,
set score(value) {
this.turnScore = value;
turnScoreElement.textContent = this.turnScore;
},
get score() {
return this.turnScore;
},
set player(otherPlayer) {
this.currentPlayer = otherPlayer;
turnPronounElement.textContent = this.currentPlayer.possessivePronoun;
},
get player() {
return this.currentPlayer;
},
switchPlayer: function () {
if (this.currentPlayer === user) {
rollBtn.setAttribute("disabled", true);
turnOverBtn.setAttribute("disabled", true);
this.player = computer;
emulateComputerTurn();
} else {
setTimeout(() => {
rollBtn.removeAttribute("disabled");
turnOverBtn.removeAttribute("disabled");
}, 400);
this.player = user;
}
turnPronounElement.textContent = this.currentPlayer.possessivePronoun;
},
};
rollBtn.addEventListener("click", rollAndValidate);
turnOverBtn.addEventListener("click", () => {
turnOver();
showInfoMsg("You turned over, you got " + currentTurn.score + " points");
});
function turnOver() {
currentTurn.player.score += currentTurn.score;
setTimeout(() => {
currentTurn.score = 0;
}, 400);
if (currentTurn.player.score >= winningScore) {
playerWins(currentTurn.player);
} else {
currentTurn.switchPlayer();
}
}
function rollAndValidate() {
const rollDetails = rollDice();
const validationResult = rollDetails.validateRoll();
const player = currentTurn.player;
if (validationResult === 1) {
currentTurn.score += rollDetails.rollValue;
if (currentTurn.player.score + currentTurn.score >= winningScore) {
turnOver();
} else if (currentTurn.player === computer) {
if (currentTurn.score >= 20) {
showInfoMsg("Computer got " + player.score + " points");
turnOver();
} else {
emulateComputerTurn();
}
}
} else {
currentTurn.score = 0;
if (validationResult === -1) {
currentTurn.player.score = 0;
showInfoMsg(`${player.pronoun} rolled two 1's!`);
} else {
showInfoMsg(`${player.pronoun} rolled a 1!`);
}
turnOver();
}
}
function playerWins(player) {
setTimeout(() => {
alert(player.pronoun + " Won!!! with " + player.score + " points");
}, 100);
setTimeout(() => {
resetGame();
}, 100);
}
function resetGame() {
user.score = 0;
computer.score = 0;
rollBtn.removeAttribute("disabled");
turnOverBtn.removeAttribute("disabled");
currentTurn.player = user;
currentTurn.score = 0;
}
function rollDice() {
let audio = new Audio("./media/dice-roll.mp3");
audio.play();
return {
die1Value: rollDie(dice[0]),
die2Value: rollDie(dice[1]),
validateRoll: function () {
if (this.die1Value === 1 && this.die2Value === 1) {
return -1;
} else if (this.die1Value === 1 || this.die2Value === 1) {
return 0;
} else {
return 1;
}
},
get rollValue() {
return this.die1Value + this.die2Value;
},
};
}
function rollDie(die) {
let randomNumber = Math.floor(Math.random() * 6);
const dieOptions = ["one", "two", "three", "four", "five", "six"];
die.className = `dice ${dieOptions[randomNumber]}`;
return randomNumber + 1;
}
function emulateComputerTurn() {
setTimeout(rollAndValidate, 2300);
}
function showInfoMsg(message) {
const infoBox = document.querySelector("#info-box > div > p");
infoBox.textContent = message;
}