-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (82 loc) · 3.8 KB
/
Copy pathscript.js
File metadata and controls
96 lines (82 loc) · 3.8 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
// Đợi cho DOM load hoàn tất trước khi chạy code
// Wait for DOM to be fully loaded before running code
document.addEventListener('DOMContentLoaded', () => {
// Mảng chứa các lựa chọn có thể của trò chơi
// Array of possible game choices
const choices = ['rock', 'paper', 'scissors'];
// Lấy tất cả các nút trong game
// Get all game buttons
const buttons = document.querySelectorAll('.game-btn');
// Lấy phần tử hiển thị kết quả và điểm số
// Get result and score display elements
const resultDisplay = document.getElementById('game-result');
const scoreDisplay = document.getElementById('score');
// Biến lưu điểm số
// Variable to store the score
let score = 0;
// Hàm để máy tính lựa chọn ngẫu nhiên
// Function for computer to make random choice
function getComputerChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}
// Hàm xác định người thắng dựa trên lựa chọn của người chơi và máy
// Function to determine winner based on player and computer choices
function determineWinner(playerChoice, computerChoice) {
// Nếu 2 lựa chọn giống nhau thì hòa
// If choices are the same, it's a draw
if (playerChoice === computerChoice) return 'draw';
// Object định nghĩa điều kiện thắng
// Object defining winning conditions
const winConditions = {
rock: 'scissors', // đá thắng kéo / rock beats scissors
paper: 'rock', // giấy thắng đá / paper beats rock
scissors: 'paper' // kéo thắng giấy / scissors beats paper
};
// Kiểm tra điều kiện thắng và trả về kết quả
// Check win condition and return result
return winConditions[playerChoice] === computerChoice ? 'win' : 'lose';
}
// Hàm cập nhật điểm số
// Function to update the score
function updateScore(result) {
if (result === 'win') score++; // Thắng +1 điểm / Win +1 point
if (result === 'lose') score--; // Thua -1 điểm / Lose -1 point
scoreDisplay.textContent = score; // Hiển thị điểm mới / Display new score
// Check for winner
if (score >= 5) {
resultDisplay.textContent = "Game Over - You are the winner!";
disableButtons();
} else if (score <= -5) {
resultDisplay.textContent = "Game Over - Computer is the winner!";
disableButtons();
}
}
// Add this new function to disable buttons after game end
function disableButtons() {
buttons.forEach(button => button.disabled = true);
}
// Hàm xử lý một lượt chơi
// Function to handle one round of play
function playRound(playerChoice) {
const computerChoice = getComputerChoice();
const result = determineWinner(playerChoice, computerChoice);
// Object chứa các thông báo kết quả
// Object containing result messages
const messages = {
win: `You win! ${playerChoice} beats ${computerChoice}`,
lose: `You lose! ${computerChoice} beats ${playerChoice}`,
draw: `It's a draw! Both chose ${playerChoice}`
};
// Hiển thị kết quả và cập nhật điểm
// Display result and update score
resultDisplay.textContent = messages[result];
updateScore(result);
}
// Thêm event listener cho mỗi nút
// Add event listener to each button
buttons.forEach(button => {
button.addEventListener('click', (e) => {
playRound(e.target.dataset.choice);
});
});
});