-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
86 lines (73 loc) · 2.37 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getComputerChoice() {
const randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0) {
return "Rock";
} else if (randomNumber === 1) {
return "Paper";
} else if (randomNumber === 2) {
return "Scissors";
}
// This will only happen if something goes wrong with the random number generation
return "Error: Invalid random number";
}
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
if (!['rock', 'paper', 'scissors'].includes(playerSelection)) {
return "Please choose rock, paper, or scissors.";
}
// Defines possible outcomes
const outcomes = {
'rock': {'rock': 'Tie', 'paper': 'You Lose!', 'scissors': 'You Win!'},
'paper': {'rock': 'You Win!', 'paper': 'Tie', 'scissors': 'You Lose!'},
'scissors': {'rock': 'You Lose!', 'paper': 'You Win!', 'scissors': 'Tie'}
};
// Determines the winner of the round
const result = outcomes[playerSelection][computerSelection];
// If tie, the round replays
if (result === 'Tie') {
return "It's a tie! Let's go again.";
} else {
return result + '! ' + result.split(' ')[1] + ' beats ' + playerSelection;
}
}
// The function that is the Game
function game() {
let playerScore = 0;
let computerScore = 0;
for (let round = 1; round <= 5; round++) {
const playerChoice = prompt('Round ' + round + ': Enter your choice (rock, paper, or scissors): ');
const computerChoice = ['rock', 'paper', 'scissors'][Math.floor(Math.random() * 3)];
alert('Computer chose: ' + computerChoice);
const result = playRound(playerChoice, computerChoice);
alert(result);
if (result.includes('Win')) {
playerScore++;
} else if (result.includes('Lose')) {
computerScore++;
}
}
alert('\nGame Over!');
alert('Player Score: ' + playerScore);
alert('Computer Score: ' + computerScore);
if (playerScore > computerScore) {
alert('Congratulations! You win!');
} else if (playerScore < computerScore) {
alert('Sorry, you lost this battle!');
} else {
alert("It's a tie! Stalemate. Go again?");
}
}
// Call the game function to start the Rock Paper Scissors game
game();
</script>
</body>
</html>