-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-rock-paper-scissors.html
223 lines (155 loc) · 4.3 KB
/
09-rock-paper-scissors.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
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<title> Object Rock Paper Scissors </title>
</head>
<body>
<p>
Rock Paper Scissors
</p>
<button onclick="
pickComputerMove();
playerMove = 'rock';
updateMove(playerMove, computerMove);
PlayGame(playerMove);
"> Rock</button>
<button onclick="
pickComputerMove();
playerMove = 'paper';
updateMove(playerMove, computerMove);
PlayGame(playerMove);
"> Paper</button>
<button onclick="
pickComputerMove();
playerMove = 'scissors';
updateMove(playerMove, computerMove);
PlayGame(playerMove);
"> Scissors</button>
<p class="js-result"></p>
<p class="js-moves"></p>
<p class="js-score"> </p>
<button onclick="
score.wins = 0;
score.losses = 0;
score.ties = 0;
// removing score object from local storage
localStorage.removeItem('score');
updateScoreElement();
">
Reset Score
</button>
<script>
// getting score from localstorage then converting to Javascript.
let score = JSON.parse(localStorage.getItem('score')) || {
wins: 0,
losses: 0,
ties: 0
};
updateScoreElement();
// above code does shortcut way of what is done below check if score is null
/*
of (score === null) {
score = {
wins: 0,
losses: 0,
ties: 0
}
}
*/
let playerMove = '';
let result = '';
let computerMove = '';
function updateResult(gameresult) {
document.querySelector('.js-result')
.innerHTML = `${gameresult}.`;
}
function updateMove(yourmove, compmove) {
document.querySelector('.js-moves')
.innerHTML = `You ${yourmove} - ${compmove} Computer`;
}
function updateScoreElement() {
document.querySelector('.js-score')
.innerHTML = `Wins: ${score.wins}, Losses: ${score.losses}, Ties: ${score.ties}`;
}
function pickComputerMove() {
const randomNumber = Math.random();
if ((0 < randomNumber) && (randomNumber < 1/3) ) {
computerMove = 'rock';
}
else if ((1/3 < randomNumber) && (randomNumber < 2/3) ) {
computerMove = 'paper';
}
else {
computerMove = 'scissors';
}
return computerMove;
}
function PlayGame(PlayerMove) {
if(playerMove === 'rock'){
if (computerMove === 'rock') {
result = 'tie';
}
else if (computerMove === 'paper') {
result = 'You lose';
}
else {
result = 'You win';
}
if (result === 'You win') {
score.wins++;
} else if (result === 'You lose') {
score.losses++;
} else if (result === 'tie') {
score.ties++;
}
// saving score into local storage but local storage only uses strings so we first convert JAVA Object > JSON string
localStorage.setItem('score', JSON.stringify(score));
updateResult(result);
updateScoreElement();
}
else if(playerMove === 'paper') {
if (computerMove === 'rock') {
result = 'You win';
}
else if (computerMove === 'paper') {
result = 'tie';
}
else {
result = 'You lose';
}
if (result === 'You win') {
score.wins++;
} else if (result === 'You lose') {
score.losses++;
} else if (result === 'tie') {
score.ties++;
}
localStorage.setItem('score', JSON.stringify(score));
updateResult(result);
updateScoreElement();
}
else if (playerMove === 'scissors') {
if (computerMove === 'rock') {
result = 'You lose';
}
else if (computerMove === 'paper') {
result = 'You win';
}
else {
result = 'tie';
}
if (result === 'You win') {
score.wins++;
} else if (result === 'You lose') {
score.losses++;
} else if (result === 'tie') {
score.ties++;
}
localStorage.setItem('score', JSON.stringify(score));
updateResult(result);
updateScoreElement();
}
}
</script>
</body>
</html>