-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.html
119 lines (91 loc) · 3.8 KB
/
rps.html
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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
/* dude from TOP discord shared this approach.
while (userScore < 5) {
if (cpuScore == 5) break;
oneRound();
console.log(userScore);
}
*/
function game() {
let userScore = 0;
let cpuScore = 0;
// loop to run game until user or cpu reach 5
while (userScore < 5 && cpuScore < 5) {
oneRound();
}
function oneRound() {
function computerPlay() {
// define random numbers and their assignments
console.log("1 = rock \n2 = paper \n3 = scissors");
// Generate random number
let randomNumber = Math.floor((Math.random() * 3) + 1);
console.log("This round's random number is: " + randomNumber + ".");
// Assign rock, paper, or scissors based on random number
if (randomNumber === 1) {
return 'rock';
} else if (randomNumber === 2) {
return 'paper';
} else {
return 'scissors';
}
}
//console.log("The computer has played: " + computerPlay() + ".");
function playRound(userSelection,cpuSelection) {
// round outcomes
if (userSelection === "rock") {
if (cpuSelection === "paper") {
cpuScore++;
return "Paper beats rock. You lose!";
} else if (cpuSelection === "scissors") {
userScore++;
return "Rock beats scissors. You win!";
} else if (userSelection === cpuSelection) {
return "Rock can't beat rock. This is a draw.";
}
} else if (userSelection === "paper") {
if (cpuSelection === "rock") {
userScore++;
return "Paper beats rock. You win!"
} else if (cpuSelection === "scissors") {
cpuScore++;
return "Scissors beats paper. You lose!"
} else if (userSelection === cpuSelection) {
return "Paper can't beat paper. This is a draw."
}
} else if (userSelection === "scissors") {
if (cpuSelection === "rock") {
cpuScore++;
return "Rock beats scissors. You lose!"
} else if (cpuSelection === "paper") {
userScore++;
return "Scissors beats paper. You win!"
} else if (userSelection === cpuSelection) {
return "Scissors can't beat scissors. This is a draw."
}
}
}
const userSelection = prompt("Enter: Rock, Paper, or Scissors.").toLowerCase();
console.log("The human has selected: " + userSelection + ".");
const cpuSelection = computerPlay();
console.log("The computer has selected: " + cpuSelection + ".");
console.log(playRound(userSelection,cpuSelection));
console.log("The human score is: " + userScore + ".");
console.log("The computer score is: " + cpuScore + ".");
}
if (userScore === 5) {
return "The human wins!";
} else {
return "The computer wins!";
}
}
console.log(game());
</script>
</body>
</html>