-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_func.js
162 lines (134 loc) · 4.51 KB
/
rpc_func.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
const rpc = ['rock', 'paper', 'scissors'];
//picks a random string from the rpc array
function computerPlay() {
let randomNum = Math.floor(Math.random() * 3);
return rpc[randomNum];
}
//Plays a single round and returns a winner
function singleRound(usr) {
usr = usr.charAt(0).toUpperCase() + usr.slice(1);
let compChoice = computerPlay()
compChoice = compChoice.charAt(0).toUpperCase() + compChoice.slice(1);
if (usr.toLowerCase() === compChoice.toLowerCase()) {
return "Its a tie!";
} else if (usr.toLowerCase() === 'rock' &&
compChoice.toLowerCase() === 'paper' ||
usr.toLowerCase() === 'paper' &&
compChoice.toLowerCase() === 'scissors' ||
usr.toLowerCase() === 'scissors' &&
compChoice.toLowerCase() === 'rock'
) {
return `You Lose! ${compChoice} beats ${usr}`;
} else {
return `You Win! ${usr} beats ${compChoice}`;
}
}
//Game is designed to run for 5 rounds and declare a winner at the end
/** function game() {
usrScore = 0;
compScore = 0;
//for loop goes over each round and adds 1 to the winner
for (let i = 0; i < 5; i++) {
let round = singleRound();
if (round.includes("You Win!")) {
usrScore += 1;
console.log(round);
} else if (round.includes("You lose!")) {
compScore += 1
console.log(round);
} else {
console.log(round);
continue;
}
}
//Checks who won overall
if (usrScore === compScore) {
return "Its a tie!";
} else if (usrScore > compScore) {
return "You win!";
} else {
return "You loose!";
}
}
**/
const userscore = document.querySelector('#usr_score');
const compscore = document.querySelector('#robot_score');
const winner = document.querySelector('#winner');
const usrquest = document.querySelector('#usrquest');
const header = document.querySelector("h4");
const rock = document.querySelector('#rock');
const paper = document.querySelector('#paper');
const scissors = document.querySelector('#scissors');
rock.addEventListener('click', function() {
if (singleRound('rock').includes("You Win!")) {
if (userscore.value < 5 && compscore.value < 5) {
userscore.value++;
winner.textContent = 'You won that round!';
}
} else if (singleRound('rock').includes("You Lose!")) {
if (userscore.value < 5 && compscore.value < 5) {
compscore.value++;
winner.textContent = 'Dang! You lost that round';
}
} else {
if (userscore.value < 5 && compscore.value < 5) {
winner.textContent = 'This round was a tie!';
}
}
winnerfunc();
});
paper.addEventListener('click', function() {
if (singleRound('paper').includes("You Win!")) {
if (userscore.value < 5 && compscore.value < 5) {
userscore.value++;
winner.textContent = 'You won that round!';
}
} else if (singleRound('paper').includes("You Lose!")) {
if (userscore.value < 5 && compscore.value < 5) {
compscore.value++;
winner.textContent = 'Dang! You lost that round';
}
} else {
if (userscore.value < 5 && compscore.value < 5) {
winner.textContent = 'This round was a tie!';
}
}
winnerfunc();
});
scissors.addEventListener('click', function() {
if (singleRound('scissors').includes("You Win!")) {
if (userscore.value < 5 && compscore.value < 5) {
userscore.value++;
winner.textContent = 'You won that round!';
}
} else if (singleRound('scissors').includes("You Lose!")) {
if (userscore.value < 5 && compscore.value < 5) {
compscore.value++;
winner.textContent = 'Dang! You lost that round';
}
} else {
if (userscore.value < 5 && compscore.value < 5) {
winner.textContent = 'This round was a tie!';
}
}
winnerfunc();
});
function winnerfunc() {
if (userscore.value == 5) {
winner.textContent = "You win!";
usrquest.textContent = "Play again?"
} else if (compscore.value == 5) {
winner.textContent = "Oh no! The computer won!";
usrquest.textContent = 'Play again?'
}
}
const playagain = document.querySelector('#playagain');
playagain.addEventListener('click', function() {
reset();
});
function reset() {
compscore.value = 0;
userscore.value = 0;
winner.textContent = " ";
usrquest.textContent = " ";
}