/
rockPaperScissors
115 lines (100 loc) · 3.57 KB
/
rockPaperScissors
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rock, paper, scissors.</title>
</head>
<body>
<script>
function computerPlay(){
let pick = Math.floor(Math.random() * 3);
switch(pick){
case 0: return 'rock';
break;
case 1: return 'paper';
break;
case 2: return 'scissors';
}
}
function singleRound(playerSelection, computerSelection){
switch(playerSelection){
case 'rock':
switch(computerSelection){
case 'rock':
return 0;
break;
case 'paper':
return 2;
break;
case 'scissors':
return 1;
}
break;
case 'paper':
switch(computerSelection){
case 'rock':
return 1;
break;
case 'paper':
return 0;
break;
case 'scissors':
return 2;
}
break;
case 'scissors':
switch(computerSelection){
case 'rock':
return 2;
break;
case 'paper':
return 1;
break;
case 'scissors':
return 0;
}
}
}
function game(){
let counter = 1, p1 = 0, p2 = 0;
while(counter <= 5){
let playerSelection = prompt('Rock, paper or scissors?'), computerSelection = computerPlay();
playerSelection = playerSelection.toLowerCase();
console.log(`Round ${counter}`);
switch(singleRound(playerSelection, computerSelection)){
case 0:
console.log(`It's a tie! Both picked ${playerSelection}.`);
++p1, ++p2, ++counter;
break;
case 1:
if(playerSelection === 'scissors'){
console.log(`You win! scissors beat ${computerSelection}.`);
++p1, ++counter;
}else{
console.log(`You win! ${playerSelection} beats ${computerSelection}`);
++p1, ++counter;
}
break;
case 2:
if(computerSelection === 'scissors'){
console.log(`You lose! scissors beat ${playerSelection}.`);
++p2, ++counter;
}else{
console.log(`You lose! ${computerSelection} beats ${playerSelection}.`);
++p2, ++counter;
}
}
}
if(p1 === p2){
console.log(`After 5 rounds of play both players are tied with ${p1} wins each!`);
}
if(p1 > p2){
console.log(`After 5 rounds of play you are the winner with ${p1} wins to ${p2}.`);
}
if(p1 < p2){
console.log(`After 5 rounds of play you are the loser with ${p2} wins to ${p1}.`);
}
}
</script>
</body>
</html>