-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
287 lines (280 loc) · 12.5 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paper, Rock, Scissors</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="container_content">
<h1>Simple Rock Paper Scissors (or is it?)</h1>
<div class="content">
<div class="round_text">
<label for="textbox_number_of_games">Play Until # of Wins:</label>
<input id="textbox_number_of_games" sty type="text">
<p id="p_CurrentRound">Current Round: 0</p>
</div>
<div class="btn_start">
<button id="btn_start" class="btn_default" onclick="setup()">Start Game</button>
</div>
<div class="choice_stuff">
<p id="instructions">Input Number of Rounds to Play and Hit 'Start Game'</p>
<div class="choice_buttons">
<button id="btn_rock" class="btn_default btn_choice">Rock</button>
<button id="btn_paper" class="btn_default btn_choice">Paper</button>
<button id="btn_scissors" class="btn_default btn_choice">Scissors</button>
</div>
</div>
<div class="scoring">
<div class="your_scores">
<p class="score_header">Your Score:</p>
<p id="human_score" class="your_score score_header">0</p>
</div>
<div class="computer_scores">
<p class="score_header">Computer Score:</p>
<p id="computer_score" class="computer_score score_header">0</p>
</div>
<div class="your_score_details">
<div class="your_wins">
<p id="lbl_your_wins" class="score_detail">Wins:</p>
<p id="your_wins" class="score_detail">0</p>
</div>
<div class="your_ties">
<p class="score_detail">Ties:</p>
<p id="your_ties" class="score_detail">0</p>
</div>
<div class="your_losses">
<p class="score_detail">Losses:</p>
<p id="your_losses" class="score_detail">0</p>
</div>
</div>
<div class="computer_score_details">
<div>
<p id="lbl_computer_wins" class="score_detail">Wins:</p>
<p id="computer_wins" class="score_detail">0</p>
</div>
<div>
<p class="score_detail">Ties:</p>
<p id="computer_ties" class="score_detail">0</p>
</div>
<div>
<p class="score_detail">Losses:</p>
<p id="computer_losses" class="score_detail">0</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
const playsenum =
{
Rock: 'rock',
Paper: 'paper',
Scissors: 'scissors'
}
let plays = [playsenum.Rock, playsenum.Paper, playsenum.Scissors];
let human_choice, score,your_wins,your_losses ,your_ties , computer_wins ,computer_losses ,computer_ties , roundsPlayed;
let roundsToPlay = parseInt(document.getElementById('textbox_number_of_games').value);
human_choice = "";
score = 0, your_wins = 0,your_losses = 0,your_ties = 0;
computer_wins = 0,computer_losses = 0,computer_ties = 0;
roundsPlayed = 0;
const btn_start = document.getElementById('btn_start');
const btn_rock = document.getElementById('btn_rock');
const btn_paper = document.getElementById('btn_paper');
const btn_scissors = document.getElementById('btn_scissors');
const instructions = document.getElementById('instructions');
const lbl_your_losses = document.getElementById('your_losses');
const lbl_your_ties = document.getElementById('your_ties');
const lbl_your_wins = document.getElementById('your_wins');
const lbl_computer_wins = document.getElementById('computer_wins');
const lbl_computer_losses = document.getElementById('computer_losses');
const lbl_computer_ties = document.getElementById('computer_ties')
const human_score = document.getElementById('human_score')
const computer_score = document.getElementById('computer_score')
//#region Helper Functions
function random(number){
let res = Math.floor(Math.random()*number);
return res;
}
function assert(condition, message) {
if (!condition) {
message = message || "Assertion failed";
alert(message);
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}
function stopGame(e){
console.log(e.srcElement.textContent);
if (e.srcElement.textContent === 'Stop') {return null}
//find a way to stop
}
function getHumanChoice(e){
console.log(e.srcElement.textContent);
if (e.srcElement.textContent.toLowerCase() === playsenum.Rock) {
human_choice = playsenum.Rock;
}
else if (e.srcElement.textContent.toLowerCase() === playsenum.Paper) {
human_choice = playsenum.Paper;
}
else if (e.srcElement.textContent.toLowerCase() === playsenum.Scissors) {
human_choice = playsenum.Scissors;
}
//alert(`human: ${human_choice}`);
startPlaying();
}
function computerPlay () {
return plays[random(plays.length)];
}
function reset(){
human_choice = "";
score = 0, your_wins = 0,your_losses = 0,your_ties = 0;
computer_wins = 0,computer_losses = 0,computer_ties = 0;
roundsPlayed = 0;
}
//#endregion
//#region Main Functions
//Returns a string showing the result of game
function getGameResult(score, roundsToPlay, lbl_your_wins, lbl_your_ties, lbl_your_losses, lbl_computer_wins, lbl_computer_ties, lbl_computer_losses){
//positive score means player won by score amount of games. Negative means computer and 0 means tie.
//alert(`final score: ${score} and roundsToPlay: ${roundsToPlay}`);
assert(Math.abs(score) <= roundsToPlay, "The score must be less than or equal to roundsToPlay");
let gameResult, playerScore, computerScore, scoreMessage;
if (score == 0) {
scoreMessage = 'Looks like we have a tie!';
}
else if (score > 0){
scoreMessage = 'Humanity prevails THIS time';
}
else {
scoreMessage = 'Was there ever really ANY doubt about machine superiority?';
}
playerScore = (roundsToPlay + score) /2;
computerScore = roundsToPlay - playerScore;
return `Result of ${roundsToPlay} Round Game:\n\nYou won ${lbl_your_wins.textContent} games, lost ${lbl_your_losses.textContent} games, and tied ${lbl_your_ties.textContent} games.`
}
//The setting up the game.
function setup() {
lbl_your_losses.textContent = "0";
lbl_computer_losses.textContent = "0";
lbl_your_wins.textContent = "0";
lbl_computer_wins.textContent = "0";
lbl_your_ties.textContent = "0";
lbl_computer_ties.textContent = "0";
roundsToPlay = parseInt(document.getElementById('textbox_number_of_games').value);
if (!roundsToPlay){
alert("please enter an integer for the number of round you want to play");
return null;
}
btn_rock.classList.add('buttons_visible');
btn_paper.classList.add('buttons_visible');
btn_scissors.classList.add('buttons_visible');
instructions.textContent = "Make a Selection Below:";
btn_start.textContent = "Stop";
btn_paper.addEventListener('click',getHumanChoice);
btn_rock.addEventListener('click',getHumanChoice);
btn_scissors.addEventListener('click',getHumanChoice);
btn_start.addEventListener('click',stopGame);
}
//Starts the playing part
function startPlaying(){
let result = playOneRound(human_choice,computerPlay());
if (result.match('win')) {
score += 1;
your_wins += 1;
computer_losses += 1;
}
else if (result.match('lose')) {
score -= 1;
your_losses += 1;
computer_wins += 1;
}
else {
your_ties += 1;
computer_ties += 1;
score += 0;
}
lbl_computer_losses.textContent = computer_losses;
lbl_computer_wins.textContent = computer_wins;
lbl_computer_ties.textContent = computer_ties;
lbl_your_losses.textContent = your_losses;
lbl_your_wins.textContent = your_wins;
lbl_your_ties.textContent = your_ties;
human_score.textContent = (your_wins-your_losses);
computer_score.textContent = (computer_wins-computer_losses);
instructions.textContent = `${result}\nGo Again.`;
//alert(`checking to end: ${roundsPlayed >= roundsToPlay}`);
// if (roundsPlayed >= roundsToPlay) {
if (your_wins >= roundsToPlay || computer_wins >= roundsToPlay ) {
btn_start.textContent = "Start Game";
btn_rock.classList.remove('buttons_visible');
btn_paper.classList.remove('buttons_visible');
btn_scissors.classList.remove('buttons_visible');
let final_msg = getGameResult(score, roundsToPlay, lbl_your_wins, lbl_your_ties, lbl_your_losses, lbl_computer_wins, lbl_computer_ties, lbl_computer_losses);
instructions.textContent = your_wins >= roundsToPlay ? 'You got lucky THIS time...' : 'Maybe next time?';
reset();
}
roundsPlayed++;
}
//play one round
function playOneRound (playerSelection, computerSelection){
let msg, winningChoice, losingChoice, outcome = "";
let winningMsg = `You win because random interference in mind reading...\nI though you would pick ${(computerSelection == playsenum.Rock) ? playsenum.Scissors : (computerSelection == playsenum.Paper) ? playsenum.Rock : playsenum.Paper}`;
let losingMsg = `You lose because the computer knew you would pick ${playerSelection}...`;
let tieMsg = `Looks like a tie! You both chose ${playerSelection}`;
switch (playerSelection.toLowerCase()) {
case playsenum.Rock:
if (computerSelection === playsenum.Scissors) {
msg = winningMsg;;
winningChoice = playerSelection;
losingChoice = computerSelection;
}
else if (computerSelection === playsenum.Paper) {
msg = losingMsg;
winningChoice = computerSelection;
losingChoice = playerSelection;
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
msg = tieMsg;
}
break;
case playsenum.Paper:
if (computerSelection === playsenum.Rock) {
msg = winningMsg;
winningChoice = playerSelection;
losingChoice = computerSelection;
}
else if (computerSelection === playsenum.Scissors) {
msg = losingMsg;
winningChoice = computerSelection;
losingChoice = playerSelection;
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
msg = tieMsg;
}
break;
case playsenum.Scissors:
if (computerSelection === playsenum.Paper) {
msg = winningMsg;
winningChoice = playerSelection;
losingChoice = computerSelection;
}
else if (computerSelection === playsenum.Rock) {
msg = losingMsg;
winningChoice = computerSelection;
losingChoice = playerSelection;
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
msg = tieMsg;
}
break;
}
return (msg != tieMsg) ? `${msg}.\n${winningChoice} beats ${losingChoice}` : msg;
}
//#endregion
</script>