Skip to content

Instantly share code, notes, and snippets.

@beaucarnes
Last active December 30, 2017 18:44
Show Gist options
  • Save beaucarnes/5d0f4443b782406b9fc8bf4c55fec6d1 to your computer and use it in GitHub Desktop.
Save beaucarnes/5d0f4443b782406b9fc8bf4c55fec6d1 to your computer and use it in GitHub Desktop.
score bowling
class Game {
constructor() {
this.frames = [];
}
getFrames() {
return this.frames;
}
score() {
let calculatedScore = 0;
let spare = false;
for (var i = 0; i < this.frames.length; i++) {
// Only add second element of array if it exisits - won't exists if first is 10
// Also, don't add second score of 10th frame if first score is a 10
calculatedScore += this.frames[i][0] +
(this.frames[i][1] ?
i == 9 && this.frames[i][0] === 10 ?
0 : this.frames[i][1] : 0)
// calculate spare bonus if previous fream was a spare
if (spare) {
calculatedScore += this.frames[i][0]
spare = false;
}
// calculate spare on final frame
if (i == 9 && this.frames[i][0] !== 10) {
if (this.frames[i][0] + this.frames[i][1] === 10) {
calculatedScore += this.frames[i][2]
}
}
if (this.frames[i].length === 2 && this.frames[i][0] + this.frames[i][1] === 10) {
spare = true;
}
}
// Calculate strike bonus
var framesFlatten = [].concat(...this.frames); // Flatten frames array
for (var i = 0; i < framesFlatten.length; i++) {
if (framesFlatten[i] === 10) {
// Don't add strike bonus if on second strike of last frame
if (!(i === framesFlatten.length - 2 && framesFlatten[i-1] === 10)) {
calculatedScore += (framesFlatten[i+1] || 0) + (framesFlatten[i+2] || 0)
}
}
}
return calculatedScore;
}
roll(pin) {
if (pin < 0 || pin > 10 ) {
console.log("Not valid pin number.");
return;
}
// Tenth frame is different because three rolls are possible
if (this.frames.length === 10) {
if (this.frames[this.frames.length-1].length <= 2) {
// Only allow third score on 10th frame if it starts with a strike or spare
if (this.frames[this.frames.length-1].length == 2 &&
this.frames[this.frames.length-1][0] + this.frames[this.frames.length-1][1] <= 10) {
return;
}
this.frames[this.frames.length-1].push(pin);
return;
} else {
console.log("Game over!")
return;
}
}
// Add two rolls per frame if no strike
if (this.frames.length > 0) {
if (this.frames[this.frames.length-1].length === 1 && this.frames[this.frames.length-1][0] != 10) {
this.frames[this.frames.length-1].push(pin)
} else {
this.frames.push([pin])
}
} else {
this.frames.push([pin])
}
}
}
game = new Game();
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
game.roll(10);
console.log("Test perfect game--");
console.log("expected score: 300, actual score: " + game.score());
console.log(game.getFrames());
game2 = new Game();
game2.roll(10);
game2.roll(7);
game2.roll(3);
game2.roll(9);
game2.roll(0);
game2.roll(10);
game2.roll(0);
game2.roll(8);
game2.roll(8);
game2.roll(2);
game2.roll(0);
game2.roll(6);
game2.roll(10);
game2.roll(10);
game2.roll(10);
game2.roll(8);
game2.roll(1);
console.log("Test average game with spares and strikes--")
console.log("expected score: 167, actual score: " + game2.score());
console.log(game2.getFrames());
game2 = new Game();
game2.roll(3);
game2.roll(7);
game2.roll(3);
game2.roll(3);
game2.roll(3);
game2.roll(3);
console.log("Test partial game with spare--")
console.log("expected score: 25, actual score: " + game2.score());
console.log(game2.getFrames());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment