-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
211 lines (209 loc) · 10.3 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors</title>
<link rel="stylesheet" href="style.css" />
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@1,700&display=swap" rel="stylesheet">
</head>
<body>
<div class="top">
<!-- title + instructions -->
<h1 class="title">Rock Paper Scissors</h1>
<p class="instructions">
Welcome to Rock Paper Scissors! This is a five round game
against the computer. Enter in one of the values 'rock',
'paper', or 'scissors' via the appearing textbox. Good luck!
</p>
</div>
<!-- selection buttons -->
<div class="selection">
<button class="rock">Rock</button>
<button class="paper">Paper</button>
<button class="scissors">Scissors</button>
</div>
<!-- response -->
<div class="result" id="result">
<p id="response"></p>
</div>
<script>
// set point default values
let playerPoints = 0;
let computerPoints = 0;
// set other default values
let computerSelection = "";
let playerSelection = "";
let values = ["rock", "paper", "scissors"];
// return random value for computer playing the game
function computerPlay() {
computerSelection =
values[Math.floor(Math.random() * values.length)];
return computerSelection;
}
window.onload = function () {
// clicked on rock
const rock = document.querySelector(".rock");
rock.addEventListener("click", () => {
computerPlay();
playerSelection = "rock";
console.log(
singleRound(playerSelection, computerSelection)
);
});
// clicked on paper
const paper = document.querySelector(".paper");
paper.addEventListener("click", () => {
computerPlay();
playerSelection = "paper";
console.log(
singleRound(playerSelection, computerSelection)
);
});
// clicked on scissors
const scissors = document.querySelector(".scissors");
scissors.addEventListener("click", () => {
computerPlay();
playerSelection = "scissors";
singleRound(playerSelection, computerSelection);
});
// play a single round
function singleRound(playerSelection, computerSelection) {
// convert input to lowercase
playerSelection = playerSelection.toLowerCase();
// if the player chose scissors
if (playerSelection == "scissors") {
if (computerSelection == "rock") {
computerPoints = computerPoints + 1;
if (computerPoints >= 5) {
document.getElementById("response").innerHTML =
"Oh no! You lost rock paper scissors. Better luck next time!";
playAgain();
} else {
document.getElementById(
"response"
).innerHTML = `You lose! Rock beats scissors. Score: You - ${playerPoints} Computer - ${computerPoints}`;
}
} else if (computerSelection == "scissors") {
document.getElementById(
"response"
).innerHTML = `Draw! Scissors does not beat scissors. Score: You - ${playerPoints} Computer - ${computerPoints}`;
} else if (computerSelection == "paper") {
playerPoints = playerPoints + 1;
if (playerPoints >= 5) {
document.getElementById("response").innerHTML =
"Congratulations! You won rock paper scissors.";
playAgain();
} else {
document.getElementById(
"response"
).innerHTML = `You win! Scissors beats paper. Score: You - ${playerPoints} Computer - ${computerPoints}`;
}
}
}
// if the player chose rock
if (playerSelection == "rock") {
if (computerSelection == "paper") {
computerPoints = computerPoints + 1;
if (computerPoints >= 5) {
document.getElementById("response").innerHTML =
"Oh no! You lost rock paper scissors. Better luck next time!";
playAgain();
} else {
document.getElementById(
"response"
).innerHTML = `You lose! Paper beats rock. Score: You - ${playerPoints} Computer - ${computerPoints}`;
}
} else if (computerSelection == "rock") {
document.getElementById(
"response"
).innerHTML = `Draw! Rock does not beat rock. Score: You - ${playerPoints} Computer - ${computerPoints}`;
} else if (computerSelection == "scissors") {
playerPoints = playerPoints + 1;
if (playerPoints >= 5) {
document.getElementById("response").innerHTML =
"Congratulations! You won rock paper scissors.";
playAgain();
} else {
document.getElementById(
"response"
).innerHTML = `You win! Rock beats scissors. Score: You - ${playerPoints} Computer - ${computerPoints}`;
}
}
}
// if the player chose paper
if (playerSelection == "paper") {
if (computerSelection == "scissors") {
computerPoints = computerPoints + 1;
if (computerPoints >= 5) {
document.getElementById("response").innerHTML =
"Oh no! You lost rock paper scissors. Better luck next time!";
playAgain();
} else {
document.getElementById(
"response"
).innerHTML = `You lose! Paper beats rock. Score: You - ${playerPoints} Computer - ${computerPoints}`;
}
} else if (computerSelection == "paper") {
document.getElementById(
"response"
).innerHTML = `Draw! Paper does not beat paper. Score: You - ${playerPoints} Computer - ${computerPoints}`;
} else if (computerSelection == "rock") {
playerPoints = playerPoints + 1;
if (playerPoints >= 5) {
document.getElementById("response").innerHTML =
"Congratulations! You won rock paper scissors.";
playAgain();
} else {
document.getElementById(
"response"
).innerHTML = `You win! Paper beats rock. Score: You - ${playerPoints} Computer - ${computerPoints}`;
}
}
}
}
};
// create button for playing again
function playAgain() {
document.querySelector(".rock").disabled = true;
document.querySelector(".scissors").disabled = true;
document.querySelector(".paper").disabled = true;
const newGame = document.createElement("button");
newGame.textContent = "Play again";
newGame.className = "playAgain";
newGame.addEventListener("click", refreshPage);
document.getElementById("result").appendChild(newGame);
}
// refresh the page
function refreshPage() {
window.location.reload(true);
}
// psuedocode
/* function computerPlay
return random value, either rock, paper or scissors
get input from user, put into function to test input
call function singleRound() with both inputs
if player chooses scissors
if computer chooses rock
"You lose"
if computer chooses scissors
"Draw"
if computer chooses paper
"You win"
if player chooses rock
if computer chooses rock
"Draw"
if computer chooses scissors
"You win"
if computer chooses paper
"You lose"
if player chooses paper
if computer chooses rock
"You win"
if computer chooses scissors
"You lose"
if computer chooses paper
"Draw"
*/
</script>
</body>
</html>