Skip to content

Commit 8f29178

Browse files
authored
Merge pull request #928 from AckermanLevi1/rockpaperscissor-AckermanLevi1
Rock-Paper-Scissor
2 parents ab3e538 + 6f867d8 commit 8f29178

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
text-align: center;
4+
}
5+
6+
h1 {
7+
margin-top: 20px;
8+
}
9+
10+
.choice {
11+
width: 100px;
12+
height: 100px;
13+
cursor: pointer;
14+
margin: 10px;
15+
}
16+
17+
#game {
18+
display: flex;
19+
justify-content: center;
20+
}
21+
22+
#player, #computer {
23+
width: 200px;
24+
}
25+
26+
#result {
27+
margin-top: 20px;
28+
}
29+
#computer {
30+
display: none; /* Initially hidden */
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Rock, Paper, Scissors Game</title>
7+
<link rel="stylesheet" href="./index.css">
8+
</head>
9+
<body>
10+
<h1>Rock, Paper, Scissors Game</h1>
11+
<div id="game">
12+
<div id="player">
13+
<h2>You</h2>
14+
<img src="rock.png" alt="Rock" id="rock" class="choice">
15+
<img src="paper.png" alt="Paper" id="paper" class="choice">
16+
<img src="scissors.png" alt="Scissors" id="scissors" class="choice">
17+
</div>
18+
<div id="computer">
19+
<h2>Computer</h2>
20+
<img src="question.png" alt="Question" id="computer-choice" class="choice">
21+
</div>
22+
<div id="result">
23+
<p>Choose your weapon!</p>
24+
</div>
25+
</div>
26+
<script src="./index.js"></script>
27+
</body>
28+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const choices = ["rock", "paper", "scissors"];
2+
3+
function getResult(player, computer) {
4+
if (player === computer) return "It's a draw!";
5+
if (
6+
(player === "rock" && computer === "scissors") ||
7+
(player === "scissors" && computer === "paper") ||
8+
(player === "paper" && computer === "rock")
9+
) {
10+
return "You win!";
11+
}
12+
return "Computer wins!";
13+
}
14+
15+
function displayResult(result) {
16+
const resultElement = document.getElementById("result");
17+
resultElement.textContent = result; // Set the result text
18+
}
19+
20+
document.querySelectorAll(".choice").forEach((element) => {
21+
element.addEventListener("click", () => {
22+
const playerChoice = element.id;
23+
const computerChoice = choices[Math.floor(Math.random() * 3)];
24+
const computerChoiceImage = document.getElementById("computer-choice");
25+
26+
// Show the computer's choice element
27+
document.getElementById("computer").style.display = "block";
28+
29+
// Update the computer's choice image
30+
computerChoiceImage.src = `${computerChoice}.png`;
31+
32+
const result = getResult(playerChoice, computerChoice);
33+
34+
displayResult(result);
35+
});
36+
});
2.31 KB
Loading
3.59 KB
Loading
6.06 KB
Loading

0 commit comments

Comments
 (0)