-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
164 lines (147 loc) · 5.53 KB
/
library.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const BOARD_SIZE = 8;
class GameState {
constructor() {
// board consists of key-value pairs to store each piece
this.board = {};
// isWhiteTurn keeps track of who should make the next move
this.isWhiteTurn = true;
}
/**
* Adds pieces to the board as an initial setup for the game.
*/
populateBoard() {
// Adds pawns for both sides
for (let i = 0; i < BOARD_SIZE; i++) {
this.board[`p0${i}`] = new Pawn(false, [1, i]);
this.board[`p1${i}`] = new Pawn(true, [6, i]);
}
// Adds remaining black pieces
this.board["r00"] = new Rook(false, [0, 0]);
this.board["k00"] = new Knight(false, [0, 1]);
this.board["b00"] = new Bishop(false, [0, 2]);
this.board["q0"] = new Queen(false, [0, 3]);
this.board["K0"] = new King(false, [0, 4]);
this.board["b01"] = new Bishop(false, [0, 5]);
this.board["k01"] = new Knight(false, [0, 6]);
this.board["r01"] = new Rook(false, [0, 7]);
// Adds remaining white pieces
this.board["r10"] = new Rook(false, [7, 0]);
this.board["k10"] = new Knight(false, [7, 1]);
this.board["b10"] = new Bishop(false, [7, 2]);
this.board["q1"] = new Queen(false, [7, 3]);
this.board["K1"] = new King(false, [7, 4]);
this.board["b11"] = new Bishop(false, [7, 5]);
this.board["k11"] = new Knight(false, [7, 6]);
this.board["r01"] = new Rook(false, [7, 7]);
}
}
class Piece {
// Enum to represent the piece types in chess
static types = Object.freeze({
king: 0,
queen: 1,
rook: 2,
bishop: 3,
knight: 4,
pawn: 5
});
/**
* Creates a Piece object with the specified colour, piece type, and location.
*
* @param {boolean} isWhite - The colour of the piece, true if white, false if black.
* @param {string} type - The type of the piece (e.g., 'king', 'queen', 'rook', etc.).
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, type, location) {
if (typeof isWhite !== "boolean") throw "isWhite must be a boolean";
if (!Piece.isValidType(type)) throw "Invalid piece type";
if (!Array.isArray(location)) throw "Location must be an array";
if (location.length !== 2) throw "Location has an invalid length";
if (typeof location[0] !== "number" || typeof location[1] !== "number")
throw "The elements in location must be numbers";
this.isWhite = isWhite;
this.type = type;
this.location = location;
}
toString() {
const colour = this.isWhite ? "White" : "Black";
const locationString = `(${this.location[0]}, ${this.location[1]})`;
return `${colour} ${this.type} at ${locationString}`;
}
/**
* Validates the given type.
*
* @param {string} type
* @returns {boolean} True if the type exists, false otherwise.
*/
static isValidType(type) {
return type in Piece.types;
}
}
class King extends Piece {
/**
* Creates a King object with the specified colour and location.
*
* @param {boolean} isWhite - The colour of the piece.
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, location) {
super(isWhite, "king", location);
}
// Note: Check if toString method is inherited
}
class Queen extends Piece {
/**
* Creates a Queen object with the specified colour and location.
*
* @param {boolean} isWhite - The colour of the piece.
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, location) {
super(isWhite, "queen", location);
}
}
class Rook extends Piece {
/**
* Creates a Rook object with the specified colour and location.
*
* @param {boolean} isWhite - The colour of the piece.
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, location) {
super(isWhite, "rook", location);
}
}
class Bishop extends Piece {
/**
* Creates a Bishop object with the specified colour and location.
*
* @param {boolean} isWhite - The colour of the piece.
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, location) {
super(isWhite, "bishop", location);
}
}
class Knight extends Piece {
/**
* Creates a Knight object with the specified colour and location.
*
* @param {boolean} isWhite - The colour of the piece.
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, location) {
super(isWhite, "knight", location);
}
}
class Pawn extends Piece {
/**
* Creates a Pawn object with the specified colour and location.
*
* @param {boolean} isWhite - The colour of the piece.
* @param {number[]} location - The location of the piece on the board, represented as an array with exactly two numbers [x, y].
*/
constructor(isWhite, location) {
super(isWhite, "pawn", location);
}
}