-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
147 lines (132 loc) · 5.08 KB
/
Copy pathjavascript.js
File metadata and controls
147 lines (132 loc) · 5.08 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
document.addEventListener("DOMContentLoaded", () => {
// Selecting buttons and result display elements
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissor = document.querySelector("#scissor");
const results = document.querySelector("#results");
const Human_Score = document.querySelector(".Human_Score");
const Computer_Score = document.querySelector(".Computer_Score");
const restartBtn = document.querySelector("#restart");
const playerImage = document.querySelector("#player-image");
const computerImage = document.querySelector("#computer-image");
results.textContent = "There are only 3 rounds in this game. Good luck!";
let HumanScore = 0;
let ComputerScore = 0;
let RoundsPlayed = 0;
const choice = ["Rock", "Paper", "Scissor"];
// Function to get a random choice for the computer
function getComputerChoice() {
const randomIndex = Math.floor(Math.random() * choice.length);
return choice[randomIndex];
}
// Function to update the computer image based on the computer's choice
function updateComputerImage(choice) {
switch (choice) {
case "Rock":
computerImage.src = "img/Rock.png";
break;
case "Paper":
computerImage.src = "img/Paper.png";
break;
case "Scissor":
computerImage.src = "img/Scissor.png";
break;
default:
computerImage.src = "";
}
}
// Function to play a round of Rock-Paper-Scissors
function PlayRound(HumanChoice, ComputerChoice) {
let resultText = "";
if (HumanChoice === "Rock" && ComputerChoice === "Paper") {
ComputerScore++;
resultText = "You lose! Paper beats Rock.";
}
else if (HumanChoice === "Paper" && ComputerChoice === "Scissor") {
ComputerScore++;
resultText = "You lose! Scissor beats Paper.";
}
else if (HumanChoice === "Scissor" && ComputerChoice === "Rock") {
ComputerScore++;
resultText = "You lose! Rock beats Scissor.";
}
else if (HumanChoice === "Paper" && ComputerChoice === "Rock") {
HumanScore++;
resultText = "You win! Paper beats Rock.";
}
else if (HumanChoice === "Scissor" && ComputerChoice === "Paper") {
HumanScore++;
resultText = "You win! Scissor beats Paper.";
}
else if (HumanChoice === "Rock" && ComputerChoice === "Scissor") {
HumanScore++;
resultText = "You win! Rock beats Scissor.";
}
else if (HumanChoice === ComputerChoice) {
resultText = "It's a tie!";
}
else {
resultText = "Invalid input!";
}
return resultText;
}
// Function to update the player image based on the player's choice
function updatePlayerImage(humanChoice) {
switch (humanChoice) {
case "Rock":
playerImage.src = "img/Rock.png";
break;
case "Paper":
playerImage.src = "img/Paper.png";
break;
case "Scissor":
playerImage.src = "img/Scissor.png";
break;
default:
playerImage.src = "";
}
}
// Function to handle the click event for each game button
function handleClick(humanChoice) {
if (RoundsPlayed >= 3) return;
updatePlayerImage(humanChoice)
const HumanChoice = humanChoice;
const ComputerChoice = getComputerChoice();
updateComputerImage(ComputerChoice);
const roundResult = PlayRound(HumanChoice, ComputerChoice);
RoundsPlayed++;
results.innerHTML = roundResult + "<br>";
Human_Score.innerHTML = "You: "+ HumanScore;
Computer_Score.innerHTML = "Computer: " + ComputerScore;
results.innerHTML += "Round: " + RoundsPlayed + "<br>";
if (RoundsPlayed === 3) {
const finalMessage = score_count();
results.innerHTML += `<br><strong>Final Result: ${finalMessage}</strong><br>`;
}
}
// Function to determine the final score after 3 rounds
function score_count() {
if (HumanScore > ComputerScore) {
return "YOU ARE THE WINNER!";
}
else if (HumanScore < ComputerScore) {
return "COMPUTER IS THE WINNER!";
}
else {
return "IT'S A TIE!";
}
}
// Adding click event listeners to game buttons
rock.addEventListener("click", () => handleClick("Rock"));
paper.addEventListener("click", () => handleClick("Paper"));
scissor.addEventListener("click", () => handleClick("Scissor"));
// Restart button functionality
restartBtn.addEventListener("click", () => {
HumanScore = 0;
ComputerScore = 0;
RoundsPlayed = 0;
results.textContent = "Game has been reset. There are only 3 rounds. Good luck!";
Human_Score.textContent = "You: 0";
Computer_Score.textContent = "Computer: 0";
});
});