-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.ts
118 lines (105 loc) · 2.98 KB
/
Board.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { BoardPosition } from "./BoardPosition.ts";
import { ChessPiece } from "./ChessPieces/_ChessPiece.ts";
import { Pawn } from "./ChessPieces/Pawn.ts";
import { Rook } from "./ChessPieces/Rook.ts";
import { Knight } from "./ChessPieces/Knight.ts";
import { Bishop } from "./ChessPieces/Bishop.ts";
import { King } from "./ChessPieces/King.ts";
import { Queen } from "./ChessPieces/Queen.ts";
export class Board {
state: (ChessPiece | null)[][];
constructor(board?: (ChessPiece | null)[][]) {
this.state = board || initBoard();
}
get(pos: BoardPosition): ChessPiece | null {
return this.state[pos.y][pos.x];
}
set(pos: BoardPosition, piece: ChessPiece | null) {
this.state[pos.y][pos.x] = piece;
}
move(from: BoardPosition, to: BoardPosition) {
const piece = this.get(from);
if (piece === null) throw new Error("No piece at " + from.toString());
this.set(from, null);
this.set(to, piece);
}
validateMove(from: BoardPosition, to: BoardPosition): boolean {
const pieceAtPos = this.get(from);
if (pieceAtPos === null) return false;
return pieceAtPos.validateMove(from, to, this);
}
toFEN(): string {
const fen = this.state.map((row) => {
let fenRow = "";
let emptySpaces = 0;
for (const piece of row.slice().reverse()) {
if (piece === null) {
emptySpaces++;
} else {
if (emptySpaces > 0) {
fenRow += emptySpaces.toString();
emptySpaces = 0;
}
fenRow += piece.toFEN();
}
}
if (emptySpaces > 0) {
fenRow += emptySpaces.toString();
}
return fenRow;
}).slice().reverse().join("/");
return fen;
}
}
function initBoard(): (ChessPiece | null)[][] {
return new Array(8)
.fill(null)
.map((_, row) => {
switch (row) {
case 0:
return initFirstRow(true);
case 1:
return initPawnRow(true);
case 7:
return initFirstRow(false);
case 6:
return initPawnRow(false);
default:
return initEmptyRow();
}
});
}
const initPawnRow = (isWhite: boolean): Pawn[] => {
return new Array(8)
.fill(null)
.map(() => new Pawn(isWhite));
};
const initFirstRow = (isWhite: boolean): (ChessPiece | null)[] => {
// Columns are counted H->A, thus King is on 3 (E) and Queen is on 4 (D)
return new Array(8)
.fill(null)
.map((_, column) => {
switch (column) {
case 0:
case 7:
return new Rook(isWhite);
case 1:
case 6:
return new Knight(isWhite);
case 2:
case 5:
return new Bishop(isWhite);
case 3:
return new King(isWhite);
case 4:
return new Queen(isWhite);
default:
return null;
}
});
};
const initEmptyRow = (): null[] => {
return new Array(8).fill(null);
};
export const initEmptyBoard = (): null[][] =>
new Array(8).fill(null).map(() => new Array(8).fill(null));