Skip to content

Commit

Permalink
Bowling: starts a new frame when the current frame is over
Browse files Browse the repository at this point in the history
  • Loading branch information
milesillsley committed May 20, 2017
1 parent e4e0ff2 commit 06546ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions spec/BowlingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@
game.throwBall(1);
expect(game.getCurrentFrame().getFirstScore()).toBe(1);
});
it('scores a frame when two balls are thrown', function() {
game.newFrame();
game.throwBall(1);
game.throwBall(2);
expect(game.getCurrentFrame().getSecondScore()).toBe(2);
});
it('starts a new frame when the current frame is over', function() {
game.newFrame();
game.throwBall(1);
game.throwBall(2);
game.throwBall(3);
expect(game.getFrames().length).toBe(2);
expect(game.getCurrentFrame().getFirstScore()).toBe(3);
});
});
}());
5 changes: 5 additions & 0 deletions spec/FrameSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
expect(frame.getFirstScore()).toBe(5);
expect(frame.getSecondScore()).toBe(4);
});
it('is over after scoring two throws', function() {
frame.score(5);
frame.score(4);
expect(frame.isOver()).toBe(true);
});
});
}());
11 changes: 9 additions & 2 deletions src/Bowling.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ var Bowling = function() {
};
Bowling.prototype.pinsLeft = function() { return this._pinsLeft; };
Bowling.prototype.throwBall = function(pinsHit) {
this._pinsLeft -= pinsHit;
this.getCurrentFrame().score(pinsHit);
if (this.getCurrentFrame().isOver()) {
this.newFrame();
this._currentFrame += 1;
this._pinsLeft -= pinsHit;
this.getCurrentFrame().score(pinsHit);
} else {
this._pinsLeft -= pinsHit;
this.getCurrentFrame().score(pinsHit);
}
};
Bowling.prototype.newFrame = function() { this._frames.push(new Frame()); };
Bowling.prototype.getFrames = function() { return this._frames; };
Expand Down
1 change: 1 addition & 0 deletions src/Frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ var Frame = function() {
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]; };
Frame.prototype.isOver = function() { return this._score.length === 2; };

0 comments on commit 06546ae

Please sign in to comment.