Allow winning or losing

This commit is contained in:
Erik Schilling
2020-12-16 20:38:39 +01:00
parent cac159bd89
commit 480daf284e
2 changed files with 53 additions and 11 deletions

View File

@@ -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();
}
}