-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathframe.js
33 lines (30 loc) · 854 Bytes
/
frame.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
class Frame {
constructor() {
this.rolls = [];
this.regularPoints = 0;
this.bonusPoints = 0;
};
roll(points) {
if (this.rolls.length == 2 || this.rolls[0] == 10) {
throw new Error('Tried to add points to a frame that is already over');
} else if ((this.regularPoints + points) > 10) {
throw new Error(`Tried to add roll that would exceed max score in a frame (${this.regularPoints} + ${points})`);
} else {
this.rolls.push(points);
this.regularPoints += points;
};
};
// is this function necessary? This logic could be in Game
playFrame(points_array) {
points_array.forEach((points) => {
this.roll(points);
});
}
isStrike() {
return this.rolls[0] == 10;
}
isSpare() {
return this.regularPoints == 10 && this.rolls.length == 2;
}
}
module.exports = Frame;