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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
document.addEventListener("DOMContentLoaded", function() {
var graph = document.getElementById("graph")
var stats = document.getElementById("stats")
var scoreboard = document.getElementById("scoreboard")
function generateEmptyGame() {
var frames = []
for (var i = 0; i < 9; i++) {
frames.push({
throws: [null, null],
score: null,
})
}
frames.push({
throws: [null, null, null],
score: null,
})
return {
frames: frames,
currentFrame: 0,
isComplete: false,
}
}
function scoreGame(game) {
var thisFrameScore
for (var i = 0; i < 10; i++) {
if (game.frames[i].throws[0] === null) {
break
}
if (i === 9) {
var firstThrow = game.frames[i].throws[0] || 0
var secondThrow = game.frames[i].throws[1] || 0
var thirdThrow = game.frames[i].throws[2] || 0
thisFrameScore = firstThrow + secondThrow + thirdThrow
} else {
// Fetch the next two throws in case we get a mark
var firstBonusThrow = game.frames[i + 1].throws[0] || 0
var secondBonusThrow
if (firstBonusThrow === 10) {
// If we're in the 9th frame, pick the second throw of
// the next frame as the 10th frame can have multiple
// strikes
if (i === 8) {
secondBonusThrow = game.frames[i + 1].throws[1] || 0
} else {
secondBonusThrow = game.frames[i + 2].throws[0] || 0
}
} else {
secondBonusThrow = game.frames[i + 1].throws[1] || 0
}
var firstThrow = game.frames[i].throws[0] || 0
var secondThrow = game.frames[i].throws[1] || 0
if (firstThrow === 10) {
thisFrameScore = 10 + firstBonusThrow + secondBonusThrow
} else if (firstThrow + secondThrow === 10) {
thisFrameScore = 10 + firstBonusThrow
} else {
thisFrameScore = firstThrow + secondThrow
}
}
if (i === 0) {
game.frames[i].score = thisFrameScore
} else {
game.frames[i].score = game.frames[i - 1].score + thisFrameScore
}
}
}
function generateThrow(game) {
var isNewFrame = game.frames[game.currentFrame].throws[0] === null
if (isNewFrame) {
var firstThrow = Math.floor(Math.random() * 11)
game.frames[game.currentFrame].throws[0] = firstThrow
if (firstThrow === 10 && game.currentFrame !== 9) {
game.currentFrame++
}
} else {
if (game.currentFrame !== 9) {
var lastThrow = game.frames[game.currentFrame].throws[0]
var nextThrow = Math.floor(Math.random() * (10 - lastThrow + 1))
game.frames[game.currentFrame].throws[1] = nextThrow
if (game.currentFrame !== 9) {
game.currentFrame++
}
} else {
var firstThrow = game.frames[game.currentFrame].throws[0]
var secondThrow = game.frames[game.currentFrame].throws[1]
if (firstThrow === 10 && secondThrow === null) {
var secondThrow = Math.floor(Math.random() * 11)
game.frames[game.currentFrame].throws[1] = secondThrow
} else if (secondThrow === null) {
var secondThrow = Math.floor(Math.random() * (10 - firstThrow + 1))
game.frames[game.currentFrame].throws[1] = secondThrow
if (firstThrow + secondThrow !== 10) {
game.isComplete = true
}
} else if (firstThrow === 10 && secondThrow === 10) {
var thirdThrow = Math.floor(Math.random() * 11)
game.frames[game.currentFrame].throws[2] = thirdThrow
game.isComplete = true
} else if (firstThrow === 10) {
var thirdThrow = Math.floor(Math.random() * (10 - secondThrow + 1))
game.frames[game.currentFrame].throws[2] = thirdThrow
game.isComplete = true
} else if (firstThrow + secondThrow === 10) {
var thirdThrow = Math.floor(Math.random() * 11)
game.frames[game.currentFrame].throws[2] = thirdThrow
game.isComplete = true
}
}
}
}
function renderScoreBoard(game) {
var frame
scoreboard.innerHTML = ""
var firstHalf = document.createElement("div")
firstHalf.classList.add("scoreboard-half")
var secondHalf = document.createElement("div")
secondHalf.classList.add("scoreboard-half")
for (var i = 0; i < 5; i++) {
frame = game.frames[i]
firstHalf.appendChild(renderFrame(frame.score, frame.throws, i === 9))
}
var mobileSpacer = document.createElement("div")
mobileSpacer.classList.add("mobile-spacer")
firstHalf.appendChild(mobileSpacer)
for (var i = 5; i < 10; i++) {
frame = game.frames[i]
secondHalf.appendChild(renderFrame(frame.score, frame.throws, i === 9))
}
scoreboard.appendChild(firstHalf)
scoreboard.appendChild(secondHalf)
}
function throwToString(points) {
if (points === null) {
return " "
} else if (points === 10) {
return "X"
} else if (points === 0) {
return "-"
} else {
return points + ""
}
}
// Not the 10th though!
function frameToString(throws) {
if (throws[0] === 10) {
return ["X", ""]
} else if (throws[0] + throws[1] === 10) {
return [throwToString(throws[0]), "/"]
} else {
return throws.map(throwToString)
}
}
function renderFrame(score, throws, isTenth) {
var contents
if (isTenth) {
// 10th frame has weird logic
if (throws === undefined) {
contents = ["", "", ""]
// Two strikes = XX[third throw]
} else if (throws[0] === 10 && throws[1] === 10) {
contents = [
throwToString(10),
throwToString(10),
throwToString(throws[2]),
]
// X[normal frame]
} else if (throws[0] === 10) {
contents = [throwToString(10)].concat(frameToString(throws.slice(1)))
// [normal frame][bonus throw]
} else if (throws[0] + throws[1] === 10) {
contents = frameToString(throws.slice(0, 2)).concat([
throwToString(throws[2]),
])
} else {
contents = frameToString(throws)
}
// Make contents 3 items long... lol
if (contents.length < 3) {
contents.push("")
}
if (contents.length < 3) {
contents.push("")
}
} else {
contents = frameToString(throws)
if (contents.length < 2) {
contents.push("")
}
}
var frame = document.createElement("div")
frame.classList.add("frame")
contents.forEach(function(formattedThrow) {
var square = document.createElement("div")
square.classList.add("square")
square.innerHTML = formattedThrow
frame.appendChild(square)
})
var scoreDiv = document.createElement("div")
scoreDiv.classList.add("score")
scoreDiv.innerHTML = score
frame.appendChild(scoreDiv)
return frame
}
function renderGraph(graphDiv) {
graphDiv.innerHTML = ""
var distribution = []
for (var i = 0; i <= 300; i++) {
distribution[i] = scores[i] || 0
}
var highestPopulation = Math.max.apply(null, distribution)
var bar
for (var i = 0; i <= 300; i++) {
bar = document.createElement("div")
bar.classList.add("bar")
bar.style.height = ((scores[i] || 0) / highestPopulation) * 100 + "%"
graphDiv.appendChild(bar)
}
}
function renderStats(statsDiv) {
var distribution = []
for (var i = 0; i <= 300; i++) {
distribution[i] = scores[i] || 0
}
var numGames = distribution.reduce(function(a, b) {
return a + b
})
var allGames = []
var totalPins = 0
var minScore, maxScore
var mode = 0,
modeIndex
for (var i = 0; i <= 300; i++) {
for (var j = 0; j < distribution[i]; j++) {
allGames.push(i)
totalPins += i
}
if (distribution[i] > 0) {
maxScore = i
if (minScore === undefined) {
minScore = i
}
}
if (distribution[i] > mode) {
mode = distribution[i]
modeIndex = i
}
}
var mean = Math.floor((totalPins / numGames) * 100) / 100
// TODO: Odd/even
var median
if (allGames.length % 2 === 1) {
median = allGames[Math.floor(allGames.length / 2)]
} else {
median =
(allGames[Math.floor(allGames.length / 2)] +
allGames[Math.floor(allGames.length / 2) - 1]) /
2
}
statsDiv.innerHTML =
"<div>" +
[
'<span class="stats-label">Games:</span><span> ' + numGames + "</span>",
'<span class="stats-label">Min:</span><span> ' + minScore + "</span>",
'<span class="stats-label">Max:</span><span> ' + maxScore + "</span>",
'<span class="stats-label">Mean:</span><span> ' + mean + "</span>",
'<span class="stats-label">Median:</span><span> ' + median + "</span>",
'<span class="stats-label">Mode:</span><span> ' +
modeIndex +
" (" +
mode +
" games)</span>",
].join("</div><div>") +
"</div>"
}
var scores = []
var game = generateEmptyGame()
renderScoreBoard(game)
var throwButton = document.getElementById("throw")
var gameButton = document.getElementById("game")
var manyGamesButton = document.getElementById("many-games")
function handleThrowClick(e) {
e.preventDefault()
if (game.isComplete) {
game = generateEmptyGame()
}
generateThrow(game)
scoreGame(game)
renderScoreBoard(game)
if (game.isComplete) {
var score = game.frames[9].score
scores[score] = (scores[score] || 0) + 1
renderGraph(graph)
renderStats(stats)
}
}
function simulateGame() {
game = generateEmptyGame()
while (!game.isComplete) {
generateThrow(game)
}
scoreGame(game)
renderScoreBoard(game)
var score = game.frames[9].score
scores[score] = (scores[score] || 0) + 1
renderGraph(graph)
}
function handleGameClick(e) {
e.preventDefault()
simulateGame()
renderStats(stats)
}
function handleManyClick(e) {
e.preventDefault()
var simulations = 0
;(function run() {
simulations++
simulateGame()
renderStats(stats)
if (simulations < 100) {
setTimeout(run, 5)
}
})()
}
throwButton.addEventListener("click", handleThrowClick)
throwButton.addEventListener("touchend", handleThrowClick)
gameButton.addEventListener("click", handleGameClick)
gameButton.addEventListener("touchend", handleGameClick)
manyGamesButton.addEventListener("click", handleManyClick)
manyGamesButton.addEventListener("touchend", handleManyClick)
})
|