Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
159 lines (145 loc) · 4.03 KB
/
Copy pathindex.html
File metadata and controls
159 lines (145 loc) · 4.03 KB
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
<html>
<header>
<link rel = "stylesheet" type = "text/css" href = "./rps.css">
</header>
<div id = 'body'>
<div id = myButtons>
<button id = 'rock' >Rock</button>
<button id = 'paper'>Paper</button>
<button id = 'scissors'>Scissors</button>
</div>
<div id = 'lowerTxt'><div id = "upperTextFeild"></div></div>
</div>
<script>
let caseComputerSelection;
let casePlayerSelection;
let playerWins = 0;
let computerWins = 0;
let ties = 0;
let cSelection;
const utxtFeild = document.querySelector('#upperTextFeild');
const rockButton = document.querySelector('#rock');
const paperButton = document.querySelector('#paper');
const scissorsButton = document.querySelector('#scissors');
const lowerDiv = document.querySelector('#lowerTxt');
const ltxtfeild = document.createElement('p');
const pscore = document.createElement('p');
const cscore = document.createElement('p');
const tieCount = document.createElement('p');
let roundNumber = 1;
lowerDiv.appendChild(ltxtfeild);
lowerDiv.appendChild(pscore);
lowerDiv.appendChild(cscore);
lowerDiv.appendChild(tieCount);
let pSelection;
rockButton.addEventListener('click',() => {
pSelection = "rock";
game()
})
paperButton.addEventListener('click',() => {
pSelection = "paper";
game()
})
scissorsButton.addEventListener('click',() => {
pSelection = "scissors";
game()
})
function game()
{
let result;
utxtFeild.textContent = "Round "+ roundNumber + ": Cick Rock, Paper, or Scissors";
cSelection = computerPlay();
result = playRound(pSelection,cSelection);
ltxtfeild.textContent = ""+ result;
pscore.textContent = "Your Score: " + playerWins;
cscore.textContent = "Computers Score: " + computerWins;
tieCount.textContent = "ties: " + ties;
roundNumber++;
lowerDiv.appendChild(ltxtfeild);
}
function computerPlay ()
{
let randomInt = Math.floor(Math.random()*3)+1;
switch (randomInt)
{
case 1:
return "Rock";
break;
case 2:
return "Paper";
break;
case 3:
return "Scissors";
break;
}
}
function playRound(playerSelection, computerSelection){
casePlayerSelection = playerSelection.toLowerCase();
caseComputerSelection = computerSelection.toLowerCase();
switch(casePlayerSelection)
{
case "rock":
switch(caseComputerSelection)
{
case "rock":
return tie();
break;
case "paper":
return loss();
break;
case "scissors":
return win();
break;
}
break;
case "paper":
switch(caseComputerSelection)
{
case "rock":
return win();
break;
case "paper":
return tie();
break;
case "scissors":
return loss();
break;
}
break;
case "scissors":
switch(caseComputerSelection)
{
case "rock":
return loss();
break;
case "paper":
return win();
break;
case "scissors":
return tie();
break;
}
break;
}
}
function capitalize(myString)
{
return myString.slice(0,1).toUpperCase().concat(myString.slice(1));
}
function win()
{
playerWins++;
return "You Win! " + capitalize(casePlayerSelection) + " Beats " + capitalize(caseComputerSelection);
}
function loss()
{
computerWins++;
return "You Loose! " + capitalize(casePlayerSelection) + " Looses to " + capitalize(caseComputerSelection);
}
function tie()
{
ties++;
return "You Tie! " + capitalize(casePlayerSelection) + " Ties with " + capitalize(caseComputerSelection);
}
</script>
</html>