From 480daf284efac5c73ac1463829427b32a17d8eb5 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Wed, 16 Dec 2020 20:38:39 +0100 Subject: [PATCH] Allow winning or losing --- src/game/Game.test.ts | 49 ++++++++++++++++++++++++++++++++++--------- src/game/Game.ts | 15 ++++++++++++- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/game/Game.test.ts b/src/game/Game.test.ts index 4d470c0..1e2676e 100644 --- a/src/game/Game.test.ts +++ b/src/game/Game.test.ts @@ -1,5 +1,5 @@ import "jest" -import {Game, SelectionMode} from "./Game" +import {EndState, Game, SelectionMode} from "./Game" describe("GameState", () => { const threeByThreeMatrix = [ @@ -8,28 +8,29 @@ describe("GameState", () => { "02", "12", "22", ] + const unrestrictedBuffer = 999 + test("getting size works", () => { - expect((new Game([], [])).size).toEqual(0) - expect((new Game(["00"], [])).size).toEqual(1) + expect((new Game(["00"], [["AA"]], unrestrictedBuffer)).size).toEqual(1) expect((new Game([ "00", "01", "10", "11", - ], [])).size).toEqual(2) + ], [], unrestrictedBuffer)).size).toEqual(2) }); test("getting cell works", () => { - const game = new Game(threeByThreeMatrix, []); + const game = new Game(threeByThreeMatrix, [["AA"]], unrestrictedBuffer); expect(game.getCell(0, 2)).toEqual("02") }); describe("picking", () => { test("starts with free pick", () => { - const game = new Game([], []); + const game = new Game([], [["AA"]], 1); expect(game.state).toEqual({selectionMode: SelectionMode.FreePick}) }); test("picking cells works", () => { - const game = new Game(threeByThreeMatrix, []); + const game = new Game(threeByThreeMatrix, [["AA"]], unrestrictedBuffer); game.pick(0, 0); expect(game.state).toEqual({selectionMode: SelectionMode.RowPick, column: 0}); expect(() => game.pick(0, 2)).toThrow(); @@ -38,7 +39,7 @@ describe("GameState", () => { }); test("picking outside of range fails", () => { - const game = new Game(threeByThreeMatrix, []); + const game = new Game(threeByThreeMatrix, [], unrestrictedBuffer); expect(() => game.pick(-1, 0)).toThrow(); expect(() => game.pick(0, -1)).toThrow(); expect(() => game.pick(3, 0)).toThrow(); @@ -47,7 +48,7 @@ describe("GameState", () => { test("picking fills buffer, fulfills sequence", () => { const simpleSequence = ["00", "10", "20"] - const game = new Game(threeByThreeMatrix, [simpleSequence]); + const game = new Game(threeByThreeMatrix, [simpleSequence], unrestrictedBuffer); expect(game.getSequences()).toEqual([{sequence: simpleSequence, numberOfFulfilled: 0}]) game.pick(0, 0); expect(game.buffer).toEqual(["00"]); @@ -60,7 +61,7 @@ describe("GameState", () => { "AA", "AA", "BB", "BB", "CC", "AA", "CC", "CC", "CC", - ], [sequence]); + ], [sequence], unrestrictedBuffer); expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 0}]) game.pick(0, 0); expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 1}]) @@ -74,4 +75,32 @@ describe("GameState", () => { expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 3}]) }); }); + + describe("game end conditions", () => { + test("game won", () => { + const game = new Game([ + "AA", "AA", "BB", + "BB", "CC", "AA", + "CC", "CC", "CC", + ], [["AA", "BB", "CC"]], 3) + game.pick(0, 0) + game.pick(2, 0) + game.pick(2, 2) + expect(game.state).toEqual(EndState.Won) + expect(() => game.pick(0, 2)).toThrow(); + }) + + test("game loose", () => { + const game = new Game([ + "AA", "AA", "BB", + "BB", "CC", "AA", + "CC", "CC", "CC", + ], [["AA", "BB", "CC"]], 3) + game.pick(0, 0) + game.pick(1, 0) + game.pick(1, 2) + expect(game.state).toEqual(EndState.Lost) + expect(() => game.pick(2, 2)).toThrow(); + }) + }); }); diff --git a/src/game/Game.ts b/src/game/Game.ts index 8c5d851..f56d28c 100644 --- a/src/game/Game.ts +++ b/src/game/Game.ts @@ -81,7 +81,7 @@ export class Game { public readonly size: number public readonly buffer: string[] = [] - constructor(public readonly matrix: string[], private readonly sequences: string[][]) { + constructor(public readonly matrix: string[], private readonly sequences: string[][], public readonly maxBufferLength: number) { this.size = Math.sqrt(matrix.length) } @@ -112,6 +112,17 @@ export class Game { }) } + private checkEndGame(): void { + const isSequenceFulfilled = (sequence: Sequence) => sequence.sequence.length === sequence.numberOfFulfilled + if (this.getSequences().every(isSequenceFulfilled)) { + this.state = EndState.Won + } else { + if (this.buffer.length >= this.maxBufferLength) { + this.state = EndState.Lost + } + } + } + pick(row: number, column: number): void { if (row < 0 || column < 0 || row >= this.size || column >= this.size) { throw new IllegalMoveError() @@ -153,5 +164,7 @@ export class Game { })(selectionMode) } })(this.state) + + this.checkEndGame(); } }