-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
114 lines (105 loc) · 3.81 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css" />
<title>Rock Paper Scissors!</title>
</head>
<body>
<div class="results">
<h3 id="resultMessage">Choose your weapon!</h3>
</div>
<div class="buttons">
<button class="btn" id="rockBtn">ROCK</button>
<button class="btn" id="paperBtn">PAPER</button>
<button class="btn" id="scissorsBtn">SCISSORS</button>
</div>
<div class="score">
<h3 id="score">Player: 0 - Computer: 0</h3>
</div>
<script>
const choices = ["Rock", "Paper", "Scissors"]
let playerScore = 0;
let computerScore = 0;
let game_ended = 0;
function computerPlay(){
return choices[~~(Math.random() * choices.length)];
}
function playRound(playerSelection, computerSelection) {
let ps = playerSelection.toUpperCase();
let cs = computerSelection.toUpperCase();
console.log(playerScore);
console.log(ps + " vs " + cs);
//Tie
if (ps == cs){
return "It's a tie! Both picked " + computerSelection;
}
//Rock
if (ps == "ROCK") {
if (cs == "SCISSORS") {
playerScore++;
console.log(playerScore);
return 'You win! Rock beats Scissors';
} else {
computerScore++;
return 'You lose! Paper beats Rock';
}
}
//Paper
if (ps == "PAPER") {
if (cs == "ROCK") {
playerScore++;
return 'You win! Paper beats Rock';
} else {
computerScore++;
return 'You lose! Scissors beats Paper';
}
}
//Scissors
if (ps == "SCISSORS") {
if (cs == "PAPER") {
playerScore++;
return 'You win! Scissors beats Paper';
} else {
computerScore++;
return 'You lose! Rock beats Scissors';
}
}
}
function game() {
while (true) {
let player = prompt("Choose a weapon: ");
console.log(playRound(player, computerPlay()));
}
}
function updateResult(string) {
if (!game_ended) {
resultTxt.textContent = string;
scoreTxt.textContent = 'Player: ' + playerScore + ' - Computer: ' + computerScore;
}
if (playerScore == 5 || computerScore == 5) {
game_ended = 1;
endGame();
}
}
function endGame() {
if (playerScore > computerScore) {
resultTxt.textContent = 'Game over! You win!';
} else {
resultTxt.textContent = 'Game over! You lose!';
}
}
//UI
const rockBtn = document.getElementById('rockBtn')
const paperBtn = document.getElementById('paperBtn')
const scissorsBtn = document.getElementById('scissorsBtn')
const resultTxt = document.getElementById('resultMessage')
const scoreTxt = document.getElementById('score')
rockBtn.addEventListener('click', () => updateResult(playRound("ROCK", computerPlay())))
paperBtn.addEventListener('click', () => updateResult(playRound("PAPER", computerPlay())))
scissorsBtn.addEventListener('click', () => updateResult(playRound("SCISSORS", computerPlay())))
</script>
</body>
</html>