-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
219 lines (187 loc) Β· 7.23 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
document.addEventListener('DOMContentLoaded', function () {
// Your main.js code here
// 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');
const btnSetScore = document.querySelector('.btn--set-score');
const inputWinningScore = document.getElementById('winning-score');
const modeToggle = document.getElementById('game-mode-toggle');
const modeLabel = document.getElementById('mode-label');
const playerName0 = document.getElementById('name--0');
const playerName1 = document.getElementById('name--1');
const aiDifficulty = document.getElementById('ai-difficulty'); // Difficulty selector
let scores, currentScore, activePlayer, playing, winningScore, singlePlayerMode;
// Initialize game
const init = function () {
scores = [0, 0];
currentScore = 0;
activePlayer = 0;
playing = true;
winningScore = inputWinningScore.value ? Number(inputWinningScore.value) : 100; // Default or set winning score
singlePlayerMode = modeToggle.checked;
score0El.textContent = 0;
score1El.textContent = 0;
current0El.textContent = 0;
current1El.textContent = 0;
diceEl.classList.add('hidden');
player0El.classList.remove('player--winner');
player1El.classList.remove('player--winner');
player0El.classList.add('player--active');
player1El.classList.remove('player--active');
// Update player names based on mode
if (singlePlayerMode) {
playerName0.textContent = 'You';
playerName1.textContent = 'AI';
} else {
playerName0.textContent = 'Player 1';
playerName1.textContent = 'Player 2';
}
modeLabel.textContent = singlePlayerMode ? 'Single Player' : 'Multiplayer';
// Enable buttons for the human player
btnRoll.disabled = false;
btnHold.disabled = false;
};
init();
// Switch player function
const switchPlayer = function () {
document.getElementById(`current--${activePlayer}`).textContent = 0;
currentScore = 0;
activePlayer = activePlayer === 0 ? 1 : 0;
player0El.classList.toggle('player--active');
player1El.classList.toggle('player--active');
// Play switch turn sound
switchTurnSound.play();
if (singlePlayerMode && activePlayer === 1) {
// AI's turn
aiTurn();
} else {
// Enable buttons for the human player
btnRoll.disabled = false;
btnHold.disabled = false;
}
};
// AI Decision Logic Based on Difficulty
const aiTurn = function () {
btnRoll.disabled = true;
btnHold.disabled = true;
const difficulty = aiDifficulty.value; // Get selected difficulty
// Different behavior based on difficulty
let holdThreshold, rollDelay;
if (difficulty === 'easy') {
holdThreshold = 15; // AI holds with a lower score, takes more risks
rollDelay = Math.random() * 500 + 1000; // Slower rolls
} else if (difficulty === 'medium') {
holdThreshold = 20; // Balanced approach
rollDelay = Math.random() * 1000 + 2500; // Moderate speed
} else if (difficulty === 'hard') {
holdThreshold = 30; // AI holds with a higher score, takes fewer risks
rollDelay = Math.random() * 500 + 2500; // Faster rolls
}
setTimeout(() => {
const dice = Math.trunc(Math.random() * 6) + 1;
diceEl.classList.remove('hidden');
diceEl.src = `images/dice-${dice}.png`;
// Play dice roll sound
diceRollSound.play();
if (dice !== 1) {
currentScore += dice;
current1El.textContent = currentScore;
// More sophisticated decision-making for hard mode
let shouldHold;
if (difficulty === 'hard') {
if (scores[1] + currentScore >= winningScore) {
shouldHold = true;
} else if (scores[1] >= scores[0] && currentScore >= holdThreshold) {
shouldHold = true;
} else if (currentScore >= holdThreshold) {
shouldHold = Math.random() > 0.3; // AI tends to hold, but with some unpredictability
} else {
shouldHold = false;
}
} else {
shouldHold = Math.random() > 0.5 || currentScore >= holdThreshold;
}
if (shouldHold) {
// Random delay before AI decides to hold
const aiHoldDelay = Math.random() * 1000 + 500;
setTimeout(() => {
scores[1] += currentScore;
score1El.textContent = scores[1];
if (scores[1] >= winningScore) {
playing = false;
diceEl.classList.add('hidden');
player1El.classList.add('player--winner');
player1El.classList.remove('player--active');
winSound.play(); // Play winning sound
} else {
switchPlayer();
}
}, aiHoldDelay);
} else {
aiTurn(); // AI rolls again
}
} else {
switchPlayer(); // AI rolls a 1, switches back to the human player
}
}, rollDelay); // AI rolls after a delay based on difficulty
};
// Rolling dice functionality
btnRoll.addEventListener('click', function () {
if (playing) {
const dice = Math.trunc(Math.random() * 6) + 1;
diceEl.classList.remove('hidden');
diceEl.src = `images/dice-${dice}.png`;
// Play dice roll sound
diceRollSound.play();
if (dice !== 1) {
currentScore += dice;
document.getElementById(`current--${activePlayer}`).textContent = currentScore;
} else {
switchPlayer();
}
}
});
// Hold score functionality
btnHold.addEventListener('click', function () {
if (playing) {
scores[activePlayer] += currentScore;
document.getElementById(`score--${activePlayer}`).textContent = scores[activePlayer];
if (scores[activePlayer] >= winningScore) {
playing = false;
diceEl.classList.add('hidden');
document.querySelector(`.player--${activePlayer}`).classList.add('player--winner');
document.querySelector(`.player--${activePlayer}`).classList.remove('player--active');
winSound.play(); // Play winning sound
} else {
switchPlayer();
}
}
});
// New Game functionality
btnNew.addEventListener('click', init);
// Set Winning Score functionality
btnSetScore.addEventListener('click', function () {
const inputScore = Number(inputWinningScore.value);
if (inputScore && inputScore > 0) {
winningScore = inputScore;
alert(`Winning score set to ${winningScore}`);
} else {
alert('Please enter a valid score');
}
});
// Handle game mode toggle
modeToggle.addEventListener('change', function () {
singlePlayerMode = modeToggle.checked;
modeLabel.textContent = singlePlayerMode ? 'Single Player' : 'Multiplayer';
init(); // Reinitialize the game when mode changes
});
});