-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
88 lines (76 loc) · 2.58 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock Paper Scissors</title>
<style>
button {
margin: 10px;
padding: 10px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<div>
<button id="rock">Rock</button>
<button id="paper">Paper</button>
<button id="scissors">Scissors</button>
</div>
<div id="result"></div>
<div id="score">Score: Player 0 - Computer 0</div>
<script>
let playerScore = 0;
let computerScore = 0;
function computerPlay() {
const choices = ["rock", "paper", "scissors"];
const randomIndex = Math.floor(Math.random() * 3);
return choices[randomIndex];
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "It's a tie!";
} else if (
(playerSelection === "rock" && computerSelection === "scissors") ||
(playerSelection === "paper" && computerSelection === "rock") ||
(playerSelection === "scissors" && computerSelection === "paper")
) {
playerScore++;
return "You win this round!";
} else {
computerScore++;
return "You lose this round!";
}
}
function updateUI(result) {
const resultDiv = document.getElementById("result");
resultDiv.textContent = result;
const scoreDiv = document.getElementById("score");
scoreDiv.textContent = `Score: Player ${playerScore} - Computer ${computerScore}`;
if (playerScore === 5 || computerScore === 5) {
if (playerScore === 5) {
resultDiv.textContent = "Congratulations! You won the game!";
} else {
resultDiv.textContent = "Sorry! You lost the game. Try again!";
}
// Disable buttons after game ends
document.getElementById("rock").disabled = true;
document.getElementById("paper").disabled = true;
document.getElementById("scissors").disabled = true;
}
}
function handleClick(event) {
const playerSelection = event.target.id;
const computerSelection = computerPlay();
const roundResult = playRound(playerSelection, computerSelection);
updateUI(roundResult);
}
// Add event listeners to buttons
document.getElementById("rock").addEventListener("click", handleClick);
document.getElementById("paper").addEventListener("click", handleClick);
document.getElementById("scissors").addEventListener("click", handleClick);
</script>
</body>
</html>