mirror of
https://github.com/Kugelschieber/breach.git
synced 2026-01-18 12:00:25 +00:00
Move Game config into separate object
The 4 constructor arguments already were a bit confusing with two `number` types, also the game config will likely be the output of the matrix generator, so it makes sense to have a separate interface for that anyway.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import "jest"
|
import "jest"
|
||||||
import {EndState, Game, SelectionMode} from "./Game"
|
import { EndState, Game, SelectionMode } from "./Game"
|
||||||
|
|
||||||
describe("GameState", () => {
|
describe("GameState", () => {
|
||||||
const threeByThreeMatrix = [
|
const threeByThreeMatrix = [
|
||||||
@@ -12,42 +12,67 @@ describe("GameState", () => {
|
|||||||
const unlimitedTime = 999 * 1000
|
const unlimitedTime = 999 * 1000
|
||||||
|
|
||||||
test("getting size works", () => {
|
test("getting size works", () => {
|
||||||
expect((new Game(["00"], [["AA"]], unrestrictedBuffer, unlimitedTime)).size).toEqual(1)
|
expect((new Game({ matrix: ["00"], sequences: [["AA"]], maxBufferLength: unrestrictedBuffer, timeout: unlimitedTime })).size).toEqual(1)
|
||||||
expect((new Game([
|
expect((new Game({
|
||||||
"00", "01",
|
matrix: [
|
||||||
"10", "11",
|
"00", "01",
|
||||||
], [], unrestrictedBuffer, unlimitedTime)).size).toEqual(2)
|
"10", "11",
|
||||||
|
],
|
||||||
|
sequences: [],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
})).size).toEqual(2)
|
||||||
});
|
});
|
||||||
|
|
||||||
test("getting cell works", () => {
|
test("getting cell works", () => {
|
||||||
const game = new Game(threeByThreeMatrix, [["AA"]], unrestrictedBuffer, unlimitedTime);
|
const game = new Game({
|
||||||
|
matrix: threeByThreeMatrix,
|
||||||
|
sequences: [["AA"]],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
});
|
||||||
expect(game.getCell(0, 2)).toEqual({ value: "02", isUsed: false })
|
expect(game.getCell(0, 2)).toEqual({ value: "02", isUsed: false })
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("picking", () => {
|
describe("picking", () => {
|
||||||
test("starts with free pick", () => {
|
test("starts with free pick", () => {
|
||||||
const game = new Game([], [["AA"]], 1, unlimitedTime);
|
const game = new Game({ matrix: [], sequences: [["AA"]], maxBufferLength: 1, timeout: unlimitedTime });
|
||||||
expect(game.state).toEqual({selectionMode: SelectionMode.FreePick})
|
expect(game.state).toEqual({ selectionMode: SelectionMode.FreePick })
|
||||||
});
|
});
|
||||||
|
|
||||||
test("picking cells works", () => {
|
test("picking cells works", () => {
|
||||||
const game = new Game(threeByThreeMatrix, [["AA"]], unrestrictedBuffer, unlimitedTime);
|
const game = new Game({
|
||||||
|
matrix: threeByThreeMatrix,
|
||||||
|
sequences: [["AA"]],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
});
|
||||||
game.pick(0, 0);
|
game.pick(0, 0);
|
||||||
expect(game.state).toEqual({selectionMode: SelectionMode.RowPick, column: 0});
|
expect(game.state).toEqual({ selectionMode: SelectionMode.RowPick, column: 0 });
|
||||||
expect(game.getCell(0, 0).isUsed).toEqual(true);
|
expect(game.getCell(0, 0).isUsed).toEqual(true);
|
||||||
expect(() => game.pick(0, 2)).toThrow();
|
expect(() => game.pick(0, 2)).toThrow();
|
||||||
game.pick(2, 0);
|
game.pick(2, 0);
|
||||||
expect(game.state).toEqual({selectionMode: SelectionMode.ColumnPick, row: 2});
|
expect(game.state).toEqual({ selectionMode: SelectionMode.ColumnPick, row: 2 });
|
||||||
});
|
});
|
||||||
|
|
||||||
test("cannot pick cell twice", () => {
|
test("cannot pick cell twice", () => {
|
||||||
const game = new Game(threeByThreeMatrix, [["AA"]], unrestrictedBuffer, unlimitedTime);
|
const game = new Game({
|
||||||
|
matrix: threeByThreeMatrix,
|
||||||
|
sequences: [["AA"]],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
});
|
||||||
game.pick(0, 0);
|
game.pick(0, 0);
|
||||||
expect(() => game.pick(0, 0)).toThrow();
|
expect(() => game.pick(0, 0)).toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("picking outside of range fails", () => {
|
test("picking outside of range fails", () => {
|
||||||
const game = new Game(threeByThreeMatrix, [], unrestrictedBuffer, unlimitedTime);
|
const game = new Game({
|
||||||
|
matrix: threeByThreeMatrix,
|
||||||
|
sequences: [],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
});
|
||||||
expect(() => game.pick(-1, 0)).toThrow();
|
expect(() => game.pick(-1, 0)).toThrow();
|
||||||
expect(() => game.pick(0, -1)).toThrow();
|
expect(() => game.pick(0, -1)).toThrow();
|
||||||
expect(() => game.pick(3, 0)).toThrow();
|
expect(() => game.pick(3, 0)).toThrow();
|
||||||
@@ -56,45 +81,60 @@ describe("GameState", () => {
|
|||||||
|
|
||||||
test("picking fills buffer, fulfills sequence", () => {
|
test("picking fills buffer, fulfills sequence", () => {
|
||||||
const simpleSequence = ["00", "10", "20"]
|
const simpleSequence = ["00", "10", "20"]
|
||||||
const game = new Game(threeByThreeMatrix, [simpleSequence], unrestrictedBuffer, unlimitedTime);
|
const game = new Game({
|
||||||
expect(game.getSequences()).toEqual([{sequence: simpleSequence, numberOfFulfilled: 0}])
|
matrix: threeByThreeMatrix,
|
||||||
|
sequences: [simpleSequence],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
});
|
||||||
|
expect(game.getSequences()).toEqual([{ sequence: simpleSequence, numberOfFulfilled: 0 }])
|
||||||
game.pick(0, 0);
|
game.pick(0, 0);
|
||||||
expect(game.buffer).toEqual([{
|
expect(game.buffer).toEqual([{
|
||||||
positionInMatrixRow: 0,
|
positionInMatrixRow: 0,
|
||||||
positionInMatrixColumn: 0,
|
positionInMatrixColumn: 0,
|
||||||
value: "00"
|
value: "00"
|
||||||
}]);
|
}]);
|
||||||
expect(game.getSequences()).toEqual([{sequence: simpleSequence, numberOfFulfilled: 1}])
|
expect(game.getSequences()).toEqual([{ sequence: simpleSequence, numberOfFulfilled: 1 }])
|
||||||
});
|
});
|
||||||
|
|
||||||
test("picking fulfills second sequence occurence", () => {
|
test("picking fulfills second sequence occurence", () => {
|
||||||
const sequence = ["AA", "BB", "CC"]
|
const sequence = ["AA", "BB", "CC"]
|
||||||
const game = new Game([
|
const game = new Game({
|
||||||
|
matrix: [
|
||||||
"AA", "AA", "BB",
|
"AA", "AA", "BB",
|
||||||
"BB", "CC", "AA",
|
"BB", "CC", "AA",
|
||||||
"CC", "CC", "CC",
|
"CC", "CC", "CC",
|
||||||
], [sequence], unrestrictedBuffer, unlimitedTime);
|
],
|
||||||
expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 0}])
|
sequences: [sequence],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
});
|
||||||
|
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 0 }])
|
||||||
game.pick(0, 0);
|
game.pick(0, 0);
|
||||||
expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 1}])
|
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 1 }])
|
||||||
game.pick(2, 0);
|
game.pick(2, 0);
|
||||||
expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 2}])
|
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 2 }])
|
||||||
game.pick(2, 1);
|
game.pick(2, 1);
|
||||||
expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 1}])
|
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 1 }])
|
||||||
game.pick(0, 1);
|
game.pick(0, 1);
|
||||||
expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 2}])
|
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 2 }])
|
||||||
game.pick(0, 2);
|
game.pick(0, 2);
|
||||||
expect(game.getSequences()).toEqual([{sequence: sequence, numberOfFulfilled: 3}])
|
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 3 }])
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("game end conditions", () => {
|
describe("game end conditions", () => {
|
||||||
test("game won", () => {
|
test("game won", () => {
|
||||||
const game = new Game([
|
const game = new Game({
|
||||||
|
matrix: [
|
||||||
"AA", "AA", "BB",
|
"AA", "AA", "BB",
|
||||||
"BB", "CC", "AA",
|
"BB", "CC", "AA",
|
||||||
"CC", "CC", "CC",
|
"CC", "CC", "CC",
|
||||||
], [["AA", "BB", "CC"]], 3, unlimitedTime)
|
],
|
||||||
|
sequences: [["AA", "BB", "CC"]],
|
||||||
|
maxBufferLength: 3,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
})
|
||||||
game.pick(0, 0)
|
game.pick(0, 0)
|
||||||
game.pick(2, 0)
|
game.pick(2, 0)
|
||||||
game.pick(2, 2)
|
game.pick(2, 2)
|
||||||
@@ -103,11 +143,16 @@ describe("GameState", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("game loose", () => {
|
test("game loose", () => {
|
||||||
const game = new Game([
|
const game = new Game({
|
||||||
|
matrix: [
|
||||||
"AA", "AA", "BB",
|
"AA", "AA", "BB",
|
||||||
"BB", "CC", "AA",
|
"BB", "CC", "AA",
|
||||||
"CC", "CC", "CC",
|
"CC", "CC", "CC",
|
||||||
], [["AA", "BB", "CC"]], 3, unlimitedTime)
|
],
|
||||||
|
sequences: [["AA", "BB", "CC"]],
|
||||||
|
maxBufferLength: 3,
|
||||||
|
timeout: unlimitedTime,
|
||||||
|
})
|
||||||
game.pick(0, 0)
|
game.pick(0, 0)
|
||||||
game.pick(1, 0)
|
game.pick(1, 0)
|
||||||
game.pick(1, 2)
|
game.pick(1, 2)
|
||||||
@@ -133,11 +178,16 @@ describe("GameState", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test("loosing through timeout works", () => {
|
test("loosing through timeout works", () => {
|
||||||
const game = new Game([
|
const game = new Game({
|
||||||
"AA", "AA", "BB",
|
matrix: [
|
||||||
"BB", "CC", "AA",
|
"AA", "AA", "BB",
|
||||||
"CC", "CC", "CC",
|
"BB", "CC", "AA",
|
||||||
], [["AA"]], 3, 10_000)
|
"CC", "CC", "CC",
|
||||||
|
],
|
||||||
|
sequences: [["AA"]],
|
||||||
|
maxBufferLength: 3,
|
||||||
|
timeout: 10_000,
|
||||||
|
})
|
||||||
fakeTimeProgress(1_000)
|
fakeTimeProgress(1_000)
|
||||||
expect(game.remainingMilliseconds).toEqual(9_000)
|
expect(game.remainingMilliseconds).toEqual(9_000)
|
||||||
fakeTimeProgress(1_000)
|
fakeTimeProgress(1_000)
|
||||||
@@ -150,11 +200,17 @@ describe("GameState", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("clock stops when game is won", () => {
|
test("clock stops when game is won", () => {
|
||||||
const game = new Game([
|
const game = new Game({
|
||||||
"AA", "AA", "BB",
|
matrix: [
|
||||||
"BB", "CC", "AA",
|
"AA", "AA", "BB",
|
||||||
"CC", "CC", "CC",
|
"BB", "CC", "AA",
|
||||||
], [["AA"]], 3, 10_000)
|
"CC", "CC", "CC",
|
||||||
|
],
|
||||||
|
sequences: [["AA"]],
|
||||||
|
maxBufferLength: unrestrictedBuffer,
|
||||||
|
timeout: 10_000,
|
||||||
|
}
|
||||||
|
)
|
||||||
fakeTimeProgress(1_000)
|
fakeTimeProgress(1_000)
|
||||||
game.pick(0, 0)
|
game.pick(0, 0)
|
||||||
expect(game.remainingMilliseconds).toEqual(9_000)
|
expect(game.remainingMilliseconds).toEqual(9_000)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import GameConfiguration from "./GameConfiguration";
|
||||||
|
|
||||||
export enum EndState {
|
export enum EndState {
|
||||||
Won,
|
Won,
|
||||||
Lost,
|
Lost,
|
||||||
@@ -95,31 +97,26 @@ export class Game {
|
|||||||
private readonly startTimeTimeStamp: number
|
private readonly startTimeTimeStamp: number
|
||||||
private endTimestamp: number | null = null
|
private endTimestamp: number | null = null
|
||||||
|
|
||||||
constructor(
|
constructor(private readonly config: GameConfiguration) {
|
||||||
public readonly matrix: string[],
|
this.size = Math.sqrt(config.matrix.length)
|
||||||
private readonly sequences: string[][],
|
|
||||||
public readonly maxBufferLength: number,
|
|
||||||
public readonly timeout: number,
|
|
||||||
) {
|
|
||||||
this.size = Math.sqrt(matrix.length)
|
|
||||||
this.timeoutInterval = setTimeout(() => {
|
this.timeoutInterval = setTimeout(() => {
|
||||||
this.state = EndState.Lost
|
this.state = EndState.Lost
|
||||||
this.stopClock()
|
this.stopClock()
|
||||||
},
|
},
|
||||||
this.timeout)
|
this.config.timeout)
|
||||||
this.startTimeTimeStamp = Date.now()
|
this.startTimeTimeStamp = Date.now()
|
||||||
}
|
}
|
||||||
|
|
||||||
get remainingMilliseconds(): number {
|
get remainingMilliseconds(): number {
|
||||||
if (this.endTimestamp) {
|
if (this.endTimestamp) {
|
||||||
return this.timeout - (this.endTimestamp - this.startTimeTimeStamp)
|
return this.config.timeout - (this.endTimestamp - this.startTimeTimeStamp)
|
||||||
}
|
}
|
||||||
return this.timeout - (Date.now() - this.startTimeTimeStamp)
|
return this.config.timeout - (Date.now() - this.startTimeTimeStamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
getCell(row: number, column: number): Cell {
|
getCell(row: number, column: number): Cell {
|
||||||
return {
|
return {
|
||||||
value: this.matrix[row + column * this.size],
|
value: this.config.matrix[row + column * this.size],
|
||||||
isUsed: this.buffer.some(x =>
|
isUsed: this.buffer.some(x =>
|
||||||
x.positionInMatrixRow == row &&
|
x.positionInMatrixRow == row &&
|
||||||
x.positionInMatrixColumn == column
|
x.positionInMatrixColumn == column
|
||||||
@@ -128,7 +125,7 @@ export class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSequences(): Sequence[] {
|
getSequences(): Sequence[] {
|
||||||
return this.sequences.map(sequence => {
|
return this.config.sequences.map(sequence => {
|
||||||
let longestPrefixLength = 0
|
let longestPrefixLength = 0
|
||||||
for (let i = 0; i < this.buffer.length; ++i) {
|
for (let i = 0; i < this.buffer.length; ++i) {
|
||||||
let prefixLength = 0;
|
let prefixLength = 0;
|
||||||
@@ -162,7 +159,7 @@ export class Game {
|
|||||||
this.state = EndState.Won
|
this.state = EndState.Won
|
||||||
} else {
|
} else {
|
||||||
this.stopClock()
|
this.stopClock()
|
||||||
if (this.buffer.length >= this.maxBufferLength) {
|
if (this.buffer.length >= this.config.maxBufferLength) {
|
||||||
this.state = EndState.Lost
|
this.state = EndState.Lost
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
src/game/GameConfiguration.ts
Normal file
6
src/game/GameConfiguration.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export default interface GameConfiguration {
|
||||||
|
matrix: string[]
|
||||||
|
sequences: string[][]
|
||||||
|
maxBufferLength: number
|
||||||
|
timeout: number
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user