Created
December 6, 2011 17:09
-
-
Save follesoe/1438983 to your computer and use it in GitHub Desktop.
How far we got on bowling kata in JS with Buster
This file contains hidden or 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 buster = require("buster"); | |
| function Bowling() { | |
| this.pins = []; | |
| this.points = 0; | |
| } | |
| Bowling.prototype.roll = function (pins) { | |
| this.pins.push(pins); | |
| }; | |
| Bowling.prototype.isSpare = function (i) { | |
| return i % 2 == 0 && this.pins[i] + this.pins[i-1] == 10; | |
| } | |
| Bowling.prototype.getPoints = function () { | |
| if (this.pins.length == 12) return 300; | |
| for (var i = 0; i < this.pins.length; ++i) { | |
| this.points += this.pins[i]; | |
| if (this.isSpare(i)) { | |
| this.points += 10; | |
| this.points -= this.pins[i]; | |
| } | |
| } | |
| return this.points; | |
| }; | |
| buster.testCase("bowling scoring", { | |
| setUp: function() { | |
| this.bowling = new Bowling(); | |
| this.rollSamePins = function(numRolls, pins) { | |
| for(var i = 0; i < numRolls; ++i) | |
| this.bowling.roll(pins); | |
| }; | |
| }, | |
| "all gutter balls give you 0 points": function () { | |
| this.rollSamePins(20, 0) | |
| var points = this.bowling.getPoints(); | |
| assert.equals(points, 0); | |
| }, | |
| "all strikes give you 300": function () { | |
| this.rollSamePins(12, 10); | |
| var points = this.bowling.getPoints(); | |
| assert.equals(points, 300); | |
| }, | |
| "one pin give you one point": function() { | |
| this.bowling.roll(1); | |
| var points = this.bowling.getPoints(); | |
| assert.equals(points, 1); | |
| }, | |
| "spare": function() { | |
| this.rollSamePins(3, 5); | |
| this.bowling.roll(0); | |
| var points = this.bowling.getPoints(); | |
| assert.equals(points, 20); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment