-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
273 lines (246 loc) · 12.1 KB
/
engine.js
File metadata and controls
273 lines (246 loc) · 12.1 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// get all posible moves in a position for a certain color
function getMoves(position, pieceType = "w", forcedCaptures = false, canCaptureBackwards = false, flyingKing = false, maxCaptures = false) {
let hasToCapture = false
let moves = []
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 8; x++) {
if ((pieceType === "b" && position[y][x] === "b") || (pieceType === "w" && position[y][x] === "w")) {
const res = getPeasantMoves(position, x, y, pieceType, hasToCapture, forcedCaptures, canCaptureBackwards)
if (res.hasToCapture && !hasToCapture) {
moves = []
hasToCapture = true
}
moves = moves.concat(res.moves);
} else if ((pieceType === "b" && position[y][x] === "B") || (pieceType === "w" && position[y][x] === "W")) {
const res = getKingMoves(position, x, y, pieceType, hasToCapture, forcedCaptures, flyingKing)
if (res.hasToCapture && !hasToCapture) {
moves = []
hasToCapture = true
}
moves = moves.concat(res.moves)
}
}
}
if(maxCaptures) moves = getMaxCaptures(moves)
return { "moves": moves, "hasToCapture": hasToCapture }
}
// get all moves for piece at a certain position
function getPieceMoves(position, x, y, hasToCapture = false, forcedCaptures = false, canCaptureBackwards = false, flyingKing = false, maxCaptures = false){
let moves = []
if (position[y][x] === "b" || position[y][x] === "w") {
const res = getPeasantMoves(position, x, y, position[y][x], hasToCapture, forcedCaptures, canCaptureBackwards)
if (res.hasToCapture && !hasToCapture) {
moves = []
hasToCapture = true
}
moves = moves.concat(res.moves);
} else if (position[y][x] === "W") {
const res = getKingMoves(position, x, y, "w", hasToCapture, forcedCaptures, flyingKing)
if (res.hasToCapture && !hasToCapture) {
moves = []
hasToCapture = true
}
moves = moves.concat(res.moves)
} else if (position[y][x] === "B") {
const res = getKingMoves(position, x, y, "b", hasToCapture, forcedCaptures, flyingKing)
if (res.hasToCapture && !hasToCapture) {
moves = []
hasToCapture = true
}
moves = moves.concat(res.moves)
}
if(maxCaptures) moves = getMaxCaptures(moves)
return {"moves": moves, "hasToCapture": hasToCapture}
}
// gets all peasant moves at a position
function getPeasantMoves(position, x, y, pieceType = "w", hasToCapture = false, forcedCaptures = false, canCaptureBackwards = false) {
let originalPiece = position[y][x]
position[y][x] = "*"
let moves = getPieceCaptures(position, x, y, pieceType, [{"x": x, "y": y, "originalPiece": originalPiece}], canCaptureBackwards)
if (moves.length !== 0) {
hasToCapture = forcedCaptures
}
if (!hasToCapture) {
// white moves forward, black moves back
let forwardY = pieceType === "b" ? y + 1 : y - 1
if (x - 1 >= 0 && forwardY >= 0 && forwardY < 8 && position[forwardY][x - 1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x - 1, "y": forwardY, "originalPiece": "*"}])
}
if (x + 1 < 8 && forwardY >= 0 && forwardY < 8 && position[forwardY][x + 1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x + 1, "y": forwardY, "originalPiece": "*"}])
}
}
position[y][x] = originalPiece
return {"moves": moves, "hasToCapture": hasToCapture}
function getPieceCaptures(pos, x1, y1, pieceType, capture) {
let capturesRes = []
let directions = [-1, 1]
let backwardDirection = canCaptureBackwards ? [-1, 1] : [pieceType === "b" ? 1 : -1]
for (let dy of backwardDirection) {
for (let dx of directions) {
let nextX = x1 + (2 * dx)
let nextY = y1 + (2 * dy)
let enemyX = x1 + dx
let enemyY = y1 + dy
if (nextX >= 0 && nextX < 8 && nextY >= 0 && nextY < 8 &&
// can capture only if the square is a piece of the opposing color and the square after that is empty
((pieceType === "b" && (pos[enemyY][enemyX] === "w" || pos[enemyY][enemyX] === "W")) ||
(pieceType === "w" && (pos[enemyY][enemyX] === "b" || pos[enemyY][enemyX] === "B"))) &&
pos[nextY][nextX] === "*") {
let capturedPiece = pos[enemyY][enemyX]
pos[enemyY][enemyX] = "*"
let newCapture = capture.concat([{"x": enemyX, "y": enemyY, "originalPiece": capturedPiece}, {"x": nextX, "y": nextY, "originalPiece": "*"}])
// check further captures
let search = getPieceCaptures(pos, nextX, nextY, pieceType, newCapture)
if (search.length === 0) capturesRes.push(newCapture)
else {
if (!forcedCaptures) capturesRes.push(newCapture)
for (let i = 0; i < search.length; i++) capturesRes.push(search[i])
}
pos[enemyY][enemyX] = capturedPiece
}
}
}
return capturesRes
}
}
// gets all king moves at a position
function getKingMoves(position, x, y, pieceType = "w", hasToCapture = false, forcedCaptures = false, flyingKing = false) {
let originalPiece = position[y][x]
position[y][x] = "*"
let moves = getKingCaptures(position, x, y, pieceType, [{"x": x, "y": y, "originalPiece": originalPiece}])
if (moves.length !== 0) {
hasToCapture = forcedCaptures
}
if (!hasToCapture) {
if(flyingKing) {
const directions = [
{ dx: 1, dy: 1 },
{ dx: 1, dy: -1 },
{ dx: -1, dy: 1 },
{ dx: -1, dy: -1 }
]
for (let direction of directions) {
let x1 = x + direction.dx
let y1 = y + direction.dy
while (x1 >= 0 && x1 < 8 && y1 >= 0 && y1 < 8 && position[y1][x1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x1, "y": y1, "originalPiece": "*"}])
x1 += direction.dx
y1 += direction.dy
}
}
}
else {
if (x - 1 >= 0 && y - 1 >= 0 && position[y - 1][x - 1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x - 1, "y": y - 1, "originalPiece": "*"}])
}
if (x + 1 < 8 && y - 1 >= 0 && position[y - 1][x + 1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x + 1, "y": y - 1, "originalPiece": "*"}])
}
if (x - 1 >= 0 && y + 1 < 8 && position[y + 1][x - 1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x - 1, "y": y + 1, "originalPiece": "*"}])
}
if (x + 1 < 8 && y + 1 < 8 && position[y + 1][x + 1] === "*") {
moves.push([{"x": x, "y": y, "originalPiece": originalPiece}, {"x": x + 1, "y": y + 1, "originalPiece": "*"}])
}
}
}
position[y][x] = originalPiece
return {"moves": moves, "hasToCapture": hasToCapture}
function getKingCaptures(pos, x1, y1, pieceType, capture) {
let capturesRes = []
const directions = [
{ dx: 1, dy: 1 },
{ dx: 1, dy: -1 },
{ dx: -1, dy: 1 },
{ dx: -1, dy: -1 }
]
if(flyingKing){
for (let direction of directions) {
let x = x1 + direction.dx
let y = y1 + direction.dy
while (x >= 0 && x < 8 && y >= 0 && y < 8) {
// can capture only if the square is a piece of the opposing color and the square after that is empty
if ((pieceType === "b" && (pos[y][x] === "w" || pos[y][x] === "W")) ||
(pieceType === "w" && (pos[y][x] === "b" || pos[y][x] === "B"))) {
let capturedPiece = pos[y][x]
let nx = x + direction.dx
let ny = y + direction.dy
while (nx >= 0 && nx < 8 && ny >= 0 && ny < 8 && pos[ny][nx] === "*") {
let newCapture = capture.concat([{ "x": x, "y": y, "originalPiece": capturedPiece }, { "x": nx, "y": ny, "originalPiece": "*" }])
pos[y][x] = "*"
// check further captures
let search = getKingCaptures(pos, nx, ny, pieceType, newCapture)
if (search.length === 0) capturesRes.push(newCapture)
else {
if (!forcedCaptures) capturesRes.push(newCapture)
for (let i = 0; i < search.length; i++) capturesRes.push(search[i])
}
nx += direction.dx
ny += direction.dy
}
pos[y][x] = capturedPiece
break
}
x += direction.dx
y += direction.dy
}
}
}
else {
for (let direction of directions) {
let nextX = x1 + (2 * direction.dx)
let nextY = y1 + (2 * direction.dy)
let enemyX = x1 + direction.dx
let enemyY = y1 + direction.dy
// can capture only if the square is a piece of the opposing color and the square after that is empty
if (nextX >= 0 && nextX < 8 && nextY >= 0 && nextY < 8 &&
((pieceType === "b" && (pos[enemyY][enemyX] === "w" || pos[enemyY][enemyX] === "W")) ||
(pieceType === "w" && (pos[enemyY][enemyX] === "b" || pos[enemyY][enemyX] === "B"))) &&
pos[nextY][nextX] === "*") {
let capturedPiece = pos[enemyY][enemyX]
pos[enemyY][enemyX] = "*"
let newCapture = capture.concat([{"x": enemyX, "y": enemyY, "originalPiece": capturedPiece}, {"x": nextX, "y": nextY, "originalPiece": "*"}])
// check further captures
let search = getKingCaptures(pos, nextX, nextY, pieceType, newCapture)
if (search.length === 0) capturesRes.push(newCapture)
else {
if (!forcedCaptures) capturesRes.push(newCapture)
for (let i = 0; i < search.length; i++) capturesRes.push(search[i])
}
pos[enemyY][enemyX] = capturedPiece
}
}
}
return capturesRes
}
}
function getMaxCaptures(moves) {
let resMoves = []
let maxSize = 2
for (let move of moves) {
if(move.length === maxSize) resMoves.push(move)
else if(move.length > maxSize) {
resMoves = [move]
maxSize = move.length
}
}
return resMoves
}
function makeMove(pos, move) {
let lastElementIndex = move.length - 1
if(move[lastElementIndex]["y"] === 7 && move[0]["originalPiece"] === "b") pos[move[lastElementIndex]["y"]][move[lastElementIndex]["x"]] = "B"
else if(move[lastElementIndex]["y"] === 0 && move[0]["originalPiece"] === "w") pos[move[lastElementIndex]["y"]][move[lastElementIndex]["x"]] = "W"
else pos[move[lastElementIndex]["y"]][move[lastElementIndex]["x"]] = move[0]["originalPiece"]
for(let i = 0; i < lastElementIndex; i++){
pos[move[i]["y"]][move[i]["x"]] = "*"
}
return pos
}
function unmakeMove(pos, move) {
for(let i = 0; i < move.length; i++){
pos[move[i]["y"]][move[i]["x"]] = move[i]["originalPiece"]
}
return pos
}
module.exports = { getMoves, getPeasantMoves, getKingMoves, getPieceMoves, makeMove, unmakeMove }