-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.js
135 lines (115 loc) · 3.45 KB
/
rps.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
const rock = document.querySelector('#rock');
const paper = document.querySelector('#paper');
const scissors = document.querySelector('#scissors');
const myScore = document.querySelector('#myScore');
const cpuScore = document.querySelector('#cpuScore');
const resultLog = document.querySelector('#resultLog');
const winText = document.querySelector('#winText');
var gameOver = false;
function getComputerChoice() {
let random = Math.floor(Math.random() * 3);
if (random === 0) {
return 'rock';
} else if (random === 1) {
return 'paper';
} else {
return 'scissors'
}
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return [`Tie, ${playerSelection} vs. ${computerSelection}`, null];
}
if (playerSelection == 'rock') {
if (computerSelection == 'paper') {
return [`You Lose, ${computerSelection} beats ${playerSelection}`, 0];
}
if (computerSelection == 'scissors') {
return [`You Win, ${playerSelection} beats ${computerSelection}`, 1];
}
}
if (playerSelection == 'paper') {
if (computerSelection == 'scissors') {
return [`You Lose, ${computerSelection} beats ${playerSelection}`, 0];
}
if (computerSelection == 'rock') {
return [`You Win, ${playerSelection} beats ${computerSelection}`, 1];
}
}
if (playerSelection == 'scissors') {
if (computerSelection == 'rock') {
return [`You Lose, ${computerSelection} beats ${playerSelection}`, 0]
}
if (computerSelection == 'paper') {
return [`You Win, ${playerSelection} beats ${computerSelection}`, 1]
}
}
}
function scoreGame(myScore) {
if (myScore == null) {
return [0, 0];
} else if (myScore == 1) {
return [1, 0];
} else {
return [0, 1];
}
}
function chooseWinner(scores) {
if (scores[0] > scores[1]) {
return `You win ${scores[0]} to ${scores[1]}`;
} else if (scores[0] < scores[1]) {
return `You lose ${scores[1]} to ${scores[0]}`;
} else {
return `Tie ${scores[1]} to ${scores[0]}`;
}
}
var round = 1;
var score = [0, 0];
function processClick(text, winner) {
var result;
result = scoreGame(winner);
// Updates score
score[0] += result[0];
score[1] += result[1];
// Updates DOM
myScore.innerHTML = score[0];
cpuScore.innerHTML = score[1];
resultLog.innerHTML += `<br />${round}. ${text}`;
round += 1;
if (score[0] == 5) {
winText.style.cssText = 'color: green';
winText.innerHTML = 'You Win';
gameOver = true;
}
else if (score[1] == 5) {
winText.style.cssText = 'color: red';
winText.innerHTML = 'You Lose';
gameOver = true;
}
}
function game() {
var text;
var winner;
rock.onclick = () => {
if (gameOver) {
return;
}
[text, winner] = playRound('rock', getComputerChoice());
processClick(text, winner);
}
paper.onclick = () => {
if (gameOver) {
return;
}
[text, winner] = playRound('paper', getComputerChoice());
processClick(text, winner);
}
scissors.onclick = () => {
if (gameOver) {
return;
}
[text, winner] = playRound('scissors', getComputerChoice());
processClick(text, winner);
}
}
game();