-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
232 lines (199 loc) · 8.62 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html>
<head>
<style>
#box {
border: 1px solid black;
height: 284px;
width: 637px;
background-color: grey;
text-align: center;
}
p {
display: block;
color: rgb(241, 241, 241);
}
#choices {
display: flex;
align-items: center;
justify-content: center;
}
.btn {
padding: 5px;
width: 100px;
font-size: 20px;
margin: 5px;
background-color: lightblue;
border-radius: 10px;
}
/*#resultBox{}*/
#result {
height: 18.4px;
}
#scoreBoard{
display: inline;
}
#result1 {
border: 2px solid black;
background-color: rgb(97, 97, 97);
color: orange;
width: 247px;
float: left;
margin-left: 20px;
text-align: center;
}
#result2 {
border: 2px solid black;
background-color: rgb(97, 97, 97);
color: orange;
width: 247px;
margin-right: 20px;
float: right;
text-align: center;
}
#vs {
color: orange;
text-align: center;
display: inline;
}
</style>
</head>
<main>
<h1>Rock Paper Scissors</h1>
<div id="box">
<p>Player. Pick Your Weapon!</p>
<div id="choices">
<button class="btn" name="Rock">Rock</button>
<button class="btn" name="Paper">Paper</button>
<button class="btn" name="Scissors">Scissors</button>
</div>
<div id="resultBox"><p id="result"> </p></div>
<div id="scoreBoard">
<div id="result1">
<h4>Human</h4>
<div class="score"><h1 id="resultHuman" data-value='0'>0</h1></div>
</div>
<h1 id="vs">VS</h1>
<div id="result2">
<h4>Computer</h4>
<div class="score"><h1 id="resultComputer" data-value='0'>0</h1></div>
</div>
</div>
</div>
<script>
// Computer play Functional and returns
function computerPlay() {
let randomized = Math.floor(Math.random() * 3);
let choice = "";
switch (randomized) {
case 0:
choice = "Rock"
break;
case 1:
choice = "Paper"
break;
case 2:
choice = "Scissors"
}
return choice;
}
//playerSelection resultou e retornou valor
function playerSelection () {
var player = prompt("Pick your 'weapon'");
player = player.charAt(0).toUpperCase() + player.slice(1).toLowerCase();
if (player === "Rock" || player === "Paper" || player === "Scissors"){
return player;
} else
alert("Please make a valid choice");
playerSelection();
}
function playRound(hum, pc) {
if (pc === hum) {
document.getElementById('result').innerHTML = "This is a draw. Draw again";
}
switch (hum){
case "Rock":
switch(pc){
case "Paper":
document.getElementById('result').innerHTML = "You Lose! " + pc + " beats " + hum ;
return 2; //Pc wins;
break;
case "Scissors":
document.getElementById('result').innerHTML = "You Win! " + hum + " beats " + pc;
return 1;
}
break;
case "Paper":
switch(pc){
case "Scissors":
document.getElementById('result').innerHTML = "You Lose! " + pc + " beats " + hum ;
return 2;
break;
case "Rock":
document.getElementById('result').innerHTML = "You Win! " + hum + " beats " + pc;
return 1;
}
break;
case "Scissors":
switch(pc){
case "Rock":
document.getElementById('result').innerHTML = "You Lose! " + pc + " beats " + hum ;
return 2;
break;
case "Paper":
document.getElementById('result').innerHTML = "You Win! " + hum + " beats " + pc;
return 1;
}
break
}
}
/*function game() {
let pc = 0;
let hum = 0;
for(var i = 0; i < 5; i++) {
let gameOn = playRound(playerSelection(), computerPlay())
if (gameOn == 1) {
hum++
}
else if(gameOn == 0){
pc++;
}
console.log("Results pc: " + pc + " human " + hum)
}
if (pc === hum) {
alert("This match is tied. Next winner takes it all")
let final= playRound(playerSelection(), computerPlay())
if (final == 1) {console.log("Human Wins")}
else if(final == 0){console.log("Computer Wins");}
console.log("Results pc: " + pc + " human " + hum)
} else if(pc > hum) {
console.log("Computer Wins")
} else console.log("Human Wins")
}
*/
const btn = document.getElementById('choices');
btn.addEventListener('click', function(e) {
let play = e.originalTarget.innerHTML;
switch(playRound(play, computerPlay())) {
case 1:// retorna 1 para vitoria hum
var resultHuman = document.getElementById('resultHuman');
resultHuman.dataset.value++;
resultHuman.innerHTML = resultHuman.dataset.value;
if (resultHuman.innerHTML == 3) {
alert("You Win");
location.reload();
}
break;
case 2: // Computador ganha
var resultComputer = document.getElementById('resultComputer');
resultComputer.dataset.value++;
resultComputer.innerHTML = resultComputer.dataset.value;
if(resultComputer.innerHTML == 3) {
alert("You Loose");
location.reload();
}
}
})
</script>
</main>
</html>