-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
146 lines (109 loc) · 3.74 KB
/
game.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
// game.js
function getComputerChoice() {
let computerChoice = Math.floor(Math.random() * 3);
switch (computerChoice) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors"
}
}
function getHumanChoice() {
let humanChoice = prompt("Choose one: rock, paper or scissors", "").toLowerCase();
switch (humanChoice) {
case "rock":
case "paper":
case "scissors":
return humanChoice;
default:
alert("Bad input. Try again.")
return getHumanChoice();
}
}
function playGame() {
let computerScore = 0;
let humanScore = 0;
let countRound = 0;
function playRound(humanChoice, computerChoice) {
if (humanChoice == "rock" && computerChoice == "rock") {
alert("The round ended with a draw");
countRound++;
} else if (humanChoice == "paper" && computerChoice == "paper") {
alert("The round ended with a draw");
countRound++;
} else if (humanChoice == "scissors" && computerChoice == "scissors") {
alert("The round ended with a draw");
countRound++;
} else if (humanChoice == "rock" && computerChoice == "paper") {
alert("You lose! Paper beats Rock.");
alert("Computer scores +1 point.");
computerScore++;
countRound++;
} else if (humanChoice == "rock" && computerChoice == "scissors") {
alert("You win! Rock beats Scissors.");
alert("Human scores +1 point.");
humanScore++;
countRound++;
} else if (humanChoice == "paper" && computerChoice == "rock") {
alert("You win! Paper beats rock.");
alert("Human scores +1 point.");
humanScore++;
countRound++;
} else if (humanChoice == "paper" && computerChoice == "scissors") {
alert("You lose! Scissors beats Paper.");
alert("Computer scores +1 point.");
computerScore++;
countRound++;
} else if (humanChoice == "scissors" && computerChoice == "rock") {
alert("You lose! Rock beats Scissors.");
alert("Computer scores +1 point.");
computerScore++;
countRound++;
} else {
alert("You win! Scissors beats Paper.");
alert("Human scores +1 point.");
humanScore++;
countRound++;
}
}
function round1(){
let humanSelection = getHumanChoice();
let computerSelection = getComputerChoice();
playRound(humanSelection, computerSelection);
}
function round2(){
let humanSelection = getHumanChoice();
let computerSelection = getComputerChoice();
playRound(humanSelection, computerSelection);
}
function round3(){
let humanSelection = getHumanChoice();
let computerSelection = getComputerChoice();
playRound(humanSelection, computerSelection);
}
function round4(){
let humanSelection = getHumanChoice();
let computerSelection = getComputerChoice();
playRound(humanSelection, computerSelection);
}
function round5(){
let humanSelection = getHumanChoice();
let computerSelection = getComputerChoice();
playRound(humanSelection, computerSelection);
}
round1();
round2();
round3();
round4();
round5();
if (computerScore > humanScore) {
return alert("The game winner is: Computer.");
} else if (computerScore < humanScore) {
return alert("The game winner is: Human.");
} else {
return alert("The game ended with a draw.");
}
}
playGame();