-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
139 lines (128 loc) · 4.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
const rock = document.querySelector('#rock').addEventListener('click',game);
const paper = document.querySelector('#paper').addEventListener('click',game);
const scissors = document.querySelector('#scissors').addEventListener('click',game);
const reset = document.querySelector('.reset').addEventListener('click',resetGame);
const clickedRock = document.querySelector('#clicked-r');
const clickedPaper = document.querySelector('#clicked-p');
const clickedScissors = document.querySelector('#clicked-s');
const compGuess_R = document.querySelector('#compguess-r');
const compGuess_P = document.querySelector('#compguess-p');
const compGuess_S = document.querySelector('#compguess-s');
const winMsg = document.querySelector('.winner');
const winContainer = document.querySelector('.win-container');
const compScore = document.querySelector('.comp-score');
const playerScore = document.querySelector('.player-score');
const roundsValue = document.querySelector('#input-value');
const popupContainer = document.querySelector('.popup-container');
const winnerCall = document.querySelector('.winner-call');
const prize = document.querySelector('.prize');
//---------------------------------------------------------------
function game(e){
const computerSelection = computerPlay();
function computerPlay() {
let computerGuess = ['rock','paper','scissors'];
return computerGuess[Math.floor(Math.random() * 3)];
}
playRound(computerSelection,e);
actionDisplay(computerSelection,e);
gameEnd();
}
//match up
function playRound(computerSelection,e) {
if (computerSelection === e.target.id) {
winMsg.innerHTML = 'Draw';
winMsg.style.color = 'black';
return;
}
if (computerSelection === 'rock') {
if (e.target.id === 'paper') {
winMsg.innerHTML = 'You Win';
winMsg.style.color = 'rgb(46, 206, 6)';
playerScore.innerHTML ++;
return;
}else
winMsg.innerHTML = 'You Lose 💩';
winMsg.style.color = 'red';
compScore.innerHTML ++;
return;
}
if (computerSelection === 'paper') {
if (e.target.id === 'rock') {
winMsg.innerHTML = 'You Lose 💩';
winMsg.style.color = 'red';
compScore.innerHTML ++;
return;
}else
winMsg.innerHTML = 'You Win';
winMsg.style.color = 'rgb(46, 206, 6)';
playerScore.innerHTML ++;
return;
}
if (computerSelection === 'scissors') {
if (e.target.id === 'rock') {
winMsg.innerHTML = 'You Win';
winMsg.style.color = 'rgb(46, 206, 6)';
playerScore.innerHTML ++;
return;
}else
winMsg.innerHTML = 'You Lose 💩';
winMsg.style.color = 'red';
compScore.innerHTML ++;
return;
}
}
//user and comp selection action display
function actionDisplay(computerSelection,e) {
if (e.target.id == 'rock') {
clickedRock.style.display = 'block';
}else clickedRock.style.display = 'none';
if (e.target.id == 'paper') {
clickedPaper.style.display = 'block';
}else clickedPaper.style.display = 'none';
if (e.target.id == 'scissors') {
clickedScissors.style.display = 'block';
}else clickedScissors.style.display = 'none';
if (computerSelection == 'rock') {
compGuess_R.style.display = 'block';
}else compGuess_R.style.display = 'none';
if (computerSelection == 'paper') {
compGuess_P.style.display = 'block';
}else compGuess_P.style.display = 'none';
if (computerSelection == 'scissors') {
compGuess_S.style.display = 'block';
}else compGuess_S.style.display = 'none';
}
function gameEnd() {
if ((Number(compScore.innerHTML) >= Number(roundsValue.value)) || (Number(playerScore.innerHTML) >= Number(roundsValue.value) || (Number(compScore.innerHTML)) >= 100 || (Number(playerScore.innerHTML)) >= 100)) {
document.getElementById("rock").disabled = true;
document.getElementById("paper").disabled = true;
document.getElementById("scissors").disabled = true;
popupContainer.style.display = 'block';
if (Number(compScore.innerHTML) > Number(playerScore.innerHTML)) {
winnerCall.innerHTML = 'You Lose 💩'
winnerCall.style.color = 'red';
}
if (Number(playerScore.innerHTML) > Number(compScore.innerHTML)) {
winnerCall.innerHTML = 'You Win';
winnerCall.style.color = 'rgb(46, 206, 6)';
prize.style.display = 'block';
}
}
}
function resetGame() { //play again button
playerScore.innerHTML = '0';
compScore.innerHTML = '0';
winMsg.innerHTML = '-----';
winMsg.style.color = 'black';
clickedRock.style.display = 'none';
clickedPaper.style.display = 'none';
clickedScissors.style.display = 'none';
compGuess_R.style.display = 'none';
compGuess_P.style.display = 'none';
compGuess_S.style.display = 'none';
popupContainer.style.display= 'none';
prize.style.display = 'none';
document.getElementById("rock").disabled = false;
document.getElementById("paper").disabled = false;
document.getElementById("scissors").disabled = false;
}