-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
470 lines (400 loc) Β· 17.2 KB
/
script.js
File metadata and controls
470 lines (400 loc) Β· 17.2 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
document.addEventListener('DOMContentLoaded', () => {
// --- 1. PENGUMPULAN ELEMEN UI ---
const ui = {
mainMenu: document.getElementById('main-menu'),
modeSelection: document.getElementById('mode-selection'),
operationSelection: document.getElementById('operation-selection'),
difficultySelection: document.getElementById('difficulty-selection'),
gameContainer: document.getElementById('game-container'),
resultContainer: document.getElementById('result-container'),
startBtn: document.getElementById('start-btn'),
playAgainBtn: document.getElementById('play-again-btn'),
scoreDisplay: document.getElementById('score'),
answerPreview: document.getElementById('answer-preview'),
divider: document.querySelector('.divider'),
player1: {
box: document.getElementById('player1-box'),
question: document.getElementById('q1'),
options: document.getElementById('opt1'),
progressBar: document.getElementById('progress-bar-P1')
},
player2: {
box: document.getElementById('player2-box'),
question: document.getElementById('q2'),
options: document.getElementById('opt2'),
progressBar: document.getElementById('progress-bar-P2')
},
backgroundMusic: document.getElementById('background-music'),
clickSound: document.getElementById('click-sound'),
// ELEMEN SUARA KEMENANGAN DIAMBIL
winSound: document.getElementById('win-sound')
};
// --- KONTROL MUSIK LATAR ---
let isMusicStarted = false;
function tryPlayMusic() {
if (ui.backgroundMusic && !isMusicStarted) {
ui.backgroundMusic.play().then(() => {
isMusicStarted = true;
document.body.removeEventListener('click', tryPlayMusic);
}).catch(error => {
console.log("Browser memblokir pemutaran otomatis, menunggu interaksi pengguna selanjutnya.");
});
}
}
document.body.addEventListener('click', tryPlayMusic);
// --- KONTROL SUARA KLIK ---
function playClickSound() {
if (ui.clickSound) {
ui.clickSound.currentTime = 0;
ui.clickSound.play().catch(e => console.log("Gagal memutar suara klik:", e));
}
}
document.addEventListener('click', playClickSound);
// --- 2. STATE MANAGEMENT GAME ---
let gameState = {};
function resetGameState() {
gameState = {
mode: 'one-player',
operation: 'addition',
difficulty: 'easy',
questionCount: 0,
maxQuestions: 10,
scores: { P1: 0, P2: 0 },
history: [],
timers: { P1: null, P2: null },
currentQuestions: { P1: null, P2: null },
};
}
// --- 3. FUNGSI LOGIKA GAME ---
function startGame() {
showView('gameContainer');
nextQuestion();
}
function nextQuestion() {
gameState.questionCount++;
clearTimers();
setPlayerBlur('P1', false);
setPlayerBlur('P2', false);
gameState.currentQuestions.P1 = createQuestionData();
updatePlayerUI('P1');
if (gameState.mode === 'two-player') {
gameState.currentQuestions.P2 = createQuestionData();
updatePlayerUI('P2');
ui.player2.box.style.display = 'flex';
ui.divider.style.display = 'block';
} else {
ui.player2.box.style.display = 'none';
ui.divider.style.display = 'none';
}
setupTurn('P1');
}
function setupTurn(player) {
if (gameState.mode === 'two-player') {
setPlayerBlur('P1', player !== 'P1');
setPlayerBlur('P2', player !== 'P2');
}
startTimer(player);
}
function processAnswer(player, selectedValue) {
if (gameState.currentQuestions[player].isAnswered) return;
stopTimer(player);
const question = gameState.currentQuestions[player];
question.isAnswered = true;
question.selectedAnswer = selectedValue;
question.isCorrect = (selectedValue == question.correctAnswer);
if (question.isCorrect) {
gameState.scores[player]++;
}
updateAnswerUI(player);
handleTurnEnd(player);
}
function handleTimeout(player) {
if (gameState.currentQuestions[player].isAnswered) return;
const question = gameState.currentQuestions[player];
question.isAnswered = true;
question.selectedAnswer = "Waktu Habis";
question.isCorrect = false;
updateAnswerUI(player, true);
handleTurnEnd(player);
}
function handleTurnEnd(player) {
if (gameState.mode === 'one-player') {
setTimeout(nextQuestionOrEnd, 1200);
} else { // Two-player mode
if (player === 'P1') {
setupTurn('P2');
} else {
setTimeout(nextQuestionOrEnd, 1500);
}
}
}
function nextQuestionOrEnd() {
recordHistory();
if (gameState.questionCount >= gameState.maxQuestions) {
endGame();
} else {
nextQuestion();
}
}
function recordHistory() {
const q1 = gameState.currentQuestions.P1;
if (!q1) return;
const p1Result = {
question: q1.questionText,
selected: q1.selectedAnswer,
correct: q1.correctAnswer,
isCorrect: q1.isCorrect
};
let p2Result = null;
if (gameState.mode === 'two-player') {
const q2 = gameState.currentQuestions.P2;
p2Result = {
question: q2.questionText,
selected: q2.selectedAnswer,
correct: q2.correctAnswer,
isCorrect: q2.isCorrect
};
}
gameState.history.push({ p1: p1Result, p2: p2Result });
}
function endGame() {
showView('resultContainer');
const scoreP1 = gameState.scores.P1;
const scoreP2 = gameState.scores.P2;
let finalScoreText, gameOutcomeMessage;
let didWin = false; // Flag untuk menandai kemenangan
if (gameState.mode === 'one-player') {
finalScoreText = `Skor Anda: ${scoreP1} / ${gameState.maxQuestions}`;
if (scoreP1 === gameState.maxQuestions) {
gameOutcomeMessage = "<br>Sempurna! Selamat!";
didWin = true; // Menang jika skor sempurna
} else {
gameOutcomeMessage = "<br>Game Selesai!";
}
} else { // two-player mode
finalScoreText = `Skor P1: ${scoreP1} | Skor P2: ${scoreP2}`;
if (scoreP1 > scoreP2) {
gameOutcomeMessage = "<br>Pemain 1 Menang!";
didWin = true;
} else if (scoreP2 > scoreP1) {
gameOutcomeMessage = "<br>Pemain 2 Menang!";
didWin = true;
} else {
gameOutcomeMessage = "<br>Seri!";
}
}
ui.scoreDisplay.innerHTML = `${finalScoreText}${gameOutcomeMessage}`;
// Putar suara kemenangan jika menang
if (didWin && ui.winSound) {
ui.winSound.play().catch(e => console.log("Gagal memutar suara kemenangan:", e));
}
renderHistoryTable();
}
// --- 4. FUNGSI PEMBUATAN SOAL ---
function createQuestionData() {
let num1, num2, questionText, correctAnswer, op;
let currentOperation = gameState.operation;
if (currentOperation === 'mixed') {
const ops = ['addition', 'subtraction', 'multiplication', 'division'];
currentOperation = ops[Math.floor(Math.random() * ops.length)];
}
const ranges = { easy: [1, 10], medium: [10, 50], hard: [50, 200] };
const currentRange = ranges[gameState.difficulty];
switch (currentOperation) {
case 'subtraction':
op = 'β';
num1 = Math.floor(Math.random() * (currentRange[1] - currentRange[0] + 1)) + currentRange[0];
num2 = Math.floor(Math.random() * (currentRange[1] - currentRange[0] + 1)) + currentRange[0];
if (num1 < num2) [num1, num2] = [num2, num1];
if (num1 === num2) num1++;
correctAnswer = num1 - num2;
break;
case 'multiplication':
op = 'Γ';
const mulRanges = { easy: [2, 9], medium: [5, 12], hard: [10, 20] };
const mulRange = mulRanges[gameState.difficulty];
num1 = Math.floor(Math.random() * (mulRange[1] - mulRange[0] + 1)) + mulRange[0];
num2 = Math.floor(Math.random() * (mulRange[1] - mulRange[0] + 1)) + mulRange[0];
correctAnswer = num1 * num2;
break;
case 'division':
op = 'Γ·';
const divRanges = { easy: [2, 9], medium: [5, 12], hard: [8, 15] };
const divRange = divRanges[gameState.difficulty];
num2 = Math.floor(Math.random() * (divRange[1] - divRange[0] + 1)) + divRange[0];
const result = Math.floor(Math.random() * 8) + 2;
num1 = num2 * result;
correctAnswer = result;
break;
default: // Addition
op = '+';
num1 = Math.floor(Math.random() * (currentRange[1] - currentRange[0] + 1)) + currentRange[0];
num2 = Math.floor(Math.random() * (currentRange[1] - currentRange[0] + 1)) + currentRange[0];
correctAnswer = num1 + num2;
break;
}
questionText = `${num1} ${op} ${num2}`;
const optionsSet = new Set([correctAnswer]);
while (optionsSet.size < 4) {
const variation = Math.floor(correctAnswer * 0.5) + 5;
const fakeAnswer = correctAnswer + Math.floor(Math.random() * variation * 2) - variation;
if (fakeAnswer !== correctAnswer && fakeAnswer >= 0) optionsSet.add(Math.round(fakeAnswer));
}
return {
questionText,
correctAnswer,
options: Array.from(optionsSet).sort(() => Math.random() - 0.5),
isAnswered: false,
isCorrect: false,
selectedAnswer: null,
timerStart: 0
};
}
// --- 5. FUNGSI PEMBARUAN TAMPILAN (UI) ---
function showView(viewName) {
ui.mainMenu.style.display = 'none';
ui.gameContainer.style.display = 'none';
ui.resultContainer.style.display = 'none';
if (viewName === 'gameContainer' || viewName === 'resultContainer') {
ui[viewName].style.display = 'flex';
} else {
ui.mainMenu.style.display = 'flex';
ui.startBtn.style.display = 'none';
ui.modeSelection.style.display = 'none';
ui.operationSelection.style.display = 'none';
ui.difficultySelection.style.display = 'none';
if (viewName === 'mainMenu') {
ui.startBtn.style.display = 'inline-block';
} else if (ui[viewName]) {
ui[viewName].style.display = 'flex';
}
}
}
function renderHistoryTable() {
const previewContainer = document.getElementById('answer-preview-wrapper');
if (!previewContainer) return;
previewContainer.innerHTML = '';
const table = document.createElement('table');
let headerHtml = '';
if (gameState.mode === 'one-player') {
headerHtml = `<thead><tr><th>No.</th><th>Soal</th><th>Jawabanmu (Benar)</th><th>Hasil</th></tr></thead>`;
} else {
headerHtml = `<thead><tr><th>No.</th><th>Soal (P1)</th><th>Jwb (P1)</th><th>Soal (P2)</th><th>Jwb (P2)</th></tr></thead>`;
}
const bodyHtml = gameState.history.map((entry, idx) => {
const p1 = entry.p1;
const p2 = entry.p2;
const p1AnswerCell = `<td class="${p1.isCorrect ? 'correct-text' : 'wrong-text'}">${p1.selected} (${p1.correct})</td>`;
if (gameState.mode === 'one-player') {
return `<tr>
<td>${idx + 1}</td>
<td>${p1.question}</td>
${p1AnswerCell}
<td>${p1.isCorrect ? 'βοΈ' : 'β'}</td>
</tr>`;
} else {
const p2AnswerCell = `<td class="${p2.isCorrect ? 'correct-text' : 'wrong-text'}">${p2.selected} (${p2.correct})</td>`;
return `<tr>
<td>${idx + 1}</td>
<td>${p1.question}</td>
${p1AnswerCell}
<td>${p2.question}</td>
${p2AnswerCell}
</tr>`;
}
}).join('');
table.innerHTML = `${headerHtml}<tbody>${bodyHtml}</tbody>`;
previewContainer.appendChild(table);
}
function setPlayerBlur(player, shouldBlur) {
const key = 'player' + player.slice(-1);
const box = ui[key].box;
shouldBlur ? box.classList.add('blurred-content') : box.classList.remove('blurred-content');
}
function updatePlayerUI(player) {
const key = 'player' + player.slice(-1);
const playerUI = ui[key];
const questionData = gameState.currentQuestions[player];
playerUI.question.textContent = questionData.questionText;
playerUI.options.innerHTML = '';
questionData.options.forEach(val => {
const btn = document.createElement("button");
btn.className = "option-btn";
btn.textContent = val;
btn.addEventListener('click', () => processAnswer(player, val));
playerUI.options.appendChild(btn);
});
}
function updateAnswerUI(player, isTimeout = false) {
const key = 'player' + player.slice(-1);
const playerUI = ui[key];
const questionData = gameState.currentQuestions[player];
const buttons = playerUI.options.querySelectorAll('button');
buttons.forEach(btn => {
btn.disabled = true;
const btnValue = parseInt(btn.textContent);
if (btnValue === questionData.correctAnswer) {
btn.classList.add("correct");
} else if (!isTimeout && btnValue == questionData.selectedAnswer) {
btn.classList.add("wrong");
}
});
}
// --- 6. FUNGSI TIMER ---
const TIMER_DURATION = 10000;
function startTimer(player) {
const key = 'player' + player.slice(-1);
const question = gameState.currentQuestions[player];
question.timerStart = Date.now();
function frame() {
const elapsed = Date.now() - question.timerStart;
const remainingPercentage = Math.max(0, 100 - (elapsed / TIMER_DURATION) * 100);
ui[key].progressBar.style.width = `${remainingPercentage}%`;
if (remainingPercentage > 0) {
gameState.timers[player] = requestAnimationFrame(frame);
} else {
handleTimeout(player);
}
}
gameState.timers[player] = requestAnimationFrame(frame);
}
function stopTimer(player) {
if(gameState.timers[player]) cancelAnimationFrame(gameState.timers[player]);
}
function clearTimers() {
stopTimer('P1');
stopTimer('P2');
if(ui.player1.progressBar) ui.player1.progressBar.style.width = '100%';
if(ui.player2.progressBar) ui.player2.progressBar.style.width = '100%';
}
// --- 7. INISIALISASI EVENT LISTENER ---
function initEventListeners() {
ui.startBtn.addEventListener('click', () => {
showView('modeSelection');
});
ui.modeSelection.addEventListener('click', e => {
if (e.target.tagName !== 'BUTTON') return;
resetGameState();
gameState.mode = e.target.id;
showView('operationSelection');
});
ui.operationSelection.addEventListener('click', e => {
const button = e.target.closest('.op-btn');
if (!button) return;
gameState.operation = button.dataset.op;
showView('difficultySelection');
});
ui.difficultySelection.addEventListener('click', e => {
if (e.target.tagName !== 'BUTTON') return;
gameState.difficulty = e.target.id.replace('difficulty-', '');
startGame();
});
ui.playAgainBtn.addEventListener('click', () => {
showView('mainMenu');
});
}
// --- MULAI APLIKASI ---
initEventListeners();
showView('mainMenu');
resetGameState();
});