-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
187 lines (141 loc) · 5.8 KB
/
script.js
File metadata and controls
187 lines (141 loc) · 5.8 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
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
'use strict'
document.addEventListener('DOMContentLoaded', () => {
const NUMBER_OF_CHOICES = 3;
let humanScore = 0, computerScore = 0;
let roundNumber = 1;
const DEFAULT_WINNING_SCORE = 5;
function playRound(humanChoice) {
function getComputerChoice() {
let computerChoice = parseInt(Math.random() * NUMBER_OF_CHOICES + 1);
return computerChoice;
}
let computerChoice = getComputerChoice();
function nameOf(choice) {
switch (choice) {
case 1: return 'rock';
case 2: return 'paper';
case 3: return 'scissors';
default: return null;
}
}
function roundResult(humanChoice, computerChoice) {
if (humanChoice === computerChoice) {
return 2;
}
if (humanChoice < computerChoice) {
return (humanChoice === 1 && computerChoice === 3) ? 1 : 0;
} else {
return (humanChoice === 3 && computerChoice === 1) ? 0 : 1;
}
}
const roundResult_ = roundResult(humanChoice, computerChoice);
function setScores() {
if (roundResult_ !== 2) {
roundResult_ ? humanScore++ : computerScore++;
}
}
setScores();
function displayRoundStats() {
function displaySelectedChoices() {
const humanChoiceDiv = document.querySelector('.human-stats .choice');
const computerChoiceDiv = document.querySelector('.computer-stats .choice');
const selectedHumanChoice = document.querySelector(`.button.choice.${nameOf(humanChoice)}`).cloneNode(true);
const selectedComputerChoice = document.querySelector(`.button.choice.${nameOf(computerChoice)}`).cloneNode(true);
selectedHumanChoice.setAttribute("disabled", "");
selectedHumanChoice.setAttribute("disabled", "");
humanChoiceDiv.replaceWith(selectedHumanChoice);
computerChoiceDiv.replaceWith(selectedComputerChoice);
}
function displayRoundResults() {
const humanScoreDiv = document.querySelector('.human-score');
const computerScoreDiv = document.querySelector('.computer-score');
humanScoreDiv.textContent = humanScore;
computerScoreDiv.textContent = computerScore;
}
function displayHumanStatus() {
const winColor = "rgb(0, 255, 0)", loseColor = "rgb(255, 0, 0)", drawColor = "rgb(255, 255, 255)";
const colors = [loseColor, winColor, drawColor];
function setColors() {
const bodyDiv = document.querySelector('.body');
const gameStatsDiv = document.querySelector('.stats');
const color = colors.at(roundResult_);
bodyDiv.style.borderColor = gameStatsDiv.style.borderColor = color;
}
const roundPossibilities = ["Round lost!", "Round won!", "Draw..."];
const gamePossibilities = ["Game lost!", "Game won!"];
const humanStatusDiv = document.querySelector('.game-result');
const possibilities = isGameOver() ? gamePossibilities : roundPossibilities;
humanStatusDiv.textContent = possibilities.at(roundResult_);
humanStatusDiv.style.color = colors.at(roundResult_);
}
displaySelectedChoices();
displayRoundResults();
displayHumanStatus();
}
displayRoundStats();
roundNumber++;
const roundNumberSpan = document.querySelector('.round-title .highlight');
roundNumberSpan.textContent = roundNumber;
}
function isGameOver() {
return humanScore === DEFAULT_WINNING_SCORE || computerScore === DEFAULT_WINNING_SCORE;
}
function checkRestart() {
if (isGameOver()) {
const restart = document.querySelector('.restart');
restart.removeAttribute("hidden");
restart.addEventListener('click', () => {
window.location.reload();
});
window.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
window.location.reload();
}
})
}
}
window.addEventListener('beforeunload', function (e) {
if (!isGameOver()) {
e.preventDefault();
e.returnValue = '';
}
})
function getHumanChoiceMouse(event) {
const button = event.target.closest('.button.choice');
if (!button) return;
if (button.classList.contains('rock')) return 1;
if (button.classList.contains('paper')) return 2;
if (button.classList.contains('scissors')) return 3;
}
function getHumanChoiceKeyboard(event) {
switch (event.key) {
case '1':
case 'r':
return 1;
case '2':
case 'p':
return 2;
case '3':
case 's':
return 3;
default: return null;
}
}
let humanChoice = null;
const choices = document.querySelector('.choices');
choices.addEventListener('click', function (e) {
if (!isGameOver()) {
humanChoice = getHumanChoiceMouse(e);
playRound(humanChoice);
checkRestart();
}
})
window.addEventListener('keydown', function (e) {
if (!isGameOver()) {
humanChoice = getHumanChoiceKeyboard(e);
if (humanChoice)
playRound(humanChoice);
checkRestart();
}
})
});