From d5180f9178a430ddf2dcdd0524e3e349c628fd57 Mon Sep 17 00:00:00 2001 From: Sam Blausten Date: Sun, 30 Jul 2017 13:09:28 +0100 Subject: [PATCH] Added end game implementation --- spec/features/playSpec.js | 29 +++++++++++++++++++++++++++++ src/Game.js | 1 + 2 files changed, 30 insertions(+) diff --git a/spec/features/playSpec.js b/spec/features/playSpec.js index 35dcdc6fd7..6d573296bd 100644 --- a/spec/features/playSpec.js +++ b/spec/features/playSpec.js @@ -32,4 +32,33 @@ describe('Play', function () { expect(scorecard.getCard().length).toEqual(10); expect(game.getTotalScore()).toEqual(0); }); + it('ends after 10 frames if no strike on final frame', function () { + var game = new Game(); + console.log = jasmine.createSpy('log'); + spyOn(Math, 'random').and.returnValue(0); + for (var i = 0; i < 21; i++) { + game.play(); + } + expect(console.log).toHaveBeenCalledWith('Game over!'); + }); + // it('ends after 11 frames if strike on 10th frame', function () { + // var game, scorecard; + // game = new Game(); + // + // console.log = jasmine.createSpy('log'); + // spyOn(Math, 'random').and.returnValue(0); + // for (var i = 0; i < 18; i++) { + // game.play(); + // } + // Math.random.calls.reset(); + // spyOn(Math, 'random').and.returnValues(10, 0); + // game.play(); + // game.play(); + // Math.random.calls.reset(); + // spyOn(Math, 'random').and.returnValue(0); + // for (var i = 0; i < 3; i++) { + // game.play(); + // } + // expect(console.log).toHaveBeenCalledWith('Game over!'); + // }); }); diff --git a/src/Game.js b/src/Game.js index abab5b4435..fd417550d0 100644 --- a/src/Game.js +++ b/src/Game.js @@ -9,6 +9,7 @@ function Game () { Game.prototype = { play: function () { + if (this.checkGameEnd()) { console.log('Game over!'); return; } this.getCurrentFrame().processRoll(); this.nextFrame(); },