mirror of
https://github.com/Kugelschieber/breach.git
synced 2026-01-18 03:50:24 +00:00
Allow winning or losing
This commit is contained in:
@@ -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();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user