-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPSDom.html
178 lines (143 loc) · 4.77 KB
/
RPSDom.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
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors DOM</title>
<meta charset="UTF-8"/>
</head>
<body>
<center>
<h1> Welcome to Rock Paper Scissors</h1>
first to reach five games wins!
<div>
<p id="NewGameSpace">
<br>
<br>
</div>
<h2>Choose one:</h2>
<br>
<div id="container">
<button id="Rock">Rock</button>
<button id="Paper">Paper</button>
<button id="Scissors">Scissors</button>
</div>
<div id="player">
<br>
<br>
You chose
<p id="youChoose">
</div>
<div id="computer">
<center>
<br>
<br>
Computer chooses
<p id="computerChoice">
</div>
<div id="result">
<br>
<br>
And the result is ...
<p id="resultLine">
<br>
<br>
</div>
<table style="width:25%">
<tr>
<th>Player Score</th>
<th>Computer Score</th>
</tr>
<tr>
<center><td id="playerScoreboard"> </td></center>
<center></center><td id="computerScoreboard"> </td><center>
</tr>
</tr>
</table>
</center>
<script>
let playerScore = 0
let computerScore = 0
const computerChoice = document.querySelector("#computerChoice");
const youChoose = document.querySelector('#youChoose');
const resultLine = document.querySelector('#resultLine');
const playerScoreboard = document.querySelector('#playerScoreboard');
playerScoreboard.style.textAlign = "center";
const computerScoreboard = document.querySelector('#computerScoreboard');
computerScoreboard.style.textAlign = "center";
const cont = document.querySelector('#NewGameSpace');
const continueButton = document.createElement('button');
continueButton.textContent = 'New Game' ;
const Rock = document.querySelector('#Rock');
const Paper = document.querySelector('#Paper');
const Scissors = document.querySelector('#Scissors');
Rock.addEventListener('click', choiceFunction);
Paper.addEventListener('click', choiceFunction);
Scissors.addEventListener('click', choiceFunction);
function choiceFunction(e) {
playerSelection = e.target.id;
playRound();}
function computerPlay() {
const compnum = Math.round(Math.random() * (2 - 0) + 0);
if (compnum == 0) {return 'Rock';}
else if (compnum == 1) {return 'Paper';}
else {return 'Scissors';}
}
function playRound() {
const computerSelection = computerPlay();
computerChoice.textContent = computerSelection;
youChoose.textContent = playerSelection;
if (playerSelection === computerSelection)
{resultLine.textContent =("Tie this round");}
else if (playerSelection === "Rock" && computerSelection === "Scissors")
{(playerScore += 1);
resultLine.textContent =
("You win this round! " + playerSelection + " beats " + computerSelection +"."); }
else if (playerSelection === "Scissors" && computerSelection === "Rock")
{(computerScore += 1);
resultLine.textContent =
("You lose this round! " + computerSelection + " beats " + playerSelection + "."); }
else if (playerSelection.length > computerSelection.length)
{(playerScore += 1);
resultLine.textContent =
("You win this round! " + playerSelection + " beats " + computerSelection + "."); }
else if (computerSelection.length > playerSelection.length)
{(computerScore += 1);
resultLine.textContent =
("You lose this round! " + computerSelection + " beats " + playerSelection + "."); };
playerScoreboard.textContent = playerScore;
computerScoreboard.textContent = computerScore;
if (playerScore == 5) {alert("You win! \r\nGame Over. \r\nPress New Game to Play Again.");}
else if (computerScore == 5) {alert("You lose! \r\nGame Over. \r\nPress New Game to Play Again.");}
if (playerScore >= 5 || computerScore >= 5)
{
Rock.removeEventListener('click', choiceFunction);
Rock.addEventListener('click', endPlay);
Paper.removeEventListener('click', choiceFunction);
Paper.addEventListener('click', endPlay);
Scissors.removeEventListener('click', choiceFunction);
Scissors.addEventListener('click', endPlay);
NewGameSpace.appendChild(continueButton);
NewGameSpace.addEventListener('click', renewBoard)}
};
function renewBoard() {
playerScore = 0
computerScore = 0
playerScoreboard.textContent = playerScore;
computerScoreboard.textContent = computerScore;
computerChoice.textContent = " ";
youChoose.textContent = " ";
resultLine.textContent = " ";
Rock.removeEventListener('click', endPlay);
Rock.addEventListener('click', choiceFunction);
Paper.removeEventListener('click', endPlay);
Paper.addEventListener('click', choiceFunction);
Scissors.removeEventListener('click', endPlay);
Scissors.addEventListener('click', choiceFunction);
cont.removeChild(continueButton);
}
function endPlay() {
alert("You can't play further. Push the \'New Game\' button.")
console.log("game ended")
};
</script>
</body>
</html>