Last active
December 19, 2015 05:29
-
-
Save stenehall/5904644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Game = function Game() { | |
this.marks = []; | |
}; | |
Game.prototype.add_mark = function add_mark(pins) { | |
this.marks.push(pins); | |
}; | |
Game.prototype.score = function score() { | |
var score = 0; | |
marks = this.marks.slice(0); // Lets clone to not mess it up. | |
while (marks.length > 0) | |
{ | |
score += this._score_frame(marks); | |
if(score === 300) break; // Nasty. | |
} | |
console.log(score); | |
} | |
Game.prototype._score_frame = function _score_frame(marks) { | |
var current_points = marks.shift(); | |
if (current_points < 10) | |
{ | |
if(marks.length > 0) { | |
current_points += marks.shift(); | |
if (current_points === 10) | |
{ | |
if(marks.length > 0) current_points += marks[0]; | |
} | |
} | |
} else { | |
if (marks.length >= 1) current_points += marks[0]; | |
if (marks.length >= 2) current_points += marks[1]; | |
} | |
return current_points; | |
} | |
// example usage | |
var game = new Game(); | |
game.add_mark(5); | |
game.score(); //=> 5 | |
game.add_mark(5); | |
game.score(); //=> 10 | |
game.add_mark(3); | |
game.score(); //=> 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment