-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
233 lines (201 loc) · 6.91 KB
/
app.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const uiElements = {
buttons: {
buttonRock: document.getElementById('buttonRock'),
buttonPaper: document.getElementById('buttonPaper'),
buttonScissor: document.getElementById('buttonScissor'),
},
}
const gameResult = {
playerPoints: 0,
computerPoints: 0,
}
/**
* Main Game function, is called every time one of the players buttons is clicked.
* @param {object} e - the click event object
*/
const playGame = function (e) {
playGameRound(e)
refreshGamescore(gameResult)
announceTheWinner(gameResult)
}
Object.values(uiElements.buttons).forEach((button) => {
button.addEventListener('click', playGame, false)
})
/**
* Computer play
* @returns {string} a random choice from: rock, paper or scissor
*/
const computerPlay = function () {
const choices = ['rock', 'paper', 'scissor']
const randomChoice = getRandomInt(choices.length)
return choices[randomChoice]
}
/**
* Get a random integer
* @param {int} max the highest value to get back
* @returns {int} a random integer from 0 to max
*/
const getRandomInt = function (max) {
return Math.floor(Math.random() * max)
}
/**
* Create and attach a game status message balloon with computer or player selection
* @param {boolean} messageOrientationToRight
* @param {string} messageTextContent
* @param {string} messageMarkupContent
*/
const createAndAttachGameStatusMessageBalloon = function (
messageOrientationToRight = false,
messageTextContent = '',
messageMarkupContent
) {
const message = document.createDocumentFragment()
const messageSection = document.createElement('section')
const playerIcon = document.createElement('i')
const messageOrientationClass = messageOrientationToRight === true ? '-right' : '-left'
const messageBalloon = document.createElement('div')
const messageAdditionalContent = document.createElement('div')
const messageText = document.createElement('p')
const messageList = document.querySelector('.message-list')
messageSection.classList.add('message', messageOrientationClass)
messageText.textContent = messageTextContent
messageBalloon.classList.add('nes-balloon', 'from' + messageOrientationClass)
messageBalloon.appendChild(messageText)
if (messageMarkupContent) {
messageAdditionalContent.innerHTML = messageMarkupContent
messageBalloon.appendChild(messageAdditionalContent)
}
messageSection.appendChild(messageBalloon)
message.appendChild(messageSection)
if (messageOrientationToRight) {
playerIcon.classList.add('nes-bcrikko')
message.querySelector('.nes-balloon').after(playerIcon)
} else {
playerIcon.classList.add('nes-bcrikko')
message.querySelector('.nes-balloon').before(playerIcon)
}
messageList.appendChild(message)
messageList.scrollTop = messageList.scrollHeight
}
/**
* Plays a single round of Rock Paper Scissors and attaches messages with selections to game status message list
*
* @param {string} playerSelection the players choice
* @param {string} computerSelection the randomly selected computers choice
* @returns {object} Returns an object with the game round result
*/
const calculateAndAttachGameRoundResult = function (playerSelection, computerSelection) {
const roundResult = {
computerPoint: 0,
playerPoint: 0,
}
playerSelection = playerSelection ? playerSelection.toLowerCase() : ''
computerSelection = computerSelection.toLowerCase()
const playerWonRound =
(playerSelection === 'rock' && computerSelection === 'scissor') ||
(playerSelection === 'paper' && computerSelection === 'rock') ||
(playerSelection === 'scissor' && computerSelection === 'paper')
const tie = playerSelection === computerSelection
const computerWon = !playerWonRound && !tie
if (computerWon) {
roundResult.computerPoint = 1
roundResult.playerPoint = 0
} else if (playerWonRound) {
roundResult.computerPoint = 0
roundResult.playerPoint = 1
} else if (tie) {
roundResult.computerPoint = 0
roundResult.playerPoint = 0
}
return roundResult
}
/**
* Checks the game result for winner
* @param {object} gameResult gets the players and computers game score
* @returns {string} winner
*/
const getWinner = function (gameResult) {
if (gameResult.playerPoints > gameResult.computerPoints) {
winner = 'Player'
} else if (gameResult.playerPoints < gameResult.computerPoints) {
winner = 'Computer'
} else {
winner = 'Nobody'
}
return winner
}
/**
* Check if someone won the game
* @param {object} gameRoundResult
* @return {boolean} Returns true if one player has reached the min. points to win.
*/
const checkGameForEnd = function (gameResult) {
const pointsToWin = 5
return gameResult.playerPoints == pointsToWin || gameResult.computerPoints == pointsToWin
}
/**
* Refreshes the game score
* @param {object} gameResult
*/
const refreshGamescore = function (gameResult) {
const computerScoreElement = document.querySelector('.score__computer')
const playerScoreElement = document.querySelector('.score__player')
computerScoreElement.textContent = gameResult.computerPoints
playerScoreElement.textContent = gameResult.playerPoints
}
/**
* Play one game round
* @param {object} event
*/
const playGameRound = function (event) {
const computerSelection = computerPlay()
const playerSelection = event.target.value
const gameRoundResult = calculateAndAttachGameRoundResult(playerSelection, computerSelection)
let messageDelay = 500
let countdown = 5
gameResult.playerPoints += gameRoundResult.playerPoint
gameResult.computerPoints += gameRoundResult.computerPoint
Array.from(event.target.parentElement.parentElement.children).forEach((child) =>
child.classList.remove('selected')
)
event.target.parentElement.classList.add('selected')
createAndAttachGameStatusMessageBalloon(true, 'You played: ' + playerSelection)
createAndAttachGameStatusMessageBalloon(false, 'I played: ' + computerSelection)
}
/**
* Check if the game reached the end condition and announces the winner. Then asks for a new game.
* @param {object} gameResult
*/
const announceTheWinner = function (gameResult) {
if (checkGameForEnd(gameResult)) {
const winner = getWinner(gameResult)
if (winner === 'Computer') {
createAndAttachGameStatusMessageBalloon(
false,
`I have won the game!`,
'<i class="nes-icon trophy is-large"></i>'
)
}
if (winner === 'Player') {
createAndAttachGameStatusMessageBalloon(
true,
`You have won the game!`,
'<i class="nes-icon trophy is-large"></i>'
)
}
createAndAttachGameStatusMessageBalloon(
false,
`Do you want to play a new game?`,
'<button type="button" class="nes-btn is-success" onclick="restartGame()">Yes please!</button>'
)
Object.values(uiElements.buttons).forEach((button) => {
button.removeEventListener('click', playGame, false)
})
}
}
/**
* Reloads the browser window and restarts the game
*/
const restartGame = function () {
window.location.reload()
}