@@ 5-191 (lines=187) @@ | ||
2 | * A Chess board module |
|
3 | * @module |
|
4 | */ |
|
5 | "use strict"; |
|
6 | ||
7 | /** |
|
8 | * Board class contains methods for setting up a chess board |
|
9 | */ |
|
10 | class Board { |
|
11 | /** |
|
12 | * Prepare board properties and init empty board |
|
13 | */ |
|
14 | constructor() { |
|
15 | this.board = { |
|
16 | "A": [], |
|
17 | "B": [], |
|
18 | "C": [], |
|
19 | "D": [], |
|
20 | "E": [], |
|
21 | "F": [], |
|
22 | "G": [], |
|
23 | "H": [], |
|
24 | }; |
|
25 | ||
26 | this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"]; |
|
27 | ||
28 | this.setUpEmptyTable(); |
|
29 | } |
|
30 | ||
31 | ||
32 | /** |
|
33 | * Set up empty board |
|
34 | */ |
|
35 | setUpEmptyTable() { |
|
36 | for (var key in this.board) { |
|
37 | this.board[key] = |
|
38 | [key, key + 1, key + 2, key + 3, key + 4, key + 5, key + 6, key + 7, key + 8]; |
|
39 | } |
|
40 | } |
|
41 | ||
42 | ||
43 | /** |
|
44 | * get the full board |
|
45 | * |
|
46 | * @returns {object} - current board state |
|
47 | */ |
|
48 | getBoard() { |
|
49 | return this.board; |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * Check if the is anything blocking the path between 2 squares |
|
54 | * |
|
55 | * @returns {mixed} - true if free or square Coo if false |
|
56 | */ |
|
57 | checkRow(x, y, nx, ny) { |
|
58 | const direction = x == nx ? "horisontal" : "vertical"; |
|
59 | let row, list, xNumber, nxNumber, i; |
|
60 | ||
61 | if (direction === "horisontal") { |
|
62 | row = this.board[x]; |
|
63 | // Slice array to get squares to check |
|
64 | list = y < ny ? row.slice(y + 1, ny) : row.slice(ny + 1, y); |
|
65 | } else { |
|
66 | row = []; |
|
67 | for (i = 1; i < this.rows.length; i++) { |
|
68 | row.push(this.board[this.rows[i]][y]); |
|
69 | } |
|
70 | // get numeric value of x nx |
|
71 | xNumber = this.rows.indexOf(x); |
|
72 | nxNumber = this.rows.indexOf(nx); |
|
73 | ||
74 | // Slice array to get squares to check |
|
75 | list = xNumber < nxNumber ? row.slice(xNumber + 1, nxNumber) : row.slice(nxNumber + 1, xNumber); |
|
76 | } |
|
77 | ||
78 | // loop squares and check type |
|
79 | for (i = 0; i < list.length; i++) { |
|
80 | if (typeof list[i] !== "string") { |
|
81 | return false; |
|
82 | } |
|
83 | } |
|
84 | ||
85 | return true; |
|
86 | } |
|
87 | ||
88 | /** |
|
89 | * Check if the is anything blocking the path between 2 squares |
|
90 | * |
|
91 | * @returns {bool} - true if free else false |
|
92 | */ |
|
93 | checkDiagonal(x, y, nx, ny) { |
|
94 | let xNumber, nxNumber, steps; |
|
95 | steps = Math.abs(y - ny); |
|
96 | xNumber = this.rows.indexOf(x) |
|
97 | nxNumber = this.rows.indexOf(nx) |
|
98 | ||
99 | if (xNumber < nxNumber && y < ny || xNumber > nxNumber && y > ny) { |
|
100 | y = y > ny ? ny : y; |
|
101 | xNumber = xNumber > nxNumber ? nxNumber : xNumber; |
|
102 | for (let i = 1; i < steps; i++) { |
|
103 | if (typeof this.board[this.rows[xNumber + i]][y + i] == "object") { |
|
104 | return false |
|
105 | } |
|
106 | } |
|
107 | } else { |
|
108 | y = y < ny ? ny : y; |
|
109 | xNumber = xNumber > nxNumber ? nxNumber : xNumber; |
|
110 | for (let i = 1; i < steps; i++) { |
|
111 | if (typeof this.board[this.rows[xNumber + i]][y - i] == "object") { |
|
112 | return false |
|
113 | } |
|
114 | } |
|
115 | } |
|
116 | return true |
|
117 | } |
|
118 | ||
119 | /** |
|
120 | * get a value for a specific square |
|
121 | * |
|
122 | * @returns {string} |
|
123 | */ |
|
124 | getSquare(row, col) { |
|
125 | return this.board[row][col]; |
|
126 | } |
|
127 | ||
128 | ||
129 | /** |
|
130 | * change the value of a square |
|
131 | * |
|
132 | * @returns {null} |
|
133 | */ |
|
134 | updateSquare(row, col, value) { |
|
135 | this.board[row][col] = value; |
|
136 | } |
|
137 | ||
138 | ||
139 | /** |
|
140 | * Move a piece to another square |
|
141 | * @param {int} - x, y, nx, ny |
|
142 | * old xy coordinates and new xy coordinates |
|
143 | * |
|
144 | */ |
|
145 | move(x, y, nx, ny) { |
|
146 | const piece = this.getSquare(x, y); |
|
147 | ||
148 | this.updateSquare(x, y, x + y); |
|
149 | this.updateSquare(nx, ny, piece); |
|
150 | } |
|
151 | ||
152 | ||
153 | /** |
|
154 | * Check that destination is no of the same color as mover |
|
155 | * @return {bool} |
|
156 | */ |
|
157 | checkDestination(turn, x, y) { |
|
158 | const piece = this.getSquare(x, y); |
|
159 | ||
160 | if (turn === piece.color) { |
|
161 | return false; |
|
162 | } |
|
163 | ||
164 | return true; |
|
165 | } |
|
166 | ||
167 | ||
168 | /** |
|
169 | * Asciify board |
|
170 | * |
|
171 | * @returns {String} |
|
172 | */ |
|
173 | getAsciiBoard() { |
|
174 | let ascii = ""; |
|
175 | ||
176 | for (var key in this.board) { |
|
177 | for (let i = 0; i < this.board[key].length; i++) { |
|
178 | if (typeof this.board[key][i] === "object") { |
|
179 | ascii += this.board[key][i].symbol + this.board[key][i].color[0] + " | " |
|
180 | } else { |
|
181 | ascii += this.board[key][i] + " | "; |
|
182 | } |
|
183 | } |
|
184 | ascii += "\n"; |
|
185 | } |
|
186 | return ascii; |
|
187 | } |
|
188 | } |
|
189 | ||
190 | ||
191 | module.exports = Board; |
|
192 |
@@ 5-191 (lines=187) @@ | ||
2 | * A Chess board module |
|
3 | * @module |
|
4 | */ |
|
5 | "use strict"; |
|
6 | ||
7 | /** |
|
8 | * Board class contains methods for setting up a chess board |
|
9 | */ |
|
10 | class Board { |
|
11 | /** |
|
12 | * Prepare board properties and init empty board |
|
13 | */ |
|
14 | constructor() { |
|
15 | this.board = { |
|
16 | "A": [], |
|
17 | "B": [], |
|
18 | "C": [], |
|
19 | "D": [], |
|
20 | "E": [], |
|
21 | "F": [], |
|
22 | "G": [], |
|
23 | "H": [], |
|
24 | }; |
|
25 | ||
26 | this.rows = [null, "A", "B", "C", "D", "E", "F", "G", "H"]; |
|
27 | ||
28 | this.setUpEmptyTable(); |
|
29 | } |
|
30 | ||
31 | ||
32 | /** |
|
33 | * Set up empty board |
|
34 | */ |
|
35 | setUpEmptyTable() { |
|
36 | for (var key in this.board) { |
|
37 | this.board[key] = |
|
38 | [key, key + 1, key + 2, key + 3, key + 4, key + 5, key + 6, key + 7, key + 8]; |
|
39 | } |
|
40 | } |
|
41 | ||
42 | ||
43 | /** |
|
44 | * get the full board |
|
45 | * |
|
46 | * @returns {object} - current board state |
|
47 | */ |
|
48 | getBoard() { |
|
49 | return this.board; |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * Check if the is anything blocking the path between 2 squares |
|
54 | * |
|
55 | * @returns {mixed} - true if free or square Coo if false |
|
56 | */ |
|
57 | checkRow(x, y, nx, ny) { |
|
58 | const direction = x == nx ? "horisontal" : "vertical"; |
|
59 | let row, list, xNumber, nxNumber, i; |
|
60 | ||
61 | if (direction === "horisontal") { |
|
62 | row = this.board[x]; |
|
63 | // Slice array to get squares to check |
|
64 | list = y < ny ? row.slice(y + 1, ny) : row.slice(ny + 1, y); |
|
65 | } else { |
|
66 | row = []; |
|
67 | for (i = 1; i < this.rows.length; i++) { |
|
68 | row.push(this.board[this.rows[i]][y]); |
|
69 | } |
|
70 | // get numeric value of x nx |
|
71 | xNumber = this.rows.indexOf(x); |
|
72 | nxNumber = this.rows.indexOf(nx); |
|
73 | ||
74 | // Slice array to get squares to check |
|
75 | list = xNumber < nxNumber ? row.slice(xNumber + 1, nxNumber) : row.slice(nxNumber + 1, xNumber); |
|
76 | } |
|
77 | ||
78 | // loop squares and check type |
|
79 | for (i = 0; i < list.length; i++) { |
|
80 | if (typeof list[i] !== "string") { |
|
81 | return false; |
|
82 | } |
|
83 | } |
|
84 | ||
85 | return true; |
|
86 | } |
|
87 | ||
88 | /** |
|
89 | * Check if the is anything blocking the path between 2 squares |
|
90 | * |
|
91 | * @returns {bool} - true if free else false |
|
92 | */ |
|
93 | checkDiagonal(x, y, nx, ny) { |
|
94 | let xNumber, nxNumber, steps; |
|
95 | steps = Math.abs(y - ny); |
|
96 | xNumber = this.rows.indexOf(x) |
|
97 | nxNumber = this.rows.indexOf(nx) |
|
98 | ||
99 | if (xNumber < nxNumber && y < ny || xNumber > nxNumber && y > ny) { |
|
100 | y = y > ny ? ny : y; |
|
101 | xNumber = xNumber > nxNumber ? nxNumber : xNumber; |
|
102 | for (let i = 1; i < steps; i++) { |
|
103 | if (typeof this.board[this.rows[xNumber + i]][y + i] == "object") { |
|
104 | return false |
|
105 | } |
|
106 | } |
|
107 | } else { |
|
108 | y = y < ny ? ny : y; |
|
109 | xNumber = xNumber > nxNumber ? nxNumber : xNumber; |
|
110 | for (let i = 1; i < steps; i++) { |
|
111 | if (typeof this.board[this.rows[xNumber + i]][y - i] == "object") { |
|
112 | return false |
|
113 | } |
|
114 | } |
|
115 | } |
|
116 | return true |
|
117 | } |
|
118 | ||
119 | /** |
|
120 | * get a value for a specific square |
|
121 | * |
|
122 | * @returns {string} |
|
123 | */ |
|
124 | getSquare(row, col) { |
|
125 | return this.board[row][col]; |
|
126 | } |
|
127 | ||
128 | ||
129 | /** |
|
130 | * change the value of a square |
|
131 | * |
|
132 | * @returns {null} |
|
133 | */ |
|
134 | updateSquare(row, col, value) { |
|
135 | this.board[row][col] = value; |
|
136 | } |
|
137 | ||
138 | ||
139 | /** |
|
140 | * Move a piece to another square |
|
141 | * @param {int} - x, y, nx, ny |
|
142 | * old xy coordinates and new xy coordinates |
|
143 | * |
|
144 | */ |
|
145 | move(x, y, nx, ny) { |
|
146 | const piece = this.getSquare(x, y); |
|
147 | ||
148 | this.updateSquare(x, y, x + y); |
|
149 | this.updateSquare(nx, ny, piece); |
|
150 | } |
|
151 | ||
152 | ||
153 | /** |
|
154 | * Check that destination is no of the same color as mover |
|
155 | * @return {bool} |
|
156 | */ |
|
157 | checkDestination(turn, x, y) { |
|
158 | const piece = this.getSquare(x, y); |
|
159 | ||
160 | if (turn === piece.color) { |
|
161 | return false; |
|
162 | } |
|
163 | ||
164 | return true; |
|
165 | } |
|
166 | ||
167 | ||
168 | /** |
|
169 | * Asciify board |
|
170 | * |
|
171 | * @returns {String} |
|
172 | */ |
|
173 | getAsciiBoard() { |
|
174 | let ascii = ""; |
|
175 | ||
176 | for (var key in this.board) { |
|
177 | for (let i = 0; i < this.board[key].length; i++) { |
|
178 | if (typeof this.board[key][i] === "object") { |
|
179 | ascii += this.board[key][i].symbol + this.board[key][i].color[0] + " | " |
|
180 | } else { |
|
181 | ascii += this.board[key][i] + " | "; |
|
182 | } |
|
183 | } |
|
184 | ascii += "\n"; |
|
185 | } |
|
186 | return ascii; |
|
187 | } |
|
188 | } |
|
189 | ||
190 | ||
191 | module.exports = Board; |
|
192 |