-
Notifications
You must be signed in to change notification settings - Fork 0
/
RockPaperScissors.js
103 lines (83 loc) · 2.63 KB
/
RockPaperScissors.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
//globals
let playerScore = 0;
let computerScore = 0;
//the computers choice
function computerPlay(){
let computerChoice = Math.floor((Math.random() * 3) + 1);
switch(computerChoice){
case 1:
console.log("Computer: Rock");
return 1;
break;
case 2:
console.log("Computer: Paper");
return 2;
break;
case 3:
console.log("Computer: Scissors");
return 3;
break;
default:
console.log("Something has gone wrong.");
}
}
//the players choice
function playerPlay() {
let playerChoice = prompt("1: Rock, 2: Paper, 3: Scissors.");
switch(playerChoice){
case "1":
console.log("Player: Rock");
return 1;
break;
case "2":
console.log("Player: Paper");
return 2;
break;
case "3":
console.log("Player: Scissors");
return 3;
break;
default:
console.log("Please enter a valid choice.")
}
}
//determines who wins and loses
function playRound(playerSelection, computerSelection){
if (((playerSelection == 2 || 3) && (playerSelection > computerSelection)) || ((playerSelection === 1) && computerSelection === 3))
{
playerScore++;
console.log(`The player has won! The score is P:${playerScore} and C:${computerScore}`);
} else if(playerSelection === computerSelection) {
console.log(`It's a draw! The score is P:${playerScore} and C:${computerScore}`);
} else {
computerScore++;
console.log(`The computer has won! The score is P:${playerScore} and C:${computerScore}`);
}
}
//runs the game and displays final score
function game(){
//round 1
console.log("Round 1");
playRound(playerPlay(), computerPlay());
//round 2
console.log("Round 2");
playRound(playerPlay(), computerPlay());
//round 3
console.log("Round 3");
playRound(playerPlay(), computerPlay());
//round 4
console.log("Round 4");
playRound(playerPlay(), computerPlay());
//round 5
console.log("Round 5");
playRound(playerPlay(), computerPlay());
//displays overall score and winner
if(playerScore > computerScore) {
console.log(`The game is over, the player has won!. The final score is p:${playerScore} and C:${computerScore}.`)
} else if (computerScore > playerScore) {
console.log(`The game is over, the computer has won! The final score is p:${playerScore} and C:${computerScore}.`)
} else {
console.log(`The game is over, it's a draw! The final score is p:${playerScore} and C:${computerScore}.`)
}
}
game();