Added game state to all components, renamed timeout to timeoutMilliseconds, removed score from savegame.

This commit is contained in:
2020-12-21 15:48:20 +01:00
parent 639fcf8c6e
commit c4b914ccfd
11 changed files with 128 additions and 91 deletions

View File

@@ -12,7 +12,7 @@ describe("GameState", () => {
const unlimitedTime = 999 * 1000
test("getting size works", () => {
expect((new Game({ matrix: ["00"], sequences: [["AA"]], maxBufferLength: unrestrictedBuffer, timeout: unlimitedTime })).size).toEqual(1)
expect((new Game({ matrix: ["00"], sequences: [["AA"]], maxBufferLength: unrestrictedBuffer, timeoutMilliseconds: unlimitedTime })).size).toEqual(1)
expect((new Game({
matrix: [
"00", "01",
@@ -20,7 +20,7 @@ describe("GameState", () => {
],
sequences: [],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
})).size).toEqual(2)
});
@@ -29,14 +29,14 @@ describe("GameState", () => {
matrix: threeByThreeMatrix,
sequences: [["AA"]],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
});
expect(game.getCell(0, 2)).toEqual({ value: "02", isUsed: false })
});
describe("picking", () => {
test("starts with free pick", () => {
const game = new Game({ matrix: [], sequences: [["AA"]], maxBufferLength: 1, timeout: unlimitedTime });
const game = new Game({ matrix: [], sequences: [["AA"]], maxBufferLength: 1, timeoutMilliseconds: unlimitedTime });
expect(game.state).toEqual({ selectionMode: SelectionMode.FreePick })
});
@@ -45,7 +45,7 @@ describe("GameState", () => {
matrix: threeByThreeMatrix,
sequences: [["AA"]],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
});
game.pick(0, 0);
expect(game.state).toEqual({ selectionMode: SelectionMode.RowPick, column: 0 });
@@ -60,7 +60,7 @@ describe("GameState", () => {
matrix: threeByThreeMatrix,
sequences: [["AA"]],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
});
game.pick(0, 0);
expect(() => game.pick(0, 0)).toThrow();
@@ -71,7 +71,7 @@ describe("GameState", () => {
matrix: threeByThreeMatrix,
sequences: [],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
});
expect(() => game.pick(-1, 0)).toThrow();
expect(() => game.pick(0, -1)).toThrow();
@@ -85,7 +85,7 @@ describe("GameState", () => {
matrix: threeByThreeMatrix,
sequences: [simpleSequence],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
});
expect(game.getSequences()).toEqual([{ sequence: simpleSequence, numberOfFulfilled: 0 }])
game.pick(0, 0);
@@ -107,7 +107,7 @@ describe("GameState", () => {
],
sequences: [sequence],
maxBufferLength: unrestrictedBuffer,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
});
expect(game.getSequences()).toEqual([{ sequence: sequence, numberOfFulfilled: 0 }])
game.pick(0, 0);
@@ -133,7 +133,7 @@ describe("GameState", () => {
],
sequences: [["AA", "BB", "CC"]],
maxBufferLength: 3,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
})
game.pick(0, 0)
game.pick(2, 0)
@@ -151,7 +151,7 @@ describe("GameState", () => {
],
sequences: [["AA", "BB", "CC"]],
maxBufferLength: 3,
timeout: unlimitedTime,
timeoutMilliseconds: unlimitedTime,
})
game.pick(0, 0)
game.pick(1, 0)
@@ -186,7 +186,7 @@ describe("GameState", () => {
],
sequences: [["AA"]],
maxBufferLength: 3,
timeout: 10_000,
timeoutMilliseconds: 10_000,
})
fakeTimeProgress(1_000)
expect(game.remainingMilliseconds).toEqual(9_000)
@@ -208,7 +208,7 @@ describe("GameState", () => {
],
sequences: [["AA"]],
maxBufferLength: unrestrictedBuffer,
timeout: 10_000,
timeoutMilliseconds: 10_000,
}
)
fakeTimeProgress(1_000)

View File

@@ -100,18 +100,17 @@ export class Game {
constructor(private readonly config: GameConfiguration) {
this.size = Math.sqrt(config.matrix.length)
this.timeoutInterval = setTimeout(() => {
this.state = EndState.Lost
this.stopClock()
},
this.config.timeout)
this.state = EndState.Lost
this.stopClock()
}, this.config.timeoutMilliseconds)
this.startTimeTimeStamp = Date.now()
}
get remainingMilliseconds(): number {
if (this.endTimestamp) {
return this.config.timeout - (this.endTimestamp - this.startTimeTimeStamp)
return this.config.timeoutMilliseconds - (this.endTimestamp - this.startTimeTimeStamp)
}
return this.config.timeout - (Date.now() - this.startTimeTimeStamp)
return this.config.timeoutMilliseconds - (Date.now() - this.startTimeTimeStamp)
}
getCell(row: number, column: number): Cell {
@@ -120,7 +119,7 @@ export class Game {
isUsed: this.buffer.some(x =>
x.positionInMatrixRow == row &&
x.positionInMatrixColumn == column
),
),
}
}
@@ -218,4 +217,20 @@ export class Game {
this.checkEndGame();
}
get maxBufferLength(): number {
return this.config.maxBufferLength
}
get sequences(): string[][] {
return this.config.sequences
}
get matrix(): string[] {
return this.config.matrix
}
get timeoutMilliseconds(): number {
return this.config.timeoutMilliseconds
}
}

View File

@@ -2,5 +2,5 @@ export default interface GameConfiguration {
matrix: string[]
sequences: string[][]
maxBufferLength: number
timeout: number
}
timeoutMilliseconds: number
}

View File

@@ -4,7 +4,6 @@ import { saveGameKey, saveGame, loadGame } from './SaveGame';
describe("SaveGame", () => {
const state = {
level: 42,
score: 89,
};
beforeEach(() => {

View File

@@ -2,7 +2,6 @@ export const saveGameKey = "save_game";
export interface SaveGame {
level: number
score: number
}
export function saveGame(saveGame: SaveGame): void {