-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (137 loc) · 4.84 KB
/
script.js
File metadata and controls
165 lines (137 loc) · 4.84 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
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
const newGameButton = document.querySelector('#newGame');
const restartGameButton = document.querySelector('#restartGame');
// Keep buttons disabled until the game starts
restartGameButton.disabled = true;
const buttonContainer = document.querySelector('#choiceSelection');
const buttons = document.querySelectorAll('.choice');
toggleButtons();
const statusBox = document.querySelector('#status');
const resultBox = document.querySelector('#moveResult');
const playerScoreElement = document.querySelector('#playerScore');
const computerScoreElement = document.querySelector('#computerScore');
let playerScore = 0, computerScore = 0, roundsPlayed = 0, gameStarted = false;
newGameButton.addEventListener('click', () => {
newGameButton.disabled = true;
restartGameButton.disabled = false;
startGame();
toggleButtons();
});
restartGameButton.addEventListener('click', () => {
startGame();
});
// Play multiple rounds of the game
buttonContainer.addEventListener('click', (e) => {
if (!gameStarted) {
return;
}
const target = e.target;
const playerChoice = target.name;
const computerChoice = getComputerChoice();
const roundResult = getWinner(playerChoice, computerChoice);
updateImage(false, playerChoice, computerChoice);
if (roundResult === 'win') {
statusBox.textContent = 'You won the round!';
resultBox.textContent = `${playerChoice} beats ${computerChoice}!`;
playerScoreElement.textContent = ++playerScore;
} else if (roundResult === 'loss') {
statusBox.textContent = 'You lost the round!';
resultBox.textContent = `${playerChoice} was beaten by ${computerChoice}!`;
computerScoreElement.textContent = ++computerScore;
} else {
statusBox.textContent = 'You tied!';
resultBox.textContent = `${playerChoice} can\'t beat ${computerChoice}!`;
}
roundsPlayed++;
// Check for winner
if (playerScore === 5 || computerScore === 5) {
newGameButton.disabled = false;
restartGameButton.disabled = true;
toggleButtons();
let result;
if (playerScore === 5) {
statusBox.textContent = 'You won the game!';
result = 'won';
} else {
statusBox.textContent = 'You lost the game';
result = 'lost';
}
resultBox.textContent = `You ${result} by ${Math.abs(computerScore - playerScore)} points!`;
}
});
function startGame() {
playerScore = 0;
computerScore = 0;
roundsPlayed = 0;
gameStarted = true;
statusBox.textContent = 'Choose Your Weapon';
resultBox.textContent = 'First to 5 points wins!';
// Reset the images
updateImage(true);
// Set UI counters to 0
let scores = document.querySelectorAll('#playerScore, #computerScore');
scores.forEach(score => score.textContent = 0);
}
function toggleButtons() {
buttons.forEach(element => {
element.disabled = element.disabled ? false : true;
});
}
// Get the computer's choice
function getComputerChoice() {
let choiceNum = Math.floor(Math.random() * 3);
if (choiceNum === 0) {
return 'Rock';
} else if (choiceNum === 1) {
return 'Paper';
} else {
return 'Scissors';
}
}
// Get the winner of a single round of rock-paper-scissors
function getWinner(playerChoice, computerChoice) {
if (playerChoice === 'Rock') {
return (computerChoice === 'Rock') ? 'tie'
: (computerChoice === 'Paper') ? 'loss'
: 'win';
} else if (playerChoice === 'Paper') {
return (computerChoice === 'Rock') ? 'win'
: (computerChoice === 'Paper') ? 'tie'
: 'loss';
} else {
return (computerChoice === 'Rock') ? 'loss'
: (computerChoice === 'Paper') ? 'win'
: 'tie';
}
}
// Changes image to show last moves by player and computer
function updateImage(reset=false, playerChoice, computerChoice) {
if (reset === true) {
const imgDivs = document.querySelectorAll('.last-moves .move-box');
imgDivs.forEach(imgDiv => {
const img = imgDiv.querySelector('img');
img.src = './images/unknown.png';
img.alt = 'No Moves Made';
});
}
for (let i = 0; i < 2; i++) {
let choice;
const img = document.querySelector('.last-moves .move-box:nth-child(' + parseInt(i + 1) + ')').querySelector('img');
if (i === 0) {
choice = playerChoice;
} else {
choice = computerChoice;
}
img.alt = choice;
switch(choice) {
case 'Rock':
img.src = './images/rock.png';
break;
case 'Paper':
img.src = './images/paper.png';
break;
case 'Scissors':
img.src = './images/scissors.png';
break;
}
}
}