-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessLogic.cs
452 lines (362 loc) · 14 KB
/
ChessLogic.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using System;
using System.Collections.Generic;
using System.Text;
namespace ChessExample
{
public class ChessLogic
{
internal const int MOVES_TO_DRAW = 50;
internal const int REPEATED_MOVES_TO_DRAW = 3;
private Piece[,] oldBoard;
internal Piece[,] board;
private PieceColor currentTurn;
private bool check;
private List<Piece>[] oldPieces;
internal List<Piece>[] pieces;
private List<Cell[]>[] oldPieceMoveList;
private List<bool>[] oldPieceMoveListGenerated;
private Pawn[,] pawns;
private Rook[,] rooks;
private Knight[,] knights;
private Bishop[,] bishops;
private Queen[] queens;
private King[] kings;
private bool gameOver;
private bool draw;
private PieceColor winner;
internal bool pawnWalkedTwoRows;
internal int pawnCol;
internal Pawn pawnToPromote;
internal int movesToDraw;
internal int repeatedMoves;
internal bool captured;
internal bool pawnMoved;
public PieceColor CurrentTurn
{
get
{
return currentTurn;
}
}
public bool Check
{
get
{
return check;
}
}
public PieceColor OtherColor
{
get
{
return GetOtherColor(currentTurn);
}
}
public bool GameOver
{
get
{
return gameOver;
}
}
public bool Draw
{
get
{
return draw;
}
}
public PieceColor Winner
{
get
{
return winner;
}
}
public Pawn PawnToPromote
{
get
{
return pawnToPromote;
}
}
public ChessLogic()
{
oldBoard = new Piece[8, 8];
board = new Piece[8, 8];
oldPieces = new List<Piece>[2];
pieces = new List<Piece>[2];
oldPieceMoveList = new List<Cell[]>[2];
oldPieceMoveListGenerated = new List<bool>[2];
pawns = new Pawn[2, 8];
rooks = new Rook[2, 2];
knights = new Knight[2, 2];
bishops = new Bishop[2, 2];
queens = new Queen[2];
kings = new King[2];
oldPieces[0] = new List<Piece>();
oldPieces[1] = new List<Piece>();
pieces[0] = new List<Piece>();
pieces[1] = new List<Piece>();
oldPieceMoveList[0] = new List<Cell[]>();
oldPieceMoveList[1] = new List<Cell[]>();
oldPieceMoveListGenerated[0] = new List<bool>();
oldPieceMoveListGenerated[1] = new List<bool>();
// cria os peões brancos
for (int col = 0; col < 8; col++)
pawns[0, col] = new Pawn(this, PieceColor.WHITE);
// cria os peões negros
for (int col = 0; col < 8; col++)
pawns[1, col] = new Pawn(this, PieceColor.BLACK);
// cria as torrres brancas
rooks[0, 0] = new Rook(this, PieceColor.WHITE);
rooks[0, 1] = new Rook(this, PieceColor.WHITE);
// cria as torrres negras
rooks[1, 0] = new Rook(this, PieceColor.BLACK);
rooks[1, 1] = new Rook(this, PieceColor.BLACK);
// cria os cavalos brancos
knights[0, 0] = new Knight(this, PieceColor.WHITE);
knights[0, 1] = new Knight(this, PieceColor.WHITE);
// cria os cavalos negros
knights[1, 0] = new Knight(this, PieceColor.BLACK);
knights[1, 1] = new Knight(this, PieceColor.BLACK);
// cria os bispos brancos
bishops[0, 0] = new Bishop(this, PieceColor.WHITE);
bishops[0, 1] = new Bishop(this, PieceColor.WHITE);
// cria os bispos negros
bishops[1, 0] = new Bishop(this, PieceColor.BLACK);
bishops[1, 1] = new Bishop(this, PieceColor.BLACK);
// cria a dama branca
queens[0] = new Queen(this, PieceColor.WHITE);
// cria o rei branco
kings[0] = new King(this, PieceColor.WHITE);
// cria a dama negra
queens[1] = new Queen(this, PieceColor.BLACK);
// cria o rei negro
kings[1] = new King(this, PieceColor.BLACK);
Reset();
}
public bool IsValidPosition(int row, int col) => IsValidPosition(new Cell(row, col));
public bool IsValidPosition(Cell position) => position.Valid;
public Piece GetPiece(int row, int col) => board[row, col];
public Piece GetPiece(Cell position) => GetPiece(position.Row, position.Col);
public bool IsEmpty(int row, int col) => board[row, col] == null;
public bool IsEmpty(Cell position) => IsEmpty(position.Row, position.Col);
private void Clear()
{
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
{
Piece piece = board[row, col];
if (piece != null)
{
piece.alive = false;
piece.position = Cell.INVALID_CELL;
piece.moveListGenerated = false;
piece.moveList.Clear();
}
board[row, col] = null;
}
pieces[0].Clear();
pieces[1].Clear();
}
public void Reset()
{
currentTurn = PieceColor.WHITE;
gameOver = false;
draw = false;
pawnWalkedTwoRows = false;
movesToDraw = MOVES_TO_DRAW;
repeatedMoves = 0;
captured = false;
pawnMoved = false;
Clear();
// reseta as posições dos peões brancos
for (int col = 0; col < 8; col++)
pawns[0, col].SetPosition(6, col);
// reseta as posições dos peões negros
for (int col = 0; col < 8; col++)
pawns[1, col].SetPosition(1, col);
// reseta as posiçõe das torrres brancas
rooks[0, 0].SetPosition(7, 0);
rooks[0, 0].wasMoved = false;
rooks[0, 1].SetPosition(7, 7);
rooks[0, 1].wasMoved = false;
// reseta as posiçõe das torrres negras
rooks[1, 0].SetPosition(0, 0);
rooks[1, 0].wasMoved = false;
rooks[1, 1].SetPosition(0, 7);
rooks[1, 1].wasMoved = false;
// reseta as posiçõe dos cavalos brancos
knights[0, 0].SetPosition(7, 1);
knights[0, 1].SetPosition(7, 6);
// reseta as posiçõe dos cavalos negros
knights[1, 0].SetPosition(0, 1);
knights[1, 1].SetPosition(0, 6);
// reseta as posiçõe dos bispos brancos
bishops[0, 0].SetPosition(7, 2);
bishops[0, 1].SetPosition(7, 5);
// reseta as posiçõe dos bispos negros
bishops[1, 0].SetPosition(0, 2);
bishops[1, 1].SetPosition(0, 5);
// reseta a posição da dama branca
queens[0].SetPosition(7, 3);
// reseta a posição do rei branco
kings[0].SetPosition(7, 4);
kings[0].wasMoved = false;
// reseta a posição da dama negro
queens[1].SetPosition(0, 3);
// reseta a posição do rei negro
kings[1].SetPosition(0, 4);
kings[1].wasMoved = false;
GenerateMoveList();
}
public PieceColor GetOtherColor(PieceColor color) => color == PieceColor.BLACK ? PieceColor.WHITE : PieceColor.BLACK;
internal void SwapColor() => currentTurn = currentTurn == PieceColor.BLACK ? PieceColor.WHITE : PieceColor.BLACK;
public Pawn GetPawn(PieceColor color, int index) => pawns[(int) color, index];
public Rook GetRook(PieceColor color, int index) => rooks[(int) color, index];
public Knight GetKnight(PieceColor color, int index) => knights[(int) color, index];
public Bishop GetBishop(PieceColor color, int index) => bishops[(int) color, index];
public Queen GetQueen(PieceColor color) => queens[(int) color];
public King GetKing(PieceColor color) => kings[(int) color];
public int GetPieceCount(PieceColor color) => pieces[(int) color].Count;
public Piece GetPiece(PieceColor color, int index) => pieces[(int) color][index];
public bool IsAttacked(Cell position, PieceColor color)
{
foreach (Piece piece in pieces[(int) color])
{
//piece.GetMoveList(false, false);
if (piece.IsAttacking(position))
return true;
}
return false;
}
internal void GenerateMoveList()
{
if (gameOver)
return;
if (movesToDraw <= 0 || repeatedMoves >= REPEATED_MOVES_TO_DRAW)
{
gameOver = true;
draw = true;
return;
}
captured = false;
pawnMoved = false;
for (int i = 0; i < 2; i++)
for (int j = 0; j < pieces[i].Count; j++)
{
Piece piece = pieces[i][j];
piece.moveListGenerated = false;
}
// verifica se o meu rei não está em cheque
PieceColor otherColor = GetOtherColor(currentTurn);
// gera a lista de movimentos do oponente
foreach (Piece piece in pieces[(int) otherColor])
{
piece.GetMoveList(false, false);
}
King myKing = GetKing(currentTurn);
check = myKing.InCheck();
// gera a lista de movimentos de minhas peças
int moveCount = 0;
for (int i = 0; i < pieces[(int) currentTurn].Count; i++)
{
Piece piece = pieces[(int) currentTurn][i];
piece.moveListGenerated = false;
piece.GetMoveList();
moveCount += piece.MoveCount;
}
gameOver = moveCount == 0;
if (gameOver)
{
draw = !myKing.InCheck();
if (!draw)
winner = GetOtherColor(currentTurn);
}
}
internal bool CheckCheck(Cell src, Cell dst)
{
bool[,] rookMoved = new bool[2, 2];
bool[] kingMoved = new bool[2];
Piece srcPiece = GetPiece(src);
if (srcPiece == null)
return false;
// salva o estado atual do tabuleiro
bool pawnWalkedTwoRows = this.pawnWalkedTwoRows;
int pawnCol = this.pawnCol;
Pawn pawnToPromote = this.pawnToPromote;
int movesToDraw = this.movesToDraw;
int repeatedMoves = this.repeatedMoves;
bool captured = this.captured;
bool pawnMoved = this.pawnMoved;
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
oldBoard[row, col] = board[row, col];
for (int i = 0; i < 2; i++)
{
oldPieces[i].Clear();
oldPieceMoveList[i].Clear();
oldPieceMoveListGenerated[i].Clear();
for (int j = 0; j < pieces[i].Count; j++)
{
oldPieces[i].Add(pieces[i][j]);
oldPieceMoveList[i].Add(pieces[i][j].moveList.ToArray());
oldPieceMoveListGenerated[i].Add(pieces[i][j].moveListGenerated);
}
}
kingMoved[0] = kings[0].wasMoved;
kingMoved[1] = kings[1].wasMoved;
rookMoved[0, 0] = rooks[0, 0].wasMoved;
rookMoved[0, 1] = rooks[0, 1].wasMoved;
rookMoved[1, 0] = rooks[1, 0].wasMoved;
rookMoved[1, 1] = rooks[1, 1].wasMoved;
PieceColor myColor = currentTurn;
// faz o movimento
srcPiece.UnsafeMove(dst);
// verifica se ainda está em cheque
King myKing = GetKing(myColor);
bool kingInCheck = myKing.InCheck();
// voltar pro estado anterior do tabieleiro
this.pawnWalkedTwoRows = pawnWalkedTwoRows;
this.pawnCol = pawnCol;
this.pawnToPromote = pawnToPromote;
this.movesToDraw = movesToDraw;
this.repeatedMoves = repeatedMoves;
this.captured = captured;
this.pawnMoved = pawnMoved;
for (int row = 0; row < 8; row++)
for (int col = 0; col < 8; col++)
{
Piece piece = oldBoard[row, col];
board[row, col] = piece;
if (piece != null)
{
piece.alive = true;
piece.position = new Cell(row, col);
}
}
for (int i = 0; i < 2; i++)
{
pieces[i].Clear();
for (int j = 0; j < oldPieces[i].Count; j++)
{
Piece piece = oldPieces[i][j];
pieces[i].Add(piece);
piece.moveListGenerated = oldPieceMoveListGenerated[i][j];
piece.moveList.Clear();
piece.moveList.AddRange(oldPieceMoveList[i][j]);
}
}
kings[0].wasMoved = kingMoved[0];
kings[1].wasMoved = kingMoved[1];
rooks[0, 0].wasMoved = rookMoved[0, 0];
rooks[0, 1].wasMoved = rookMoved[0, 1];
rooks[1, 0].wasMoved = rookMoved[1, 0];
rooks[1, 1].wasMoved = rookMoved[1, 1];
currentTurn = myColor;
// retorna o resultado
return kingInCheck;
}
}
}