-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
194 lines (169 loc) · 4.89 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const playerScore = document.querySelector('.player-score');
const computerScore = document.querySelector('.computer-score');
const rockButton = document.querySelector('.rock-button');
const paperButton = document.querySelector('.paper-button');
const scissorsButton = document.querySelector('.scissors-button');
const roundResult = document.querySelector('.score-2 h5');
const finalResult = document.querySelector('.popup');
const finalMessage = document.querySelector('.final-message');
const finalResultButton = document.querySelector('.popup button');
rockButton.addEventListener('click', e => game('rock'));
paperButton.addEventListener('click', e => game('paper'));
scissorsButton.addEventListener('click', e => game('scissors'));
finalResultButton.addEventListener('click', e => resetGame());
function computerPlay()
{
let randomNumber = Math.ceil(Math.random() * 3);
switch (randomNumber)
{
case 1:
return "Rock";
break;
case 2:
return "Paper";
break;
case 3:
return "Scissors";
break;
}
}
function compareAnswers(playerSelection, computerSelection)
{
if (playerSelection == 'rock')
{
switch (computerSelection)
{
case "Rock":
return 'Tie!';
break;
case "Paper":
return "You Lose! Paper beats Rock";
break;
case "Scissors":
return "You Win! Rock beats Scissors"
break;
}
}
else if (playerSelection == 'paper')
{
switch (computerSelection)
{
case "Rock":
return "You Win! Paper beats Rock";
break;
case "Paper":
return "Tie!";
break;
case "Scissors":
return "You Lose! Scissors beats Paper"
break;
}
}
else if (playerSelection == 'scissors')
{
switch (computerSelection)
{
case "Rock":
return "You Lose! Rock beats Scissors";
break;
case "Paper":
return "You Win! Scissors beats Paper";
break;
case "Scissors":
return "Tie!"
break;
}
}
}
function playRound(playerSelection, computerSelection)
{
let result = compareAnswers(playerSelection, computerSelection);
return result;
}
// function checkForValidAnswer(playerSelection)
// {
// // do
// // {
// // if (playerSelection != null)
// // {
// // alert('Please give a Valid answer!');
// // }
// // playerSelection = prompt("Rock/Paper/Scissors").toLocaleLowerCase();
// // } while (playerSelection != 'rock' &&
// // playerSelection != 'paper' &&
// // playerSelection != 'scissors')
// return playerSelection;
// }
function countWins(result, playerWins, computerWins)
{
let objectWins = {};
if (result.includes("Win"))
{
playerWins++;
}
else if (result.includes("Lose"))
{
computerWins++;
}
objectWins.playerWins = playerWins;
objectWins.computerWins = computerWins;
return objectWins;
}
function declareWinner(playerWins, computerWins)
{
if (playerWins > computerWins)
{
return ('You win the Game!');
console.log('You win the Game!');
}
else if (computerWins > playerWins)
{
return ('Computer wins the Game!');
console.log('Computer wins the Game!');
}
else
{
return ('The game is a Tie!');
console.log('The game is a Tie!');
}
}
let playerWins = 0;
let computerWins = 0;
let winsCount = {};
function game(playerSelection)
{
console.log('Best out of 5 Wins!')
// for (let i = 0; i < 5; i++)
// {
// let playerSelection;
// playerSelection = checkForValidAnswer(playerSelection);
const computerSelection = computerPlay();
let result = playRound(playerSelection, computerSelection);
roundResult.textContent = result;
console.log(result);
winsCount = countWins(result, playerWins, computerWins);
playerWins = winsCount.playerWins;
computerWins = winsCount.computerWins;
playerScore.textContent = `Player: ${playerWins}`;
computerScore.textContent = `Computer: ${computerWins}`;
// }
console.log('Player: ' + playerWins + ' - ' + 'Computer: ' + computerWins);
if (playerWins >= 5 || computerWins >= 5)
{
popup(declareWinner(playerWins, computerWins));
}
}
function popup(message)
{
finalResult.style.visibility = 'visible';
finalMessage.textContent = message;
}
function resetGame()
{
playerWins = 0;
computerWins = 0;
playerScore.textContent = `Player: ${playerWins}`;
computerScore.textContent = `Computer: ${computerWins}`;
finalResult.style.visibility = 'hidden';
}
// game();