-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathscoreCalculator.js
70 lines (57 loc) · 1.75 KB
/
scoreCalculator.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
class ScoreCalculator {
static calculate_score(frames) {
let score = 0;
let frame_index = 0;
let consecutive_strikes = 0;
for (let frame_number = 0; frame_number < 10; frame_number++) {
const [roll1, roll2, roll3] = frames[frame_index] || [0, 0, 0];
if (frame_number === 9) {
score += roll1 + roll2;
if (roll1 === 10 || roll1 + roll2 === 10) {
const roll3 = frames[frame_index][2] || 0;
score += roll3;
}
} else if (roll1 === 10) {
score += 10 + ScoreCalculator.strike_bonus(frame_index, frames);
consecutive_strikes += 1;
frame_index += 1;
} else if (roll1 + roll2 === 10) {
score += 10 + ScoreCalculator.spare_bonus(frame_index, frames);
consecutive_strikes = 0;
frame_index += 1;
} else {
score += roll1 + roll2;
consecutive_strikes = 0;
frame_index += 1;
}
if (consecutive_strikes >= 2) {
score += roll1;
}
}
return score;
}
static strike_bonus(frame_index, frames) {
const next_frame = frames[frame_index + 1] || [0, 0, 0];
const next_next_frame = frames[frame_index + 2] || [0, 0, 0];
const [next_roll1, next_roll2] = next_frame;
if (next_roll1 === 10) {
if (next_next_frame[0] === 10) {
return next_next_frame[0];
} else {
return 10 + next_next_frame[0] + next_next_frame[1];
}
} else {
return next_roll1 + next_roll2;
}
}
static spare_bonus(frame_index, frames) {
const next_frame = frames[frame_index + 1] || [0, 0, 0];
const [next_roll1] = next_frame;
if (next_roll1 === 10) {
return 10;
} else {
return next_roll1;
}
}
}
module.exports = ScoreCalculator;