-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreatenHighlightFunctions.cs
276 lines (266 loc) · 10.3 KB
/
ThreatenHighlightFunctions.cs
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
274
275
276
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chess
{
class ThreatenHighlightFunctions
{
UtilityFunctions uf = new UtilityFunctions();
public void highlightThreatened(Piece piece, int row, int col, List<int[]> threatened, List<int[]> enPassant, Piece[,] board, bool filtered = true)
{
switch (piece.PieceType)
{
case (Piece.Type.Pawn):
pawnThreatenHighlight(piece, row, col, threatened, board, enPassant);
break;
case (Piece.Type.Rook):
rookThreatenHighlight(piece.PieceColour, row, col, threatened, board);
break;
case (Piece.Type.Knight):
knightThreatenHighlight(piece.PieceColour, row, col, threatened);
break;
case (Piece.Type.Bishop):
bishopThreatenHighlight(piece.PieceColour, row, col, threatened, board);
break;
case (Piece.Type.Queen):
queenThreatenHighlight(piece.PieceColour, row, col, threatened, board);
break;
case (Piece.Type.King):
kingThreatenedHighlight(piece, row, col, threatened);
break;
default:
throw new NotImplementedException("Attempted to move a non existent piece type");
}
//filtered list only squares that can be moved to, ie. not pieces of the same colour, (need to add) or threatened pieces of the other colour for kings
if (filtered)
{
List<int[]> toRemove = new List<int[]>();
foreach (int[] pair in threatened)
if (board[row, col].PieceColour == board[pair[0], pair[1]].PieceColour)
toRemove.Add(pair);
//if (piece.Type == king)
// foreach (int[] pair in threatened)
// if (isThreatened(pair[0], pair[1], uf.OtherColour(piece.Colour), Playfield))
// toRemove.Add(pair);
foreach (int[] pair in toRemove)
threatened.Remove(pair);
}
}
void pawnThreatenHighlight(Piece piece, int row, int col, List<int[]> threatened, Piece[,] board, List<int[]> enPassant)
{
//capturing
if (piece.PieceColour == Piece.Colour.Black)
{
addThreatened(piece.PieceColour, piece.Row, piece.Col, 1, 1, threatened);
addThreatened(piece.PieceColour, piece.Row, piece.Col, 1, -1, threatened);
}
if (piece.PieceColour == Piece.Colour.White)
{
addThreatened(piece.PieceColour, piece.Row, piece.Col, -1, 1, threatened);
addThreatened(piece.PieceColour, piece.Row, piece.Col, -1, -1, threatened);
}
//en passant
if (piece.PieceColour == Piece.Colour.Black)
{
addEnPassant(piece.PieceColour, piece.Row, piece.Col, 1, 1, enPassant, board);
addEnPassant(piece.PieceColour, piece.Row, piece.Col, 1, -1, enPassant, board);
}
if (piece.PieceColour == Piece.Colour.White)
{
addEnPassant(piece.PieceColour, piece.Row, piece.Col, -1, 1, enPassant, board);
addEnPassant(piece.PieceColour, piece.Row, piece.Col, -1, -1, enPassant, board);
}
}
void rookThreatenHighlight(Piece.Colour colour, int row, int col, List<int[]> threatened, Piece[,] board)
{
int i = 1;
//check down
while (addThreatened(colour, row, col, i, 0, threatened))
{
if (board[row + i, col].Exists)
break;
i++;
}
//check up
i = 1;
while (addThreatened(colour, row, col, -i, 0, threatened))
{
if (board[row - i, col].Exists)
break;
i++;
}
//check right
i = 1;
while (addThreatened(colour, row, col, 0, i, threatened))
{
if (board[row, col + i].Exists)
break;
i++;
}
//check left
i = 1;
while (addThreatened(colour, row, col, 0, -i, threatened))
{
if (board[row, col - i].Exists)
break;
i++;
}
}
void knightThreatenHighlight(Piece.Colour colour, int row, int col, List<int[]> threatened)
{
//check down right
addThreatened(colour, row, col, 2, 1, threatened);
//check right down
addThreatened(colour, row, col, 1, 2, threatened);
//check right up
addThreatened(colour, row, col, -1, 2, threatened);
//check up right
addThreatened(colour, row, col, -2, 1, threatened);
//check up left
addThreatened(colour, row, col, -2, -1, threatened);
//check left up
addThreatened(colour, row, col, -1, -2, threatened);
//check left down
addThreatened(colour, row, col, 1, -2, threatened);
//check down left
addThreatened(colour, row, col, 2, -1, threatened);
}
void bishopThreatenHighlight(Piece.Colour colour, int row, int col, List<int[]> threatened, Piece[,] board)
{
int i = 1;
//check down right
while (addThreatened(colour, row, col, i, i, threatened))
{
if (board[row + i, col + i].Exists)
break;
i++;
}
//check up right
i = 1;
while (addThreatened(colour, row, col, -i, i, threatened))
{
if (board[row - i, col + i].Exists)
break;
i++;
}
//check up left
i = 1;
while (addThreatened(colour, row, col, -i, -i, threatened))
{
if (board[row - i, col - i].Exists)
break;
i++;
}
//check down left
i = 1;
while (addThreatened(colour, row, col, i, -i, threatened))
{
if (board[row + i, col - i].Exists)
break;
i++;
}
}
void queenThreatenHighlight(Piece.Colour colour, int row, int col, List<int[]> threatened, Piece[,] board)
{
int i = 1;
//check down
while (addThreatened(colour, row, col, i, 0, threatened))
{
if (board[row + i, col].Exists)
break;
i++;
}
//check up
i = 1;
while (addThreatened(colour, row, col, -i, 0, threatened))
{
if (board[row - i, col].Exists)
break;
i++;
}
//check right
i = 1;
while (addThreatened(colour, row, col, 0, i, threatened))
{
if (board[row, col + i].Exists)
break;
i++;
}
//check left
i = 1;
while (addThreatened(colour, row, col, 0, -i, threatened))
{
if (board[row, col - i].Exists)
break;
i++;
}
i = 1;
//check down right
while (addThreatened(colour, row, col, i, i, threatened))
{
if (board[row + i, col + i].Exists)
break;
i++;
}
//check up right
i = 1;
while (addThreatened(colour, row, col, -i, i, threatened))
{
if (board[row - i, col + i].Exists)
break;
i++;
}
//check up left
i = 1;
while (addThreatened(colour, row, col, -i, -i, threatened))
{
if (board[row - i, col - i].Exists)
break;
i++;
}
//check down left
i = 1;
while (addThreatened(colour, row, col, i, -i, threatened))
{
if (board[row + i, col - i].Exists)
break;
i++;
}
}
void kingThreatenedHighlight(Piece piece, int row, int col, List<int[]> threatened)
{
addThreatened(piece.PieceColour, row, col, 1, 0, threatened);
addThreatened(piece.PieceColour, row, col, 1, 1, threatened);
addThreatened(piece.PieceColour, row, col, 0, 1, threatened);
addThreatened(piece.PieceColour, row, col, -1, 1, threatened);
addThreatened(piece.PieceColour, row, col, -1, 0, threatened);
addThreatened(piece.PieceColour, row, col, -1, -1, threatened);
addThreatened(piece.PieceColour, row, col, 0, -1, threatened);
addThreatened(piece.PieceColour, row, col, 1, -1, threatened);
}
bool addThreatened(Piece.Colour colour, int row, int col, int rowOffset, int colOffset, List<int[]> threatened)
{
bool added = false;
int destinationRow = row + rowOffset;
int destinationCol = col + colOffset;
if (uf.IsInBounds(destinationRow, destinationCol))
{
threatened.Add(new int[] { destinationRow, destinationCol });
added = true;
}
return added;
}
void addEnPassant(Piece.Colour colour, int row, int col, int rowOffset, int colOffset, List<int[]> enPassant, Piece[,] board)
{
int destinationRow = row + rowOffset;
int destinationCol = col + colOffset;
if (destinationRow >= Board.FirstRow && destinationRow <= Board.LastRow && destinationCol >= Board.FirstCol
&& destinationCol <= Board.LastCol && board[row, col + colOffset].EnPassant)
{
enPassant.Add(new int[] { destinationRow, destinationCol });
}
}
}
}