forked from makersacademy/bowling-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoresheet.js
71 lines (64 loc) · 1.66 KB
/
scoresheet.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
class Scoresheet {
constructor() {
this.frames = [];
}
addFrame(frame) {
this.frames.push(frame);
}
newFrame(frame) {
this.currentFrame = frame;
}
frameScore(frameNum) {
let score = 0;
score += this.frames[frameNum].firstRoll();
if (this.frames[frameNum].isStrike() === true) {
score += this.#strikeBonus(frameNum);
} else if (this.frames[frameNum].isSpare() === true) {
score += this.frames[frameNum].secondRoll();
score += this.#spareBonus(frameNum);
} else {
score += this.frames[frameNum].secondRoll();
}
return score;
}
totalScore() {
let score = 0;
for (let i = 0; i < 10; i++) {
score += this.frameScore(i);
}
return score;
}
#strikeBonus(frameNum) {
let bonus = 0;
if (frameNum === 9) {
bonus +=
this.frames[frameNum].secondRoll() + this.frames[frameNum].bonusRoll();
} else if (
frameNum === 8 &&
this.frames[frameNum + 1].isStrike() === true
) {
bonus +=
this.frames[frameNum + 1].firstRoll() +
this.frames[frameNum + 1].secondRoll();
} else if (this.frames[frameNum + 1].isStrike() === true) {
bonus +=
this.frames[frameNum + 1].firstRoll() +
this.frames[frameNum + 2].firstRoll();
} else {
bonus +=
this.frames[frameNum + 1].firstRoll() +
this.frames[frameNum + 1].secondRoll();
}
return bonus;
}
#spareBonus(frameNum) {
let bonus = 0;
if (frameNum === 9) {
bonus += this.frames[frameNum].bonusRoll();
} else {
bonus += this.frames[frameNum + 1].firstRoll();
}
return bonus;
}
}
module.exports = Scoresheet;