-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
149 lines (128 loc) · 4 KB
/
Copy pathcode.js
File metadata and controls
149 lines (128 loc) · 4 KB
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
let playerWins = 0;
let computerWins = 0;
let rounds = 0;
let chosenRounds = false;
let computerSelection = "";
let playerSelection = "";
welcome();
function welcome(){
console.log("Welcome to Rock-Scissors-Paper!");
startGame();
}
function chooseRounds(){
while(!chosenRounds){
rounds = parseInt(prompt("Best of how many rounds? (Odd numbers only)"));
if(rounds<10 && rounds>1 && rounds %2 !== 0){
console.log(rounds + " round game chosen.");
chosenRounds=true;
}
else{
console.log("Invalid input");
}
}
}
function startGame(){
chooseRounds();
for(let i = 0; i<rounds; i++){
computerSelection = "";
playerSelection = "";
console.log("\nPlayer: " + playerWins + " / Computer: " + computerWins);
playRound();
}
whoWon();
}
function playRound(playerSelection, computerSelection){
humanPlay();
computerPlay();
calculate();
}
function humanPlay(){
while(playerSelection != "rock" && playerSelection != "scissors" && playerSelection != "paper"){
playerSelection = prompt("Rock, paper, or scissors?");
if(playerSelection !=null){
playerSelection = playerSelection.toLowerCase();
console.log("You chose " + playerSelection);}
}
}
function computerPlay(){
let RNG = Math.floor(Math.random() * 3);
switch (RNG){
case 0:
computerSelection = "rock";
break;
case 1:
computerSelection = "scissors";
break;
case 2:
computerSelection = "paper";
break;
}
console.log("Computer chose " + computerSelection);
}
function calculate(){
switch(true){
case((playerSelection === "rock")&&(computerSelection === "rock")):
console.log("It's a tie.");
rounds+=1;
break;
case((playerSelection === "rock")&&(computerSelection === "scissors")):
console.log("You win the round.");
playerWins ++;
break;
case((playerSelection === "rock")&&(computerSelection === "paper")):
console.log("You lose the round.");
computerWins ++;
break;
case((playerSelection === "scissors")&&(computerSelection === "rock")):
console.log("You lose the round.");
computerWins ++;
break;
case((playerSelection === "scissors")&&(computerSelection === "scissors")):
console.log("It's a tie.");
rounds+=1;
break;
case((playerSelection === "scissors")&&(computerSelection === "paper")):
console.log("You win the round.");
playerWins ++;
break;
case((playerSelection === "paper")&&(computerSelection === "rock")):
console.log("You win the round.");
playerWins ++;
break;
case((playerSelection === "paper")&&(computerSelection === "scissors")):
console.log("You lose the round.");
computerWins ++;
break;
case((playerSelection === "paper")&&(computerSelection === "paper")):
console.log("It's a tie.");
rounds+=1;
break;
}
}
function whoWon(){
let again = "";
console.log("\nPlayer: " + playerWins + " / Computer: " + computerWins);
if(playerWins>computerWins){
console.log("You win! Congratulations.")
}
else if(computerWins>playerWins){
console.log("You lose! Better luck next time.")
}
console.log("Play another game?");
while(again !== "yes" && again !== "no"){
again=prompt("Play another game?");
again=again.toLowerCase();
}
if(again === "yes"){
playerWins = 0;
computerWins = 0;
rounds = 0;
chosenRounds = false;
computerSelection = "";
playerSelection = "";
startGame();
}
if(again === "no"){
console.log("Thanks for playing.");
}
}