-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessBoard.ts
65 lines (57 loc) · 1.91 KB
/
ChessBoard.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
import {Colours} from "./enums";
import {newPawn} from "./pieces/Pawn";
import {newKnight} from "./pieces/Knight";
import {newRook} from "./pieces/Rook";
import {newSquare, Square} from "./Square";
import {newBishop} from "./pieces/Bishop";
import {newKing} from "./pieces/King";
import {newQueen} from "./pieces/Queen";
import {CoordinatePair} from "./Move";
export interface ChessBoard {
board: Square[][];
selected_piece: CoordinatePair | null
}
export function newChessBoard(): ChessBoard {
let board: Square[][] = [];
// initialise all indexes with Squares
for (let i = 0; i < 8; i++) {
let current: Square[] = [];
board.push(current);
for (let j = 0; j < 8; j++) {
board[i].push(newSquare(j, i));
}
}
// set up the board with chess pieces
// set the top of the board as black
let colour = Colours.BLACK;
board[0][0].piece = newRook(colour);
board[0][1].piece = newKnight(colour);
board[0][2].piece = newBishop(colour);
board[0][3].piece = newQueen(colour);
board[0][4].piece = newKing(colour);
board[0][5].piece = newBishop(colour);
board[0][6].piece = newKnight(colour);
board[0][7].piece = newRook(colour);
// set the pawns on the next row
for (let i=0; i<8; i++) {
board[1][i].piece = newPawn(colour);
}
// set the bottom of the board as white
colour = Colours.WHITE;
board[7][0].piece = newRook(colour);
board[7][1].piece = newKnight(colour);
board[7][2].piece = newBishop(colour);
board[7][3].piece = newQueen(colour);
board[7][4].piece = newKing(colour);
board[7][5].piece = newBishop(colour);
board[7][6].piece = newKnight(colour);
board[7][7].piece = newRook(colour);
// set the pawns on the next row
for (let i=0; i<8; i++) {
board[6][i].piece = newPawn(colour);
}
return {
board: board,
selected_piece: null
};
}