mirror of
https://github.com/Kugelschieber/breach.git
synced 2026-01-18 12:00:25 +00:00
Allow winning or losing
This commit is contained in:
@@ -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