-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
149 lines (125 loc) · 5.94 KB
/
script.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
let pick = ["rock", "paper", "scissors"];
// The computer gives a random choice between rock, paper and scissors
function getComputerChoice() {
return pick[Math.floor(Math.random() * 3)];
}
// The player picks rock, paper or scissors
function getHumanChoice() {
return prompt("Rock, paper, scissors?").toLowerCase();
}
// Declare the players score variables
let humanScore;
let computerScore;
// Declare the desire to do another round variable
let cont;
// Single round logic
// Rock beats scissors, paper beats rock, scissors beat paper
function playRound(humanChoice, computerChoice) {
// User picks something different from what the computer picks
if (computerChoice !== humanChoice && (humanChoice === "rock" || humanChoice === "paper" || humanChoice === "scissors")) {
// User picks rock
if (humanChoice === "rock") {
if (computerChoice === "scissors") {
console.log(`You win! The other player chose scissors. Rock beats scissors. \nUser: ${++humanScore}, computer: ${computerScore}`);
} else if (computerChoice === "paper") {
console.log(`You lose! The other player chose paper. Paper beats rock. \nUser: ${humanScore}, computer: ${++computerScore}`);
}
// User picks paper
} else if (humanChoice === "paper") {
if (computerChoice === "rock") {
console.log(`You win! The other player chose rock. Paper beats rock. \nUser: ${++humanScore}, computer: ${computerScore}`)
} else if (computerChoice === "scissors") {
console.log(`You lose! The other player chose scissors. Scissors beat paper. \nUser: ${humanScore}, computer: ${++computerScore}`);
}
// User picks scissors
} else if (humanChoice === "scissors") {
if (computerChoice === "paper") {
console.log(`You win! The other player chose paper. Scissors beat paper. \nUser: ${++humanScore}, computer: ${computerScore}`)
} else if (computerChoice === "rock") {
console.log(`You lose! The other player chose rock. Rock beats scissors. \nUser: ${humanScore}, computer: ${++computerScore}`);
}
}
// User picks the same thing the computer picked
} else if (humanChoice === computerChoice) {
console.log(`It's a draw! The other player chose ${computerChoice} as well.`);
} else {
console.error("Try again.");
return cont = "again";
}
// Let user decide if he/she wants to play another round
return cont = prompt("Do you want to play another round? Type n or no for no or anything else for yes.").toLowerCase();
}
// The whole game logic
console.log("This is a simple Rock Paper Scissors game. You will play against your computer. Each round, you'll have to choose rock, paper or scissors. The computer will choose something too. If you win, you get one point. If the computer wins, the computer gets the point. There supposed to be five rounds and if you decide to stop playing before the fifth round, you'll lose.");
let start = prompt("Type n to not play the game. Type anything else to start the game.");
if (start !== "n") {
let games = 0;
let fairWins = 0;
let wins = 0;
let rounds;
let fairResults = [];
let results = [];
// The whole game
while (start !== "n") {
games++;
rounds = 0;
humanScore = 0;
computerScore = 0;
cont = "y";
// One game
while(cont !== "n" && cont !== "no") {
rounds++;
if (rounds === 5 && cont !== "again") {
console.log("This game is supposed to be five-round. This is the fifth round, supposedly the last. But if you want to play extra rounds, just continue answering yes to the question at the end of rounds.");
}
playRound(getHumanChoice(), getComputerChoice());
if (cont === "again") {
rounds--;
continue;
}
if (rounds === 5) {
fairResults.push([humanScore, computerScore]);
}
}
results.push([humanScore, computerScore]);
// One game results
if (rounds < 5) {
console.log("You lose the game because you quit.");
fairResults.push([humanScore, computerScore]);
start = prompt("Do you want to play one more game?");
} else {
// if humanScore was greater than computerScore by the fifth round
if (fairResults[games - 1][0] > fairResults[games - 1][1]) {
fairWins++;
wins++;
console.log(`You win game ${games} fairly. Congratulations!`);
// if computerScore was greater than humanScore by the fifth round but humanScore is greater than computerScore now
} else if (fairResults[games - 1][1] > fairResults[games - 1][0] && humanScore > computerScore) {
console.log(`You lose game ${games} fairly, but you win if you count the extra rounds.`);
wins++;
// if computerScore was and is greater in both cases
} else if (fairResults[games - 1][1] > fairResults[games - 1][0] && humanScore < computerScore) {
console.log(`You lose game ${games}.`);
// if the scores were and are equal
} else if (fairResults[games - 1][0] === fairResults[games - 1][1] && humanScore === computerScore) {
console.log(`Game ${games} is a draw.`);
// if the scores were equal but aren't now
} else if (fairResults[games - 1][0] === fairResults[games - 1][1] && humanScore !== computerScore) {
if (humanScore > computerScore) {
console.log(`Fairly, game ${games} is a draw. But if you count the extra rounds, you win.`);
wins++;
} else if (computerScore) {
console.log(`Fairly, game ${games} is a draw. But if you count the extra rounds, you lose.`);
}
}
start = prompt("Do you want to play one more game?");
}
}
console.log("You finished your game. The results:");
console.log(`Fair wins: ${fairWins}\nWins: ${wins}`);
for(let i = 0; i < games; i++) {
console.log(`Game ${i + 1}: ${fairResults[i][0]}-${fairResults[i][1]} fairly, ${results[i][0]}-${results[i][1]} counting extra rounds`);
}
} else {
console.log("You declined the offer to play the game.");
}