-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
599 lines (483 loc) · 19 KB
/
Board.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
namespace Chess
{
/*
* The Chess board. This class is a singleton (that is, only one instance is allowed).
*/
public class Board : Panel
{
public const int NUM_ROWS = 8;
private Square[,] matrix; //The board is a matrix of buttons
private List whitePieces;
private List blackPieces;
private string path = "Images/Classic";
private MainForm mainForm;
//Singleton
private static Board board;
private Board()
{
matrix = new Square[NUM_ROWS, NUM_ROWS];
whitePieces = new List();
blackPieces = new List();
this.Size = new Size(480, 480);
this.Location = new Point(30, 30);
// Create the squares
int h;
int v = 0;
for (int i = 0; i < NUM_ROWS; i++)
{
h = 0;
for (int j = 0; j < NUM_ROWS; j++)
{
matrix[i, j] = new Square(new Position(i, j));
if ((i + j) % 2 == 0)
{
matrix[i, j].BackgroundImage = Image.FromFile(path + "//WhiteWood.gif");
}
else {
matrix[i, j].BackgroundImage = Image.FromFile(path + "//BlackWood.gif");
}
matrix[i, j].Name = "square" + i + "" + j; //square00, square01, etc.
matrix[i, j].Location = new Point(h, v);
matrix[i, j].Click += new System.EventHandler(this.square_Click);
h += 60;
matrix[i, j].Size = new Size(60, 60);
this.Controls.Add(matrix[i, j]);
}
v += 60;
}
}
/*
* This method returns the only instance of this class
*/
public static Board getInstance()
{
if (board == null)
{
board = new Board();
}
return board;
}
//Getters and setters
public List getWhitePieces()
{
return whitePieces;
}
public List getBlackPieces()
{
return blackPieces;
}
public void setMainForm(MainForm mf)
{
this.mainForm = mf;
}
public string getPath()
{
return path;
}
public void setPath(string path)
{
this.path = path;
}
#region Memento
/*
* This method restores the previous state of the game
*/
public void restoreFromMementos(MementoBoard mb, MementoImages mi)
{
whitePieces = mb.getWhitePieces();
blackPieces = mb.getBlackPieces();
mainForm.setWhiteImages(mi.getWhiteImages());
mainForm.setBlackImages(mi.getBlackImages());
redrawBoard();
mainForm.paintDeletedWhitePieces();
mainForm.paintDeletedBlackPieces();
mainForm.shiftTurn();
mainForm.removeFromLog();
}
public void createMementos()
{
Caretaker.mementoBoard = new MementoBoard(whitePieces.clone(), blackPieces.clone());
ImageList whiteImages = new ImageList();
ImageList blackImages = new ImageList();
foreach (Image i in mainForm.getWhiteImages().Images)
{
whiteImages.Images.Add(i);
}
foreach (Image i in mainForm.getBlackImages().Images)
{
blackImages.Images.Add(i);
}
Caretaker.mementoImages = new MementoImages(whiteImages, blackImages);
}
#endregion
public void initializePieces()
{
clear();
// White pieces
whitePieces.insert(new Rook(new Position(7, 0), Color.White));
whitePieces.insert(new Knight(new Position(7, 1), Color.White));
whitePieces.insert(new Bishop(new Position(7, 2), Color.White));
whitePieces.insert(new Queen(new Position(7, 3), Color.White));
whitePieces.insert(new King(new Position(7, 4), Color.White));
whitePieces.insert(new Bishop(new Position(7, 5), Color.White));
whitePieces.insert(new Knight(new Position(7, 6), Color.White));
whitePieces.insert(new Rook(new Position(7, 7), Color.White));
for (int i = 0; i < NUM_ROWS; i++) //White pawns
{
whitePieces.insert(new Pawn(new Position(6, i), Color.White));
}
// Black pieces
blackPieces.insert(new Rook(new Position(0, 0), Color.Black));
blackPieces.insert(new Knight(new Position(0, 1), Color.Black));
blackPieces.insert(new Bishop(new Position(0, 2), Color.Black));
blackPieces.insert(new Queen(new Position(0, 3), Color.Black));
blackPieces.insert(new King(new Position(0, 4), Color.Black));
blackPieces.insert(new Bishop(new Position(0, 5), Color.Black));
blackPieces.insert(new Knight(new Position(0, 6), Color.Black));
blackPieces.insert(new Rook(new Position(0, 7), Color.Black));
for (int i = 0; i < NUM_ROWS; i++) // Black pawns
{
blackPieces.insert(new Pawn(new Position(1, i), Color.Black));
}
redrawBoard();
}
public void redrawBoard()
{
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_ROWS; j++)
{
if ((i + j) % 2 == 0)
{
matrix[i, j].BackgroundImage = Image.FromFile(path + "//WhiteWood.gif");
}
else {
matrix[i, j].BackgroundImage = Image.FromFile(path + "//BlackWood.gif");
}
matrix[i, j].Image = null;
}
}
Iterator it = whitePieces.getIterator();
while (it.MoveNext())
{
drawPiece(it.Current as Piece);
}
it = blackPieces.getIterator();
while (it.MoveNext())
{
drawPiece(it.Current as Piece);
}
}
/*
* This method performs a movement. If a piece is deleted in this movement,
* the piece is returned by the method.
*/
private Piece realizeMovement(Position pos)
{
Piece piece = killPiece(pos, false);
//Redraw the board and update the piece position
deletePieceSquare(MainForm.selectedPiece);
Board.getInstance().deletePiece(MainForm.selectedPiece);
MainForm.selectedPiece.setPosition(pos);
Board.getInstance().drawPiece(MainForm.selectedPiece);
deleteAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
return piece;
}
/*
* Clears the lists of pieces. It is used when the game is over.
*/
public void clear()
{
whitePieces.clear();
blackPieces.clear();
}
/*
* Draws a piece in the board
*/
public void drawPiece(Piece p)
{
Position pos = p.getPosition();
matrix[pos.getRow(), pos.getColumn()].Image = p.getImage();
}
/*
* Removes a piece from the board
*/
public void deletePiece(Piece p)
{
Position pos = p.getPosition();
matrix[pos.getRow(), pos.getColumn()].Image = null;
}
/*
* Checks whether there is a piece in a given position
*/
public Piece consultPiece(Position p, bool white)
{
Iterator it;
if (white)
{
it = whitePieces.getIterator();
}
else {
it = blackPieces.getIterator();
}
while (it.MoveNext())
{
Piece piece = (it.Current as Piece);
if (piece.getPosition().Equals(p))
{
return piece;
}
}
return null;
}
/*
* Deletes a piece from a given position. In case of "pawn promotion", do not put into deleted pieces set.
*/
public Piece killPiece(Position pos, bool promotion)
{
bool found = false;
Piece piece = null;
Iterator it = whitePieces.getIterator();
while (it.MoveNext())
{
Piece p = (it.Current as Piece);
if (p.getPosition().Equals(pos))
{
piece = p;
if (!promotion)
{
mainForm.getWhiteImages().Images.Add(p.getImage());
}
whitePieces.remove(p);
found = true;
break;
}
}
if (!found)
{
it = blackPieces.getIterator();
while (it.MoveNext())
{
Piece p = (it.Current as Piece);
if (p.getPosition().Equals(pos))
{
piece = p;
if (!promotion)
{
mainForm.getBlackImages().Images.Add(p.getImage());
}
blackPieces.remove(p);
break;
}
}
}
return piece;
}
//Methods for painting squares
private void paintAllowedMovements(List positions)
{
int row, column;
Iterator it = positions.getIterator();
while (it.MoveNext())
{
row = (it.Current as Position).getRow();
column = (it.Current as Position).getColumn();
matrix[row, column].BackgroundImage = null;
matrix[row, column].BackColor = Color.LightGreen;
}
}
public void deleteAllowedMovements(List positions)
{
int row, column;
Iterator it = positions.getIterator();
while (it.MoveNext())
{
row = (it.Current as Position).getRow();
column = (it.Current as Position).getColumn();
matrix[row, column].BackColor = Color.Empty;
if ((row + column) % 2 == 0)
{
matrix[row, column].BackgroundImage = Image.FromFile(Board.getInstance().getPath() + "//WhiteWood.gif");
}
else {
matrix[row, column].BackgroundImage = Image.FromFile(Board.getInstance().getPath() + "//BlackWood.gif");
}
}
}
private void paintPiece(Piece p)
{
int row = p.getPosition().getRow();
int column = p.getPosition().getColumn();
matrix[row, column].BackgroundImage = null;
matrix[row, column].BackColor = Color.LightCoral;
}
public void deletePieceSquare(Piece p)
{
int row = p.getPosition().getRow();
int column = p.getPosition().getColumn();
matrix[row, column].BackColor = Color.Empty;
if ((row + column) % 2 == 0)
{
matrix[row, column].BackgroundImage = Image.FromFile(Board.getInstance().getPath() + "//WhiteWood.gif");
}
else {
matrix[row, column].BackgroundImage = Image.FromFile(Board.getInstance().getPath() + "//BlackWood.gif");
}
}
private void square_Click(object sender, System.EventArgs e)
{
Square sq = (Square)sender;
BoardLogic.updateListAllowedMovements();
bool whiteTurn = (MainForm.turn == "White");
listenerSquare(sq, whiteTurn);
}
private void listenerSquare(Square sq, bool whiteTurn)
{
List pWhite = null;
List pBlack = null;
ImageList iWhite = new ImageList();
ImageList iBlack = new ImageList();
Piece piece = null;
if (whiteTurn)
{
piece = Board.getInstance().consultPiece(sq.getPosition(), true);
}
else {
piece = Board.getInstance().consultPiece(sq.getPosition(), false);
}
if (piece != null)
{
if (MainForm.selectedPiece != null)
{
deletePieceSquare(MainForm.selectedPiece);
deleteAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
}
MainForm.selectedPiece = piece;
paintPiece(MainForm.selectedPiece);
paintAllowedMovements(MainForm.selectedPiece.getAllowedMovements());
}
else { //Empty square or occupied by an opponent's piece
Color turnColor;
if (whiteTurn)
{
turnColor = Color.White;
}
else {
turnColor = Color.Black;
}
if (MainForm.selectedPiece != null && MainForm.selectedPiece.getColor() == turnColor)
{ // A piece of the current player is selected
Position posIni = MainForm.selectedPiece.getPosition();
Position posFin = sq.getPosition();
Piece deletedPiece = null; //Deleted piece. It is returned in case an undo action is triggered.
if (MainForm.selectedPiece.isvalidMove(posFin)) //If the movement is valid ...
{
if (Caretaker.mementoBoard != null)
{
pWhite = Caretaker.mementoBoard.getWhitePieces().clone();
pBlack = Caretaker.mementoBoard.getBlackPieces().clone();
}
if (Caretaker.mementoImages != null)
{
foreach (Image i in Caretaker.mementoImages.getWhiteImages().Images)
{
iWhite.Images.Add(i);
}
foreach (Image i in Caretaker.mementoImages.getBlackImages().Images)
{
iBlack.Images.Add(i);
}
}
createMementos();
deletedPiece = realizeMovement(posFin);
BoardLogic.updateListAllowedMovements();
bool kingCheck = false;
if (whiteTurn)
{
kingCheck = BoardLogic.isCheck(true);
}
else {
kingCheck = BoardLogic.isCheck(false);
}
if (kingCheck)
{
//Undo the movement
realizeMovement(posIni);
Caretaker.mementoBoard.setWhitePieces(pWhite);
Caretaker.mementoBoard.setBlackPieces(pBlack);
Caretaker.mementoImages.setWhiteImages(iWhite);
Caretaker.mementoImages.setBlackImages(iBlack);
if (deletedPiece != null)
{
//Return the deleted piece
if (whiteTurn)
{
blackPieces.insert(deletedPiece);
}
else {
whitePieces.insert(deletedPiece);
}
//Delete inserted image
if (whiteTurn)
{
mainForm.deleteLastImage(mainForm.getBlackImages());
}
else {
mainForm.deleteLastImage(mainForm.getWhiteImages());
}
drawPiece(deletedPiece);
}
MessageBox.Show(this, "Your king would be in check if you carried out this movement", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else { //The king is not in check
int lastRow = -1;
if (whiteTurn)
{
lastRow = 0;
}
else {
lastRow = 7;
}
if (MainForm.selectedPiece is Pawn && posFin.getRow() == lastRow)
{
//Promotion
FormPromotion f = new FormPromotion(MainForm.selectedPiece as Pawn);
f.ShowDialog();
}
BoardLogic.updateListAllowedMovements();
bool doCheck;
if (whiteTurn)
{
doCheck = BoardLogic.isCheck(false);
}
else {
doCheck = BoardLogic.isCheck(true);
}
if (doCheck)
{
MessageBox.Show(this, "King is in check!", "Check", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
if (deletedPiece != null)
{
if (whiteTurn)
{
mainForm.paintDeletedBlackPieces();
}
else {
mainForm.paintDeletedWhitePieces();
}
}
mainForm.showInLog(MainForm.selectedPiece.ToString() + " --> " + "From " + posIni.ToString() + " to " + posFin.ToString());
mainForm.enableUndo();
mainForm.shiftTurn();
}
}
}
}
}
}
}