-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
120 lines (108 loc) · 3.49 KB
/
Script.js
File metadata and controls
120 lines (108 loc) · 3.49 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
/*
* Arianna Biernacki
* Student ID: xxxxxxxxx
* SYST10199 - Web Programming
*/
var playAgain = true;
var playerOneWins = 0;
var computerWins = 0;
var gameResult;
var playerOneChoice;
do {
do{
var playerOne = prompt("Press 1 for ROCK" + "\n" + "Press 2 for PAPER" + "\n" + "Press 3 for SCISSOR");
var formattedPlayerOne = parseInt(playerOne);
} while(formattedPlayerOne !== 1 && formattedPlayerOne !== 2 && formattedPlayerOne !== 3);
var computer = Math.random() * (3 - 1) + 1;
var computerRounded = Math.round(computer);
function evaluateWinner(formattedPlayerOne, computer) {
switch (formattedPlayerOne) {
case 1:
playerOneChoice = "ROCK";
switch (computer) {
case 1:
playerTwoChoice = "ROCK";
gameResult = "tied.";
return;
case 2:
playerTwoChoice = "PAPER";
computerWins += 1;
gameResult = "lost.";
return;
case 3:
playerTwoChoice = "SCISSOR";
playerOneWins += 1;
gameResult = "won!";
return;
default:
break;
}
case 2:
playerOneChoice = "PAPER";
switch (computer) {
case 1:
playerTwoChoice = "ROCK";
playerOneWins += 1;
gameResult = "won!";
return;
case 2:
playerTwoChoice = "PAPER";
gameResult = "tied.";
return;
case 3:
playerTwoChoice = "SCISSOR";
computerWins += 1;
gameResult = "lost.";
return;
default:
break;
}
case 3:
playerOneChoice = "SCISSOR";
switch (computer) {
case 1:
playerTwoChoice = "ROCK";
computerWins = computerWins + 1;
gameResult = "lost.";
return;
case 2:
playerTwoChoice = "PAPER";
playerOneWins += 1;
gameResult = "won!";
return;
case 3:
playerTwoChoice = "SCISSOR";
gameResult = "tied.";
return;
default:
break;
}
default:
break;
}
}
evaluateWinner(formattedPlayerOne, computerRounded);
alert("You picked " + playerOneChoice + ". Computer picked " + playerTwoChoice + ". You " + gameResult + "\n" + "You: " + playerOneWins + "\n" + "Computer: " + computerWins);
playAgain = confirm("Do you want to play again?");
} while (playAgain === true);
if(playAgain == false){
if(playerOneWins > computerWins){
alert("Player one is the grand winner!");
}
else if(computerWins > playerOneWins){
alert("The computer is the grand winner!");
} else {
alert("The game is a tie.");
}
document.getElementById("header").innerHTML="Results!";
document.getElementById("playerOne").innerHTML = "Player One has " + playerOneWins + " wins!";
document.getElementById("computer").innerHTML = "Computer has " + computerWins + " wins!";
if(playerOneWins > computerWins){
document.getElementById("finalWinner").innerHTML="The grand winner is Player one!";
}
else if(computerWins > playerOneWins){
document.getElementById("finalWinner").innerHTML="The grand winner is the computer!";
} else {
document.getElementById("finalWinner").innerHTML="The game is a tie - there is no grand winner.";
}
}