Skip to content

Commit

Permalink
Bowling: scores a frame when the ball is thrown, add current frame fu…
Browse files Browse the repository at this point in the history
…nctionality
  • Loading branch information
milesillsley committed May 20, 2017
1 parent 1da5754 commit e4e0ff2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!-- include spec files here... -->
<script src="spec/BowlingSpec.js"></script>
<script src="spec/FrameSpec.js"></script>

</head>

Expand Down
6 changes: 6 additions & 0 deletions spec/BowlingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
expect(game.pinsLeft()).toBe(10);
});
it('can knock down a pin with a bowling ball', function() {
game.newFrame();
game.throwBall(1);
expect(game.pinsLeft()).toBe(9);
});
it('starts a new frame', function() {
game.newFrame();
expect(game.getFrames()).toContain(new Frame());
});
it('scores a frame when the ball is thrown', function() {
game.newFrame();
game.throwBall(1);
expect(game.getCurrentFrame().getFirstScore()).toBe(1);
});
});
}());
18 changes: 18 additions & 0 deletions spec/FrameSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function() {
'use strict';

describe('Frame', function() {

var frame;

beforeEach(function() {
frame = new Frame();
});
it('can score two throws', function() {
frame.score(5);
frame.score(4);
expect(frame.getFirstScore()).toBe(5);
expect(frame.getSecondScore()).toBe(4);
});
});
}());
9 changes: 8 additions & 1 deletion src/Bowling.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
var Bowling = function() {
this._pinsLeft = 10;
this._frames = [];
this._currentFrame = 0;
};
Bowling.prototype.pinsLeft = function() { return this._pinsLeft; };
Bowling.prototype.throwBall = function(pinsHit) { this._pinsLeft -= pinsHit; };
Bowling.prototype.throwBall = function(pinsHit) {
this._pinsLeft -= pinsHit;
this.getCurrentFrame().score(pinsHit);
};
Bowling.prototype.newFrame = function() { this._frames.push(new Frame()); };
Bowling.prototype.getFrames = function() { return this._frames; };
Bowling.prototype.getCurrentFrame = function() {
return this._frames[this._currentFrame];
};
7 changes: 6 additions & 1 deletion src/Frame.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
var Frame = function() {};
var Frame = function() {
this._score = [];
};
Frame.prototype.score = function(pinsHit) { this._score.push(pinsHit); };
Frame.prototype.getFirstScore = function() { return this._score[0]; };
Frame.prototype.getSecondScore = function() { return this._score[1]; };

0 comments on commit e4e0ff2

Please sign in to comment.