-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pieces.cs
386 lines (384 loc) · 16.8 KB
/
Pieces.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChessNN
{
/// <summary>
/// An abstract class from which the pieces are derrived
///
/// Once the neurons are weighted properly, CVal can be regressed back to posVals
/// For now, though, weighting takes the priority
/// ...
/// Probably...
/// It may be encapsulated in the weights... I don't know yet.
/// </summary>
[Serializable]
public abstract class Piece
{
public string Name { get; set; }
public int PosX { get; set; }
public int PosY { get; set; }
public int LegalX { get; set; }
public int LegalY { get; set; }
public int CVal { get; set; }
public Player Player { get; set; }
public abstract Board Move(Board b, int toX, int toY);
}
/// <summary>
/// No bugs known
/// </summary>
[Serializable]
public class Pawn : Piece
{
bool enPass, twoStep;
public Pawn(Player player, int posX, int posY)
{
Name = "Pawn"; PosX = posX; PosY = posY; Player = player; twoStep = true; enPass = false;
if (player.IsW == true) { LegalX = -1; }
else { LegalX = 1; }
CVal = Data.ReadPiece("Pawn");
}
public override Board Move(Board b, int toX, int toY)
{
Board board = GoDiePointers.DeepClone(b);
bool move = true;
if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
{
//standard move
if (toX == PosX + LegalX && toY == PosY && toX <= 7 && toY <= 7 && board.Pieces[toX, toY] is Empty)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
move = false; enPass = false; twoStep = false;
board.WTurn = !board.WTurn;
}
//capture
if (!(board.Pieces[toX, toY] is Empty) && move && (toY == PosY + 1 || toY == PosY - 1) && toX == PosX + LegalX && toX <= 7 && toY <= 7 &&
board.Pieces[toX, toY].Player.IsW != Player.IsW)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
move = false; enPass = false; twoStep = false;
board.WTurn = !board.WTurn;
}
//twostep
if (board.Pieces[toX, toY] is Empty && twoStep == true && toX == PosX + (2 * LegalX) && toY == PosY && toX <= 7 && toY <= 7 && move)
{
if (board.Pieces[PosX + LegalX, PosY] is Empty)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
twoStep = false; move = false; enPass = true;
board.WTurn = !board.WTurn;
}
}
//enpass
if ((toY == PosY + 1 || toY == PosY - 1) && toX == PosX + LegalX && toX <= 7 && toY <= 7 && board.Pieces[toX - LegalX, toY] is Pawn &&
((Pawn)board.Pieces[toX - LegalX, toY]).enPass && move && board.Pieces[toX - LegalX, toY].Player.IsW != Player.IsW && board.Pieces[toX, toY] is Empty)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
board.Pieces.SetValue(new Empty(toX - LegalX, toY), new int[] { toX - LegalX, toY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
move = false; twoStep = false;
board.WTurn = !board.WTurn;
}
if (move) { throw new Exception("Failure of pawn move"); }
}
else { throw new Exception("Failure of pawn move"); }
//Promotion
if (PosX == 7 || PosX == 0)
{ board.Pieces.SetValue(new Queen(Player, PosX, PosY), new int[] { PosX, PosY }); }
return board;
}
}
/// <summary>
/// No bugs known
/// </summary>
[Serializable]
class Rook : Piece
{
public new int LegalX = 7, LegalY = 7; public bool CanCastle = true;
public Rook(Player player, int posX, int posY)
{
Player = player; PosX = posX; PosY = posY; Name = "Rook";
CVal = Data.ReadPiece("Rook");
}
public override Board Move(Board b, int toX, int toY)
{
Board board = GoDiePointers.DeepClone(b);
if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
{
bool throughX = false, throughY = false;
if (toX != PosX)
{
for (int i = 1; i < Math.Abs(PosX - toX); i = Math.Abs(i) + 1)
{
if (toX < PosX) { i = i * -1; }
if (!(board.Pieces[PosX + i, toY] is Empty)) { throughX = true; break; }
}
}
if (toY != PosY)
{
for (int i = 1; i < Math.Abs(PosY - toY); i = Math.Abs(i) + 1)
{
if (toY < PosY) { i = i * -1; }
if (!(board.Pieces[toX, PosY + i] is Empty)) { throughY = true; break; }
}
}
//Shift the legal + to outside of the loop for efficiency?
if (!throughX && !throughY && ((toX <= LegalX && toY == PosY) || (toY <= LegalY && toX == PosX)))
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { toX, toY });
CanCastle = false;
board.WTurn = !board.WTurn;
}
else { throw new Exception("Failure of rook move"); }
if (throughX || throughY) { throw new Exception("Rook can't move through pieces"); }
}
else { throw new Exception("Rook can't move on own pieces"); }
return board;
}
}
/// <summary>
/// No bugs known
/// </summary>
[Serializable]
class Knight : Piece
{
public new int LegalX = 2, LegalY = 2;
public Knight(Player player, int posX, int posY)
{
Player = player; PosX = posX; PosY = posY; Name = "Knight";
CVal = 2;
}
public override Board Move(Board b, int toX, int toY)
{
Board board = GoDiePointers.DeepClone(b);
if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
{
bool L = false;
if (Math.Abs(PosX - toX) + Math.Abs(PosY - toY) == 3) { L = true; }
if (L && toX <= 7 && toY <= 7)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
board.WTurn = !board.WTurn;
}
else { throw new Exception("Failure of knight move"); }
}
else { throw new Exception("Knight can't move on own pieces"); }
return board;
}
}
/// <summary>
/// No bugs known
/// </summary>
[Serializable]
class Bishop : Piece
{
//Should use legalx and legaly in the initializer to make move easier
public new int LegalX = 7, LegalY = 7;
public Bishop(Player player, int posX, int posY)
{
Player = player; PosX = posX; PosY = posY; Name = "Bishop";
CVal = Data.ReadPiece("Bishop");
}
public override Board Move(Board b, int toX, int toY)
{
//Unecessary?
for (int i = -7; i <= 7; i++)
{
if (PosX + i == toX && PosY + i == toY) { break; }
if (PosX - i == toX && PosY + i == toY) { break; }
if (i == 7)
{ throw new Exception("Failure of bishop move"); }
}
Board board = GoDiePointers.DeepClone(b);
if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
{
bool throughPiece = false;
int xFactor = -1; int yFactor = -1;
if (PosX < toX) { xFactor = 1; }
if (PosY < toY) { yFactor = 1; }
if ((Math.Abs(PosX - toX) + Math.Abs(PosY - toY)) % 2 == 0 && toX <= 7 && toY <= 7)
{
for (int i = 1; i <= ((Math.Abs(PosX - toX) + Math.Abs(PosY - toY)) / 2) - 1; i = Math.Abs(i) + 1)
{
int ii = GoDiePointers.DeepClone(i);
i = i * xFactor;
ii = ii * yFactor;
if (!(board.Pieces[PosX + i, PosY + ii] is Empty))
{ throughPiece = true; }
}
}
else { throughPiece = true; throw new Exception("Failure of bishop move"); }
if (throughPiece) { throw new Exception("Can't move through pieces"); }
if ((Math.Abs(PosX - toX) + (PosY - toY)) % 2 == 0 && toX <= 7 && toY <= 7 && !throughPiece)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
board.WTurn = !board.WTurn;
}
else { throw new Exception("Failure of bishop move"); }
}
else { throw new Exception("Bishop can't move on own pieces"); }
return board;
}
}
/// <summary>
/// No bugs known
/// </summary>
[Serializable]
class Queen : Piece
{
public new int LegalX = 7, LegalY = 7;
public Queen(Player player, int posX, int posY)
{
Player = player; PosX = posX; PosY = posY; Name = "Queen";
CVal = Data.ReadPiece("Queen");
}
public override Board Move(Board b, int toX, int toY)
{
bool rMove = true; bool bMove = true;
Board board = GoDiePointers.DeepClone(b);
try
{
Rook Qrook = new Rook(Player, PosX, PosY);
board = Qrook.Move(board, toX, toY);
}
catch
{
rMove = false;
try
{
Bishop Qbish = new Bishop(Player, PosX, PosY);
board = Qbish.Move(board, toX, toY);
}
catch { bMove = false; }
}
finally
{
board = GoDiePointers.DeepClone(b);
if (rMove)
{
board.Pieces[PosX, PosY] = new Empty(PosX, PosY);
board.Pieces[toX, toY] = new Queen(Player, toX, toY);
}
else
{
if (bMove)
{
board.Pieces[PosX, PosY] = new Empty(PosX, PosY);
board.Pieces[toX, toY] = new Queen(Player, toX, toY);
}
else { throw new Exception("QMove failure"); }
}
}
board.WTurn = !board.WTurn;
return board;
}
}
/// <summary>
/// No bugs known
/// </summary>
[Serializable]
class King : Piece
{
public new int LegalX = 1, LegalY = 1; public bool CanCastle = true;
public King(Player player, int posX, int posY)
{
Player = player; PosX = posX; PosY = posY; Name = "king"; LegalX = 1; LegalY = 1; CanCastle = true;
CVal = 9999;
}
public override Board Move(Board b, int toX, int toY)
{
Board board = GoDiePointers.DeepClone(b);
if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
{
//Castling
//Does not work in the traditional way where you can't castle when in danger
if (CanCastle && toX == PosX && toY == 2 || toY == 6)
{
if (toY == 2)
{
if (board.Pieces[PosX, toY - 2] is Rook && ((Rook)board.Pieces[PosX, toY - 2]).CanCastle)
{
if (board.Pieces[PosX, toY + 1] is Empty && board.Pieces[PosX, toY - 1] is Empty && board.Pieces[PosX, toY] is Empty)
{
board.Pieces.SetValue(new Empty(PosX, toY - 2), new int[] { PosX, toY - 2 }); //Rook
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY }); //King
board.Pieces.SetValue(new King(Player, PosX, toY), new int[] { PosX, toY }); //King
board.Pieces.SetValue(new Rook(Player, PosX, toY + 1), new int[] { PosX, toY + 1 }); //Rook
board.WTurn = !board.WTurn;
return board;
}
else
{
throw new Exception("Can't move through pieces");
}
}
else { throw new Exception("The rook can't castle"); }
}
if (toY == 6)
{
if (board.Pieces[PosX, toY + 1] is Rook && ((Rook)board.Pieces[PosX, toY + 1]).CanCastle)
{
if (board.Pieces[PosX, toY - 1] is Empty && board.Pieces[PosX, toY] is Empty)
{
board.Pieces.SetValue(new Empty(PosX, toY + 1), new int[] { PosX, toY + 1 }); //Rook
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY }); //King
board.Pieces.SetValue(new King(Player, PosX, toY), new int[] { PosX, toY }); //King
board.Pieces.SetValue(new Rook(Player, PosX, toY - 1), new int[] { PosX, toY - 1 }); //Rook
board.WTurn = !board.WTurn;
return board;
}
else
{
throw new Exception("Can't move through pieces");
}
}
else { throw new Exception("The rook can't castle"); }
}
}
if ((toX == PosX + LegalX || toX == PosX - LegalX || toX == PosX) && (toY == PosY + LegalY || toY == PosY - LegalY || toY == PosY) && toX <= 7 && toY <= 7)
{
board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
PosX = toX; PosY = toY;
board.Pieces.SetValue(this, new int[] { PosX, PosY });
CanCastle = false;
board.WTurn = !board.WTurn;
}
else { throw new Exception("Failure of king move"); }
}
else { throw new Exception("Failure of king move"); }
return board;
}
}
/// <summary>
/// Will f*** you up if you forget it DOES NOT HAVE A PLAYER!
/// Usually occurs when verifying the isW parameter
/// </summary>
[Serializable]
class Empty : Piece
{
public Empty(int posX, int posY)
{
PosX = posX; PosY = posY; Name = ".empty";
CVal = Data.ReadPiece("Empty");
}
public override Board Move(Board board, int toX, int toY)
{
throw new Exception("Can't move nothing");
}
}
}