-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps-script.js
108 lines (95 loc) · 2.84 KB
/
rps-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
//a program that plays a game of rock-paper-scissors
function getComputerChoice(){
let computerInput = Math.random();
//console.log("random is " + Choice);
if(computerInput <= 0.3){
return "rock";
} else if(computerInput > 0.6){
return "paper";
} else return "scissors";
}
function getPlayerChoice(){
let playerInput = prompt("Choose either 'rock', 'paper' or 'scissors'").toLowerCase();
return playerInput;
}
function inputCheck(pInput){
if(pInput == "rock" || pInput == "paper" || pInput == "scissors"){
return 1;
} else return 0;
}
function playRpsRound(cChoice, pChoice){
if(inputCheck(pChoice) == 0){
console.log("We can't play if you don't choose either 'rock', 'paper' or 'scissors'");
roundCount -= 1;
return;
}
let outcome;
switch(cChoice){
case "rock":
switch(pChoice){
case "rock":
outcome = 0;
break;
case "paper":
outcome = 1;
break;
case "scissors":
outcome = -1;
}
break;
case "paper":
switch(pChoice){
case "rock":
outcome = -1;
break;
case "paper":
outcome = 0;
break;
case "scissors":
outcome = 1;
}
break;
case "scissors":
switch(pChoice){
case "rock":
outcome = 1;
break;
case "paper":
outcome = -1;
break;
case "scissors":
outcome = 0;
}
break;
}
console.log(`The computer chose '${cChoice}', you chose '${pChoice}'`);
if(outcome == -1){
computerWins += 1;
return "The computer wins this round";
} else if(outcome == 1){
playerWins += 1;
return "You win this round!";
} else return "It's a draw this round";
}
function playRpsGame(){
let computerSeleciton = getComputerChoice();
let playerSelection = getPlayerChoice();
console.log(playRpsRound(computerSeleciton, playerSelection));
if(roundCount < 4){
roundCount += 1;
playRpsGame();
}
roundCount -= 1;
if(roundCount == 0){
console.log(`The computer has ${computerWins} wins, you have ${playerWins} wins`);
let result = computerWins - playerWins;
if(result > 0){
console.log("The computer won");
} else if(result == 0){
console.log("The game is a draw");
} else console.log("You won!");
}
}
let roundCount = 0;
let computerWins = 0, playerWins = 0;
playRpsGame();