-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
67 lines (62 loc) · 2.4 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
<html>
<link rel="stylesheet" href="style.css"
<head>
<title>Simple RPS</title>
</head>
<body>
<div class="top">
<h1>Rock-Paper-Scissors</h1>
<p>Choose your move</p>
</div>
<div class = 'interactions'>
<button id="rock" onclick="playRound('rock')">Rock</button>
<button id="paper" onclick="playRound('paper')">Paper</button>
<button id="scissors" onclick="playRound('scissors')">Scissors</button>
</div>
<br><br>
<div class = 'bottom'>
<div id="results"></div>
<br><br>
<!-- <button id="restart" onclick="window.location.reload()">Restart</button> -->
</div>
<div class="restart">
<button id="restart" onclick="window.location.reload()">Restart</button>
</div>
<script>
const options = ['rock', 'paper', 'scissors'];
let playerScore = 0;
let computerScore = 0;
let round = 0;
const resultsDiv = document.getElementById('results');
function playRound(playerChoice) {
round++;
if (round > 5) {
if (playerScore > computerScore) {
resultsDiv.innerHTML += `<br> You win the game with a score of ${playerScore} to ${computerScore}!`;
} else if (computerScore > playerScore) {
resultsDiv.innerHTML += `<br> Computer wins the game with a score of ${computerScore} to ${playerScore}!`;
} else {
resultsDiv.innerHTML += `<br> The game is a tie with a score of ${playerScore} to ${computerScore}!`;
}
return;
}
const computerChoice = options[Math.floor(Math.random() * 3)];
let outcome;
if (playerChoice === computerChoice) {
outcome = "It's a tie!";
} else if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
outcome = 'You win!';
playerScore++;
} else {
outcome = 'Computer wins!';
computerScore++;
}
resultsDiv.innerHTML += `Round ${round}: ${outcome} Player: ${playerChoice} ---- Computer: ${computerChoice} <br>`;
}
</script>
</body>
</html>