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