Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (110 loc) · 3.73 KB
/
Copy pathscript.js
File metadata and controls
133 lines (110 loc) · 3.73 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
"use strict";
// ====================================
// PROJECT #3: Pig Game
// Selection & Core Logic
// ====================================
// Selecting Elements
const player0El = document.querySelector(".player--0");
const player1El = document.querySelector(".player--1");
const score0El = document.querySelector("#score--0");
const score1El = document.getElementById("score--1");
const current0El = document.getElementById("current--0");
const current1El = document.getElementById("current--1");
const diceEl = document.querySelector(".dice");
const btnNew = document.querySelector(".btn--new");
const btnRoll = document.querySelector(".btn--roll");
const btnHold = document.querySelector(".btn--hold");
// ====================================
// Sound Effect
// ====================================
const clickSound = new Audio("sound/click.mp3");
// ====================================
// Game State Variables
// ====================================
let scores, currentScore, activePlayer, playing;
// Initialization Function
const init = function () {
scores = [0, 0]; // Scores stored in an array [Player 1, Player 2]
currentScore = 0; // Temporary score for current turn
activePlayer = 0; // 0 = Player 1, 1 = Player 2
playing = true; // game state flag
// Reset UI values
score0El.textContent = 0;
score1El.textContent = 0;
current0El.textContent = 0;
current1El.textContent = 0;
// Hide dice at start
diceEl.classList.add("hidden");
// Remove winner classes and reset active player highlight
player0El.classList.remove("player--winner");
player1El.classList.remove("player--winner");
player0El.classList.add("player--active");
player1El.classList.remove("player--active");
};
init();
// Helper Function
function switchPlayer() {
// Reset the current score in the UI for the active player
document.getElementById(`current--${activePlayer}`).textContent = 0;
// Reset Current Score
currentScore = 0;
// Flip activePlayer (0 → 1, or 1 → 0)
activePlayer = activePlayer === 0 ? 1 : 0;
// Toggle highlight between players
player0El.classList.toggle("player--active");
player1El.classList.toggle("player--active");
}
// ==================================
// Rolling The Dice
// ==================================
btnRoll.addEventListener("click", () => {
if (playing) {
clickSound.play(); // play sound on roll
// 1. Generate a random dice roll
const dice = Math.floor(Math.random() * 6) + 1;
// 2. Display the dice
diceEl.classList.remove("hidden");
diceEl.src = `images/dice-${dice}.png`;
// 3. Check for rolled 1
if (dice !== 1) {
currentScore += dice;
document.getElementById(`current--${activePlayer}`).textContent =
currentScore;
} else {
switchPlayer();
}
}
});
// ==================================
// Holding The Score
// ==================================
btnHold.addEventListener("click", () => {
if (playing) {
clickSound.play(); // play sound on hold
// 1. Add currentScore to active player's total
scores[activePlayer] += currentScore;
// 2. Update the UI
document.getElementById(`score--${activePlayer}`).textContent =
scores[activePlayer];
// 3. Check for win
if (scores[activePlayer] >= 100) {
playing = false;
diceEl.classList.add("hidden");
document
.querySelector(`.player--${activePlayer}`)
.classList.add("player--winner");
document
.querySelector(`.player--${activePlayer}`)
.classList.remove("player--active");
} else {
switchPlayer();
}
}
});
// ==================================
// Reset Game
// ==================================
btnNew.addEventListener("click", () => {
clickSound.play(); // play sound on reset
init();
});