-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpiece.js
171 lines (149 loc) · 4.01 KB
/
piece.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
165
166
167
168
169
170
171
const changePosition = (piece, position, castle=false) => {
piece.position = position;
if (piece.rank === 'king') {
if (castle) {
piece.ableToCastle = false;
}
}
if (piece.rank === 'rook') {
piece.ableToCastle = false;
}
}
const getMovesTop = (piece) => {
const movesTop = [];
for (let move = piece.position+10; move <= 88; move+=10) movesTop.push(move);
return movesTop;
}
const getMovesBottom = (piece) => {
const movesBottom = [];
for (let move = piece.position-10; move >= 11 ; move-=10) movesBottom.push(move);
return movesBottom;
}
const getMovesRight = (piece) => {
const num = piece.position+'';
const movesRight = [];
for (let move = piece.position+1; move <= parseInt(num[0]+'8'); move++) movesRight.push(move);
return movesRight;
}
const getMovesLeft = (piece) => {
const num = piece.position+'';
const movesLeft = [];
for (let move = piece.position-1; move >= parseInt(num[0]+'1'); move--) movesLeft.push(move);
return movesLeft;
}
const getMovesTopRight = (piece) => {
const movesTopRight = [];
for (let move = piece.position+11; move <= 88; move+=11) {
const firstDigit = (''+move)[1];
if (firstDigit > 8 || firstDigit < 1) break;
movesTopRight.push(move);
}
return movesTopRight;
}
const getMovesTopLeft = (piece) => {
const movesTopLeft = [];
for (let move = piece.position+9; move <= 88; move+=9) {
const firstDigit = (''+move)[1];
if (firstDigit > 8 || firstDigit < 1) break;
movesTopLeft.push(move);
}
return movesTopLeft;
}
const getMovesBottomRight = (piece) => {
const movesBottomRight = [];
for (let move = piece.position-9; move >= 11 ; move-=9) {
const firstDigit = (''+move)[1];
if (firstDigit > 8 || firstDigit < 1) break;
movesBottomRight.push(move);
}
return movesBottomRight;
}
const getMovesBottomLeft = (piece) => {
const movesBottomLeft = [];
for (let move = piece.position-11; move >= 11 ; move-=11) {
const firstDigit = (''+move)[1];
if (firstDigit > 8 || firstDigit < 1) break;
movesBottomLeft.push(move);
}
return movesBottomLeft;
}
const getPawnAllowedMoves = (pawn) => {
const position = pawn.position;
const mathSign = (pawn.color === 'white') ? 1: -1;
const allowedMoves = [position + mathSign * 10];
if ( (position >20 && position < 29) || (position >70 && position < 79) ) {
allowedMoves.push(position + mathSign * 20);
}
const attackMoves = [position + mathSign * 9, position + mathSign * 11];
return [ attackMoves, allowedMoves ];
}
const getKnightAllowedMoves = (knight) => {
const position = knight.position;
return [
[position + 21],
[position - 21],
[position + 19],
[position - 19],
[position + 12],
[position - 12],
[position + 8],
[position - 8]
];
}
const getKingAllowedMoves = king => {
const position = king.position;
return [
[position + 1],
[position - 1],
[position + 10],
[position - 10],
[position + 11],
[position - 11],
[position + 9],
[position - 9]
];
}
const getBishopAllowedMoves = (bishop) => {
return [ getMovesTopRight(bishop), getMovesTopLeft(bishop), getMovesBottomRight(bishop), getMovesBottomLeft(bishop) ];
}
const getRookAllowedMoves = (rook) => {
return [ getMovesTop(rook), getMovesBottom(rook), getMovesRight(rook), getMovesLeft(rook) ];
}
const getQueenAllowedMoves = queen => {
return [
getMovesTop(queen),
getMovesTopRight(queen),
getMovesTopLeft(queen),
getMovesBottom(queen),
getMovesBottomRight(queen),
getMovesBottomLeft(queen),
getMovesRight(queen),
getMovesLeft(queen)
];
}
const getAllowedMoves = (piece) => {
let allowedMoves;
switch (piece.rank) {
case 'pawn':
allowedMoves = getPawnAllowedMoves(piece);
break;
case 'knight':
allowedMoves = getKnightAllowedMoves(piece);
break;
case 'king':
allowedMoves = getKingAllowedMoves(piece);
break;
case 'bishop':
allowedMoves = getBishopAllowedMoves(piece);
break;
case 'rook':
allowedMoves = getRookAllowedMoves(piece);
break;
case 'queen':
allowedMoves = getQueenAllowedMoves(piece);
break;
default:
throw "Unknown rank: " + piece.type;
}
return allowedMoves;
}