-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
169 lines (147 loc) · 4.68 KB
/
index.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang ="en">
<head>
<meta charset = "utf-8"/>
</head>
<body>
<div id="container">
<button id="rock">Rock</button>
<button id="paper">Paper</button>
<button id="scissors">Scissors</button>
</div>
</body>
<script>
var round = 0;
var playerScore = 0;
var computerScore = 0;
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click' , (e) => {
var computerSelection = getComputerSelection();
var playerSelection = button.id;
var roundOutcome = playRound(playerSelection, computerSelection);
const content = document.createElement('div');
content.classList.add('content');
content.textContent = `You threw ${playerSelection}. ` + `Computer threw ${computerSelection}. ` + roundOutcome.text;
container.appendChild(content);
roundUp();
});
});
function roundUp() {
if((round > 3) && (playerScore > computerScore)) {
alert(`Good game! Player scored ${playerScore}. Computer scored ${computerScore}. Player wins!`);
removeContent('content');
} else if((round > 3) && (playerScore < computerScore)) {
alert(`Good game! Player scored ${playerScore}. Computer scored ${computerScore}. Computer wins!`);
removeContent('content');
} else if((round > 3) && (playerScore === computerScore)) {
alert(`Good game! Player scored ${playerScore}. Computer scored ${computerScore}. It's a tie!`);
removeContent('content');
} else {
round += 1;
}
console.log(round);
}
function removeContent(className){
var elements = document.getElementsByClassName(className);
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
round = 0;
playerScore = 0;
computerScore = 0;
}
// Player selects paper, rock, or scissors
const getPlayerSelection = playerInput => {
playerInput = playerInput.toLowerCase();
if (
playerInput === "rock" ||
playerInput === "paper" ||
playerInput === "scissors"
) {
return playerInput;
} else {
alert("Not a valid input. Please select rock, paper, or scissors.");
}
};
//computer randomly selects paper, rock, or scissors
function getComputerSelection() {
switch (Math.floor(Math.random() * 3)) {
case 0:
return "rock";
break;
case 1:
return "paper";
break;
case 2:
return "scissors";
break;
default:
alert("The computer is broken! AHHH!");
}
}
//take playerSelection and computerSelection to determine round winner.
function playRound(playerSelection, computerSelection) {
if (computerSelection === playerSelection) {
return {
text: "This match was a tie."
}
} else if (playerSelection === "rock") {
if (computerSelection === "paper") {
return {
text: "Paper beats rock. Computer Wins!",
score: computerScore += 1,
};
} else {
return {
text: "Rock beats scissors. Player wins!",
score: playerScore += 1,
};
}
} else if (playerSelection === "paper") {
if (computerSelection === "scissors") {
return {
text: "Scissors beats paper. Computer Wins!",
score: computerScore += 1,
};
} else {
return {
text: "Paper beats rock. Player wins!",
score: playerScore += 1,
};
}
} else if (playerSelection === "scissors") {
if (computerSelection === "rock") {
return {
text: "Rock beats scissors. Computer Wins!",
score: computerScore += 1,
};
} else {
return {
text: "Scissors beat paper. Player wins!",
score: playerScore += 1,
};
}
} else {
alert("playRound function failed!");
}
}
//call playGame to run game inside the console.
function playGame() {
var playerSelection = getPlayerSelection(prompt("YOU READY FOR THIS? ROCK, PAPER, SCISSORS?!"));
var computerSelection = getComputerSelection();
var roundOutcome = playRound(playerSelection, computerSelection);
console.log(`You threw ${playerSelection}. ` + `Computer threw ${computerSelection}. ` + roundOutcome.text);
}
// //loops the game 5 times with different computer selections.
// var i = 0;
// for (i; i < 5; i++) {
// var computerSelection = getComputerSelection();
// var roundOutcome = playRound(playerSelection, computerSelection);
//
// //display the outcome in the console.
// console.log(`You threw ${playerSelection}. ` + `Computer threw ${computerSelection}. ` + roundOutcome);
// }
// }
</script>
</html>