-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockpaperscissor.js
More file actions
159 lines (135 loc) · 5.93 KB
/
Copy pathrockpaperscissor.js
File metadata and controls
159 lines (135 loc) · 5.93 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
const choices = ["Rock", "Paper", "Scissor"];
let userScore = 0;
let computerScore = 0;
let roundsPlayed = 0;
document.getElementById("startGame").addEventListener("click", startGame);
document.getElementById("exitGame").addEventListener("click", () => {
window.close(); // This will only work in certain browsers
});
document.getElementById("restartGame").addEventListener("click", restartGame);
function startGame() {
document.getElementById("startScreen").classList.add("hidden");
document.getElementById("gameArea").classList.remove("hidden");
}
document.querySelectorAll(".choice").forEach(button => {
button.addEventListener("click", () => {
const userChoice = button.getAttribute("data-choice");
const computerChoice = getComputerChoice();
const result = determineWinner(userChoice, computerChoice);
displayResult(userChoice, computerChoice, result);
});
});
function getComputerChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(user, computer) {
if (user === computer) {
return "Match Draw";
}
if (
(user === "Rock" && computer === "Scissor") ||
(user === "Paper" && computer === "Rock") ||
(user === "Scissor" && computer === "Paper")
) {
userScore++;
return "You Win!";
}
computerScore++;
return "Computer Wins!";
}
function displayResult(userChoice, computerChoice, result) {
roundsPlayed++;
// Hide choices and show result images
document.querySelector(".choices").classList.add("hidden");
// Show the result with images
let resultHTML = `
<p>Your Choice: <strong>${userChoice}</strong></p>
<p>Computer Choice: <strong>${computerChoice}</strong></p>
<p style="font-weight: bold;">${result}</p>
`;
if ((userChoice === "Rock" && computerChoice === "Paper") ||
(userChoice === "Paper" && computerChoice === "Rock")) {
resultHTML = `
<p>Your Choice: <strong>${userChoice}</strong></p>
<img src="images/rock-paper.jpg" alt="Rock vs Paper">
<p>Computer Choice: <strong>${computerChoice}</strong></p>
<p style="font-weight: bold;">${result}</p>
`;
} else if ((userChoice === "Rock" && computerChoice === "Scissor") ||
(userChoice === "Scissor" && computerChoice === "Rock")) {
resultHTML = `
<p>Your Choice: <strong>${userChoice}</strong></p>
<img src="images/rock-scissors.jpg" alt="Rock vs Scissor">
<p>Computer Choice: <strong>${computerChoice}</strong></p>
<p style="font-weight: bold;">${result}</p>
`;
} else if ((userChoice === "Paper" && computerChoice === "Scissor") ||
(userChoice === "Scissor" && computerChoice === "Paper")) {
resultHTML = `
<p>Your Choice: <strong>${userChoice}</strong></p>
<img src="images/paper-scissors.jpg" alt="Scissor vs Paper">
<p>Computer Choice: <strong>${computerChoice}</strong></p>
<p style="font-weight: bold;">${result}</p>
`;
}
document.getElementById("result").innerHTML = resultHTML;
document.getElementById("result").classList.remove("hidden");
updateScoreboard();
if (roundsPlayed >= 5) {
endGame();
} else {
// Show "Play Next Round" button
showPlayNextRoundButton();
}
}
function showPlayNextRoundButton() {
const playNextButtonHTML = `
<button id="playNextRound">Play Next Round</button>
<button id="exitGame">Exit Game</button>
`;
document.getElementById("result").innerHTML += playNextButtonHTML;
document.getElementById("playNextRound").addEventListener("click", () => {
document.getElementById("result").classList.add("hidden");
document.querySelector(".choices").classList.remove("hidden");
});
document.getElementById("exitGame").addEventListener("click", () => {
window.close(); // This will only work in certain browsers
});
}
function updateScoreboard() {
document.getElementById("userScore").textContent = userScore;
document.getElementById("computerScore").textContent = computerScore;
document.getElementById("roundsPlayed").textContent = roundsPlayed;
}
function endGame() {
// Hide the game area and show the final result section
document.getElementById("gameArea").classList.add("hidden");
document.getElementById("finalResult").classList.remove("hidden");
// Update the final scores
document.getElementById("finalUserScore").textContent = document.getElementById("userScore").textContent;
document.getElementById("finalComputerScore").textContent = document.getElementById("computerScore").textContent;
// Update the total rounds played
document.getElementById("finalRoundsPlayed").textContent = document.getElementById("roundsPlayed").textContent;
// Set the final message based on the scores
let finalMessage;
const userScore = parseInt(document.getElementById("userScore").textContent);
const computerScore = parseInt(document.getElementById("computerScore").textContent);
if (userScore > computerScore) {
finalMessage = "Congratulations! You are the champion!";
} else if (userScore < computerScore) {
finalMessage = "Sorry! The computer wins this time.";
} else {
finalMessage = "It's a tie!";
}
// Display the final message
document.getElementById("finalMessage").textContent = finalMessage;
}
function restartGame() {
userScore = 0;
computerScore = 0;
roundsPlayed = 0;
document.getElementById("finalResult").classList.add("hidden");
document.getElementById("startScreen").classList.remove("hidden");
document.getElementById("result").innerHTML = "";
updateScoreboard();
}