-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
154 lines (130 loc) · 4.67 KB
/
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
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
/*ROCK PAPER SCISSOR GAME
*
* Objective: A game of rock paper scissors played over 5 rounds.
* A single user plays against the "computer".
* Whoever has the points points after 5 rounds wins.
*
* Pseudocode:
* The computer is the first to make its choice which is stored until the user responds.
* As with the traditional game,
* choosing the same move is a tie,
* rock wins over scissor,
* scissor wins over paper,
* paper wins over rock.
*
* The result of the round is announced to the user through an output message and also
* stored and the next round begins until all 5 rounds are passed.
*
* After all 5 rounds, the winner is announced or a tie is declared based on #points.
*/
function getComputerChoice() {
const choice = Math.floor(Math.random() * 3);
//console.log(choice)
if (choice === 0) {
return "rock";
}
else if (choice === 1) {
return "paper";
}
else {
return "scissor";
}
}
function playRound(playerSelection, computerSelection) {
//console.log("computer choice: " + computerSelection);
//console.log("player choice: " + playerSelection);
if (computerSelection === playerSelection) {
return "tie";
}
else if ((computerSelection === "scissor" && playerSelection === "paper")) {
return "cWin";
}
else if (computerSelection === "paper" && playerSelection === "rock") {
return "cWin";
}
else if (computerSelection === "rock" && playerSelection === "scissor") {
return "cWin";
}
else if (playerSelection === "rock" && computerSelection === "scissor") {
return "pWin";
}
else if (playerSelection === "paper" && computerSelection === "rock") {
return "pWin";
}
else if (playerSelection === "scissor" && computerSelection === "paper") {
return "pWin";
}
}
function endGame(winner) {
statusBar.style.display = 'none';
messageBox.style.display = 'none';
document.querySelector('#buttons').style.display = 'none';
if (winner === "p") {
heading.textContent = 'You Win!'
finalMessage.textContent = `
Congratulations on making it to 5 points before the computer!
It was a a tough game but you've proven yourself to be even tougher!
Thanks for playing!`
}
else {
heading.textContent = 'You Lose...'
finalMessage.textContent = `
You fought long and hard but it simply was not meant to be.
Come again if you dare...`
}
heading.style.display = 'block';
finalMessage.style.display = 'block';
playAgain.style.display = 'block';
}
function startGame() {
heading.style.display = 'none';
statusBar.style.display = 'flex';
messageBox.style.display = 'flex';
}
let cWins = 0;
let pWins = 0;
let rounds = 0;
let gameStarted = false;
//HTML ELEMENTS
const statusBar = document.querySelector('#status-bar');
const heading = document.querySelector('#heading');
const buttons = document.querySelectorAll('button');
const numRounds = document.querySelector('#num-rounds');
const userPoints = document.querySelector('#user-points');
const computerPoints = document.querySelector('#computer-points');
const messageBox = document.querySelector('#message-box');
const finalMessage = document.querySelector('#final-message');
const playAgain = document.querySelector("#play-again-button");
buttons.forEach(button => {
button.addEventListener('click', (event) => {
if (!gameStarted) {
gameStarted = true;
startGame();
}
const computerSelection = getComputerChoice();
const playerSelection = event.target.id;
const roundResult = playRound(playerSelection, computerSelection);
numRounds.textContent = ++rounds;
if (roundResult === "cWin") {
cWins++;
computerPoints.textContent = cWins;
messageBox.textContent = `${computerSelection} beats ${playerSelection}. You lose the round!`;
}
else if (roundResult === 'pWin') {
pWins++;
userPoints.textContent = pWins;
messageBox.textContent = `
${playerSelection} beats ${computerSelection}. You win the round!`;
}
else {
messageBox.textContent = `${playerSelection} against ${computerSelection}. It's a tie!`
}
if (cWins == 5 || pWins == 5) {
endGame(cWins === 5 ? "c" : "p");
//console.log(winner);
// const endHeading = document.querySelector("#heading-end");
// endHeading = winner == "p" ? "You Win!" : "You Lost...";
}
});
});
playAgain.addEventListener('click', () => {window.location.reload()});