|
|
@ -3,7 +3,10 @@ import { BoardType, SquareState, formatSquareState, parseSquareState } from "./d |
|
|
|
|
|
|
|
|
|
|
|
export class Board implements BoardType { |
|
|
|
export class Board implements BoardType { |
|
|
|
// State should be immutable
|
|
|
|
// State should be immutable
|
|
|
|
constructor(private readonly state: SquareState[][]) {} |
|
|
|
constructor( |
|
|
|
|
|
|
|
private readonly state: SquareState[][], |
|
|
|
|
|
|
|
private serialized?: string, |
|
|
|
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
|
|
hasRow(row: number) { |
|
|
|
hasRow(row: number) { |
|
|
|
return !!this.state[row]; |
|
|
|
return !!this.state[row]; |
|
|
@ -53,10 +56,11 @@ export class Board implements BoardType { |
|
|
|
return squareState; |
|
|
|
return squareState; |
|
|
|
}), |
|
|
|
}), |
|
|
|
), |
|
|
|
), |
|
|
|
|
|
|
|
serialized, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
serialize() { |
|
|
|
private forceSerialize() { |
|
|
|
return this.state |
|
|
|
return this.state |
|
|
|
.map((row) => |
|
|
|
.map((row) => |
|
|
|
row |
|
|
|
row |
|
|
@ -72,4 +76,13 @@ export class Board implements BoardType { |
|
|
|
) |
|
|
|
) |
|
|
|
.join("|"); |
|
|
|
.join("|"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
serialize() { |
|
|
|
|
|
|
|
if (!this.serialized) { |
|
|
|
|
|
|
|
const serialized = this.forceSerialize(); |
|
|
|
|
|
|
|
this.serialized = serialized; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.serialized; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|