-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
173 lines (160 loc) · 5.91 KB
/
script.js
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
// player wins:
// PAPER v rock
// ROCK v scissors
// SCISSORS v paper
// computer wins:
// rock v PAPER
// paper v SCISSORS
// scissors v ROCK
const gameContainer = document.getElementById('game-container');
const play = document.getElementById('play');
const reset = document.getElementById('reset');
const playerChoice = document.querySelectorAll('.player-choice');
const playerScore = document.getElementById('player-score');
const computerScore = document.getElementById('computer-score');
const roundSummary = document.getElementById('round-summary');
const vs = document.getElementById('vs');
const fist = document.getElementById('game-fist');
const fistSVG = document.getElementById('fist');
const cRck = document.getElementById('c-rck');
const cPpr = document.getElementById('c-ppr');
const cScrs = document.getElementById('c-scrs');
let playerSelection;
let computerSelection;
let playerWins = 0;
let computerWins = 0;
let winnerText = '';
let counter = 1;
let roundArr = [];
window.addEventListener('load', () => {
play.addEventListener('click', game);
} )
reset.addEventListener('click', resetGame);
function updateToGameUI() {
const bgImg = document.getElementById('bg-img');
play.style.display = "none";
bgImg.style.display = "none";
gameContainer.style.visibility = 'visible';
}
function animateFist(){
fist.classList.add('animate');
fist.addEventListener('animationend', () => {
fist.classList.remove('animate');
});
}
function getWeapons(elem) {
fistSVG.style.opacity = ".2";
vs.innerText = "vs.";
if(elem.classList.contains('rck')) playerSelection = "rock";
if(elem.classList.contains('ppr')) playerSelection = "paper";
if(elem.classList.contains('scrs')) playerSelection = "scissors";
elem.classList.add('chosen');
computerPlay();
roundArr.push(computerSelection, playerSelection, elem);
return roundArr;
}
function computerPlay(){
let randomNum = Math.floor(Math.random() * 3) + 1;
computerSelection = (randomNum == 1)? "rock" :
(randomNum == 2)? "paper" : "scissors";
if(computerSelection === "rock") cRck.classList.add('cmptr-chosen');
if(computerSelection === "paper") cPpr.classList.add('cmptr-chosen');
if(computerSelection === "scissors") cScrs.classList.add('cmptr-chosen');
console.log(`computer selected ${computerSelection}`);
return computerSelection;
}
function playRound(computerSelection, playerSelection) {
const rock = "rock";
const paper = "paper";
const scissors = "scissors"
// str.localeCompare(str2) => 0 if same, -1 if 1st operand sorted first, 1 if 2nd operand sorted first..
if(playerSelection.localeCompare(computerSelection) == 0) {
winnerText = `Both you and the computer chose "${playerSelection}." That's a tie, please go again...`;
} else if((playerSelection.localeCompare(paper) == 0
&& computerSelection.localeCompare(rock) == 0)
|| (playerSelection.localeCompare(rock) == 0
&& computerSelection.localeCompare(scissors) == 0)
|| (playerSelection.localeCompare(scissors) == 0
&& computerSelection.localeCompare(paper) == 0)) {
winnerText = `You won this round, ${playerSelection} beats ${computerSelection}!!! `
playerWins +=1;
counter++;
} else {
winnerText = `You lost this round, ${computerSelection} beats ${playerSelection}. WhaaaWhaaaa.`
computerWins += 1;
counter++;
}
updateRoundUI();
return roundArr;
}
function updateRoundUI() {
if(playerWins === 5) {
winnerText = 'CONGRATULATIONS! You are the undisputed CHAMPION!!!';
}
if(computerWins === 5) {
winnerText = `The COMPUTER WINS!!! \n You simply must practice more. \n (Just kidding - this is a game of chance)`;
}
console.log(winnerText);
roundSummary.innerText = winnerText;
playerScore.innerText = playerWins;
computerScore.innerText = computerWins;
}
function getWeaponsAndPlay(event){
event.stopPropagation();
gameContainer.style.pointerEvents = 'none';
const elem = event.currentTarget;
getWeapons(elem);
playRound(computerSelection, playerSelection);
if(playerWins === 5 || computerWins === 5) {
gameContainer.style.pointerEvents = 'none';
playerChoice.forEach((choice) => {
choice.removeEventListener('click', getWeaponsAndPlay);
});
return;
}
setTimeout(resetRoundUI, 2500, computerSelection, elem);
}
function resetRoundUI(computerSelection, element) {
gameContainer.style.pointerEvents = 'auto';
element.classList.remove('chosen');
if(playerWins === 5 || computerWins === 5) return;
if(computerSelection === "rock") cRck.classList.remove('cmptr-chosen');
if(computerSelection === "paper") cPpr.classList.remove('cmptr-chosen');
if(computerSelection === "scissors") cScrs.classList.remove('cmptr-chosen');
fistSVG.style.opacity = "1";
vs.innerText = "";
winnerText = `Round ${counter}`;
roundSummary.innerText = winnerText;
animateFist();
roundArr.length = 0;
return roundArr;
}
function game(){
updateToGameUI();
animateFist();
playerChoice.forEach((choice) => {
choice.addEventListener('click', getWeaponsAndPlay);
});
}
function resetGame() {
playerChoice.forEach((choice) => {
choice.classList.remove('chosen');
});
if(computerSelection === "rock") cRck.classList.remove('cmptr-chosen');
if(computerSelection === "paper") cPpr.classList.remove('cmptr-chosen');
if(computerSelection === "scissors") cScrs.classList.remove('cmptr-chosen');
fistSVG.style.opacity = "1";
vs.innerText = "";
playerWins = 0;
computerWins = 0;
counter = 1;
winnerText = `Round ${counter}`;
roundSummary.innerText = winnerText;
playerScore.innerText = playerWins;
computerScore.innerText = computerWins;
playerChoice.forEach((choice) => {
choice.removeEventListener('click', getWeaponsAndPlay);
});
game();
}
function nothing(){return};