Repository navigation
Expand file tree
/
Copy pathBoard.js
More file actions
59 lines (53 loc) · 1.69 KB
/
Copy pathBoard.js
File metadata and controls
59 lines (53 loc) · 1.69 KB
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
/* global Piece, Square */
class Board {
constructor() {
this.board = [];
this.constructBoard();
}
constructBoard() {
let isWhite = false;
const $board = document.getElementById('board');
const $columnLabels = document.getElementById('column-labels');
$columnLabels.classList.add('row');
for (let i = 0; i < 8; i++) {
const $row = document.createElement('div');
$row.classList = 'row';
// Column labels
const $columnLabel = document.createElement('div');
$columnLabel.classList = 'square label';
$columnLabel.textContent = 'ABCDEFGH'[i];
$columnLabels.appendChild($columnLabel);
// Row labels
const $rowLabel = document.createElement('div');
$rowLabel.classList = 'square label';
$rowLabel.textContent = i + 1;
$row.appendChild($rowLabel);
for (let j = 0; j < 8; j++) {
const $square = document.createElement('div');
$square.classList = 'square';
this.board.push(new Square(i + 1, j + 1, isWhite, $square));
isWhite = !isWhite;
$row.appendChild($square);
}
$board.appendChild($row);
isWhite = !isWhite;
}
}
filter(callback) {
return this.board.filter(callback);
}
sq(name) {
return this.board.find(square => square.name.toLowerCase() === name.toLowerCase());
}
squareAt(row, column) {
return this.board.find(square => square.row === row && square.column === column);
}
opponentsOf(piece) {
return this.piecesByColour(piece.isWhite ? 'black' : 'white');
}
piecesByColour(colour) {
return this.board
.filter(square => square.piece && square.piece.colour === colour)
.map(square => square.piece);
}
}