mirror of
https://github.com/Kugelschieber/breach.git
synced 2026-01-18 12:00:25 +00:00
Do not allow picking cells twice
This commit is contained in:
@@ -76,10 +76,21 @@ interface Sequence {
|
||||
numberOfFulfilled: number
|
||||
}
|
||||
|
||||
interface Buffer {
|
||||
value: string
|
||||
positionInMatrixRow: number
|
||||
positionInMatrixColumn: number
|
||||
}
|
||||
|
||||
interface Cell {
|
||||
value: string
|
||||
isUsed: boolean
|
||||
}
|
||||
|
||||
export class Game {
|
||||
state: State = {selectionMode: SelectionMode.FreePick}
|
||||
public readonly size: number
|
||||
public readonly buffer: string[] = []
|
||||
public readonly buffer: Buffer[] = []
|
||||
private readonly timeoutInterval: ReturnType<typeof setTimeout>
|
||||
private readonly startTimeTimeStamp: number
|
||||
private endTimestamp: number | null = null
|
||||
@@ -106,8 +117,14 @@ export class Game {
|
||||
return this.timeout - (Date.now() - this.startTimeTimeStamp)
|
||||
}
|
||||
|
||||
getCell(row: number, column: number): string {
|
||||
return this.matrix[row + column * this.size]
|
||||
getCell(row: number, column: number): Cell {
|
||||
return {
|
||||
value: this.matrix[row + column * this.size],
|
||||
isUsed: this.buffer.some(x =>
|
||||
x.positionInMatrixRow == row &&
|
||||
x.positionInMatrixColumn == column
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
getSequences(): Sequence[] {
|
||||
@@ -116,7 +133,7 @@ export class Game {
|
||||
for (let i = 0; i < this.buffer.length; ++i) {
|
||||
let prefixLength = 0;
|
||||
for (let j = 0; j < Math.min(sequence.length, this.buffer.length - i); ++j) {
|
||||
if (this.buffer[i + j] != sequence[j]) {
|
||||
if (this.buffer[i + j].value != sequence[j]) {
|
||||
// abort sequence
|
||||
prefixLength = 0
|
||||
break;
|
||||
@@ -160,7 +177,16 @@ export class Game {
|
||||
Won: () => {throw new IllegalMoveError()},
|
||||
Lost: () => {throw new IllegalMoveError()},
|
||||
InProgress: (selectionMode) => {
|
||||
this.buffer.push(this.getCell(row, column))
|
||||
const cell = this.getCell(row, column)
|
||||
if (cell.isUsed) {
|
||||
throw new IllegalMoveError()
|
||||
}
|
||||
|
||||
this.buffer.push({
|
||||
value: cell.value,
|
||||
positionInMatrixRow: row,
|
||||
positionInMatrixColumn: column,
|
||||
})
|
||||
|
||||
matchSelectionState({
|
||||
Free: () => {
|
||||
|
||||
Reference in New Issue
Block a user