-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchessui.cpp
622 lines (543 loc) · 16.6 KB
/
chessui.cpp
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
Copyright (c) 2016 Walter William Karas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "misc.hpp"
#include "brdsize.hpp"
#include "chess.hpp"
#include "chessui.hpp"
#include "chcharui.hpp"
CHESSUSERIFACE ChessUI;
const char whiteText[] = "White",
blackText[] = "Black";
LOCAL const char *colorText(PIECECOLOR color)
{ return(color == WHITE ? whiteText : blackText); }
// returns piece abbreviation to be displayed in board squares
// for a given piece
LOCAL const char *pieceAbbrev
(
PIECECOLOR color,
PIECETYPE type
)
{
if (color == WHITE)
switch (type)
{
case TYPEKING: return("WK");
case TYPEQUEEN: return("WQ");
case TYPEBISHOP: return("WB");
case TYPEKNIGHT: return("WN");
case TYPEROOK: return("WR");
case TYPEPAWN: return("WP");
}
else
switch (type)
{
case TYPEKING: return("BK");
case TYPEQUEEN: return("BQ");
case TYPEBISHOP: return("BB");
case TYPEKNIGHT: return("BN");
case TYPEROOK: return("BR");
case TYPEPAWN: return("BP");
}
return(""); // dummy return
}
const char kingText[] = " King",
queenText[] = " Queen",
bishopText[] = " Bishop",
knightText[] = " Knight",
rookText[] = " Rook",
pawnText[] = " Pawn";
// fills in two entries in a message textList with color/name of
// a piece
LOCAL void pieceText
(
PIECECOLOR color,
PIECETYPE type,
const char **textList
)
{
*(textList++) = colorText(color);
switch (type)
{
case TYPEKING:
*textList = kingText;
return;
case TYPEQUEEN:
*textList = queenText;
return;
case TYPEBISHOP:
*textList = bishopText;
return;
case TYPEKNIGHT:
*textList = knightText;
return;
case TYPEROOK:
*textList = rookText;
return;
case TYPEPAWN:
*textList = pawnText;
return;
}
}
// base textLists for messages. terminated by null pointers. some
// have null pointer place holders for variable portions of message.
const char *noMemory[] =
{
"Insufficient memory. Press any key to exit:",
(char *) 0
};
const char *checkMated[] =
{
(char *) 0,
" King in checkmate. Press any key to exit:",
(char *) 0
};
const int MATEDCOLORINDEX = 0;
const char *staleMate[] =
{
(char *) 0,
" King in stalemate. Press any key to exit:",
(char *) 0
};
const int STALEMATEDCOLORINDEX = 0;
const char *selectPiece[] =
{
(char *) 0,
" Player: Select piece to move with arrow keys, then hit enter:",
(char *) 0
};
const int SELECTCOLORINDEX = 0;
const char *illegalSelection[] =
{
"That location does not contain one of your pieces."
" Press any key to try again:",
(char *) 0
};
const char *moveIllegal[] =
{
"You cannot move the selected piece to that location."
" Press any key to try again:",
(char *) 0
};
const char *wouldLoseKing[] =
{
"This move would result in your King being taken by the ",
(char *) 0,
(char *) 0,
". Press any key to select another move:",
(char *) 0
};
const int DANGERINDEX = 1;
const char *castleQueenRook[] =
{
"Do you want to castle with Queen's Rook? (Type Y or N):",
(char *) 0
};
const char *castleKingRook[] =
{
"Do you want to castle with King's Rook? (Type Y or N):",
(char *) 0
};
const char *selectDest[] =
{
"Select destination of move with arrow keys, then hit enter:",
(char *) 0
};
const char *promoteToWhat[] =
{
"Do you want your Pawn to be promoted to a Queen, Bishop, "
"Knight or Rook? (Type Q, B, K or R):",
(char *) 0
};
// character key lists
const char promoteOptions[] = "qQbBkKrR";
const PIECETYPE promoteType[] = { TYPEQUEEN, TYPEBISHOP, TYPEKNIGHT,
TYPEROOK };
const char yesNoAnswer[] = "yYnN";
const int YESINDEX = 0;
const int NOINDEX = 1;
// more message text lists
const char *thinking[] =
{
(char *) 0,
" Player thinking, please wait...",
(char *) 0
};
const int THINKINGCOLORINDEX = 0;
const char *doCastleQueenRook[] =
{
(char *) 0,
" Player castles with Queen's Rook. Press any key to continue:",
(char *) 0
};
const char *doCastleKingRook[] =
{
(char *) 0,
" Player castles with King's Rook. Press any key to continue:",
(char *) 0
};
const int CASTLECOLORINDEX = 0;
const char *whatMoved[] =
{
(char *) 0,
" Player moves ",
(char *) 0,
(char *) 0,
". Press any key to continue:",
(char *) 0
};
const int MOVECOLORINDEX = 0;
const int MOVEPIECEINDEX = 2;
const char *whatMovedCaptured[] =
{
(char *) 0,
" Player moves ",
(char *) 0,
(char *) 0,
", capturing ",
(char *) 0,
(char *) 0,
". Press any key to continue:",
(char *) 0
};
const int CAPTURINGCOLORINDEX = 0;
const int CAPTURINGPIECEINDEX = 2;
const int CAPTUREDPIECEINDEX = 5;
const char *doPromotion[] =
{
(char *) 0,
" Player promotes Pawn to ",
(char *) 0,
(char *) 0,
". Press any key to continue:",
(char *) 0
};
const int PROMOTECOLORINDEX = 0;
const int PROMOTEPIECEINDEX = 2;
void CHESSUSERIFACE::outOfMemory(void)
{
ChessCharUI.showMessage(noMemory, (char *) 0, (uint *) 0);
return;
}
void CHESSUSERIFACE::init(const BOARD &board)
{
int row, col;
PIECE *p;
ChessCharUI.initScreen();
for (row = 0; row < NUMROWS; row++)
for (col = 0; col < NUMCOLS; col++)
{
p = board.whatPiece(row, col);
if (p)
ChessCharUI.showPiece
(
POSITION(row, col),
pieceAbbrev(p->whatColor(), p->whatType())
);
}
return;
}
void CHESSUSERIFACE::mated(PIECECOLOR color)
{
checkMated[MATEDCOLORINDEX] = colorText(color);
ChessCharUI.showMessage(checkMated, (char *) 0, (uint *) 0);
return;
}
void CHESSUSERIFACE::staleMated(PIECECOLOR color)
{
staleMate[STALEMATEDCOLORINDEX] = colorText(color);
ChessCharUI.showMessage(staleMate, (char *) 0, (uint *) 0);
return;
}
BOOL CHESSUSERIFACE::userMove(BOARD &board, PIECECOLOR color)
{
POSITION start, end;
PIECE *p;
uint keyIndex;
MOVESTATUS moveStatus;
MOVEUNDODATA dummy;
selectPiece[SELECTCOLORINDEX] = colorText(color);
// loop to get legal move
for ( ; ; )
{
// loop to get piece to move
for ( ; ; )
{
start.row = start.col = 0;
if (!ChessCharUI.selectPosition(selectPiece, start))
return(FALSE);
p = board.whatPiece(start);
if (p)
if (p->whatColor() == color)
break;
if (!ChessCharUI.showMessage(illegalSelection,
(char *) 0, (uint *) 0))
return(FALSE);
ChessCharUI.clearSelect(start);
}
if (board.userCanCastle(QUEENSIDECASTLE, color))
if ((p->whatType() == TYPEKING) ||
((p->whatType() == TYPEROOK) && (start.row == 0)))
{
if (!ChessCharUI.showMessage(castleQueenRook,
yesNoAnswer, &keyIndex))
return(FALSE);
if ((keyIndex / 2) == YESINDEX)
{
ChessCharUI.clearSelect(start);
board.castle(QUEENSIDECASTLE, color, dummy);
ChessCharUI.clearPiece(POSITION(0, start.col));
ChessCharUI.showPiece
(
POSITION(3, start.col),
pieceAbbrev(color, TYPEROOK)
);
ChessCharUI.clearPiece(POSITION(4, start.col));
ChessCharUI.showPiece
(
POSITION(2, start.col),
pieceAbbrev(color, TYPEKING)
);
return(TRUE);
}
}
if (board.userCanCastle(KINGSIDECASTLE, color))
if ((p->whatType() == TYPEKING) ||
((p->whatType() == TYPEROOK) && (start.row == 7)))
{
if (!ChessCharUI.showMessage(castleKingRook,
yesNoAnswer, &keyIndex))
return(FALSE);
if ((keyIndex / 2) == YESINDEX)
{
ChessCharUI.clearSelect(start);
board.castle(KINGSIDECASTLE, color, dummy);
ChessCharUI.clearPiece(POSITION(7, start.col));
ChessCharUI.showPiece
(
POSITION(5, start.col),
pieceAbbrev(color, TYPEROOK)
);
ChessCharUI.clearPiece(POSITION(4, start.col));
ChessCharUI.showPiece
(
POSITION(6, start.col),
pieceAbbrev(color, TYPEKING)
);
return(TRUE);
}
}
end = start;
if (!ChessCharUI.selectPosition(selectDest, end))
return(FALSE);
moveStatus = board.doUserMove
(
start,
end
);
if ((moveStatus.status == MOVEDONE) ||
(moveStatus.status == MOVEENPASSANT))
break;
if (moveStatus.status == ILLEGALMOVE)
{
if (!ChessCharUI.showMessage(moveIllegal, (char *) 0,
(uint *) 0))
return(FALSE);
}
else // king would be taken
{
pieceText
(
OtherColor(color),
board.whatPiece(moveStatus.dangerToKing)->whatType(),
wouldLoseKing + DANGERINDEX
);
if (!ChessCharUI.showMessage(wouldLoseKing, (char *) 0,
(uint *) 0))
return(FALSE);
}
ChessCharUI.clearSelect(start);
ChessCharUI.clearSelect(end);
} // end loop to get legal move
ChessCharUI.clearSelect(start);
ChessCharUI.clearPiece(start);
ChessCharUI.clearPiece(end);
if (moveStatus.status == MOVEENPASSANT)
ChessCharUI.clearPiece(POSITION(end.row, start.col));
ChessCharUI.showPiece
(
end,
pieceAbbrev(color, p->whatType())
);
if (board.canPromote(end))
{
if (!ChessCharUI.showMessage(promoteToWhat, promoteOptions,
&keyIndex))
return(FALSE);
keyIndex /= 2;
board.promote(end, promoteType[keyIndex]);
ChessCharUI.clearPiece(end);
ChessCharUI.showPiece
(
end,
pieceAbbrev(color, promoteType[keyIndex])
);
}
ChessCharUI.clearSelect(end);
return(TRUE);
}
void CHESSUSERIFACE::thinkingMessage(PIECECOLOR color)
{
thinking[THINKINGCOLORINDEX] = colorText(color);
ChessCharUI.showMessage(thinking);
return;
}
void CHESSUSERIFACE::clearMessage(void)
{
ChessCharUI.clearMessage();
return;
}
BOOL CHESSUSERIFACE::computerMove
(
BOARD &board,
PIECECOLOR color,
PIECEMOVE &moveInfo
)
{
int backCol = color == WHITE ? 0 : 7;
MOVEUNDODATA undoData;
switch (moveInfo.type)
{
case KINGSIDECASTLE:
board.castle(KINGSIDECASTLE, color, undoData);
ChessCharUI.clearPiece(POSITION(7, backCol));
ChessCharUI.clearPiece(POSITION(4, backCol));
ChessCharUI.showPiece
(
POSITION(5, backCol),
pieceAbbrev(color, TYPEROOK)
);
ChessCharUI.showPiece
(
POSITION(6, backCol),
pieceAbbrev(color, TYPEKING)
);
ChessCharUI.setSelect(POSITION(5, backCol));
ChessCharUI.setSelect(POSITION(6, backCol));
doCastleKingRook[CASTLECOLORINDEX] = colorText(color);
if (!ChessCharUI.showMessage(doCastleKingRook,
(char *) 0, (uint *) 0))
return(FALSE);
ChessCharUI.clearSelect(POSITION(5, backCol));
ChessCharUI.clearSelect(POSITION(6, backCol));
return(TRUE);
case QUEENSIDECASTLE:
board.castle(QUEENSIDECASTLE, color, undoData);
ChessCharUI.clearPiece(POSITION(0, backCol));
ChessCharUI.clearPiece(POSITION(4, backCol));
ChessCharUI.showPiece
(
POSITION(3, backCol),
pieceAbbrev(color, TYPEROOK)
);
ChessCharUI.showPiece
(
POSITION(2, backCol),
pieceAbbrev(color, TYPEKING)
);
ChessCharUI.setSelect(POSITION(3, backCol));
ChessCharUI.setSelect(POSITION(2, backCol));
doCastleQueenRook[CASTLECOLORINDEX] = colorText(color);
if (!ChessCharUI.showMessage(doCastleQueenRook,
(char *) 0, (uint *) 0))
return(FALSE);
ChessCharUI.clearSelect(POSITION(3, backCol));
ChessCharUI.clearSelect(POSITION(2, backCol));
return(TRUE);
case NORMALMOVE:
board.doMove(moveInfo.start, moveInfo.end, undoData);
ChessCharUI.clearPiece(moveInfo.start);
ChessCharUI.clearPiece(moveInfo.end);
if (undoData.enPassantEffect == ENPASSANTCAPTURE)
ChessCharUI.clearPiece(POSITION(moveInfo.end.row,
moveInfo.start.col));
ChessCharUI.showPiece
(
moveInfo.end,
pieceAbbrev(color,
board.whatPiece(moveInfo.end)->whatType())
);
ChessCharUI.setSelect(moveInfo.start);
ChessCharUI.setSelect(moveInfo.end);
if (undoData.capturedPiece)
{
whatMovedCaptured[CAPTURINGCOLORINDEX] = colorText(color);
pieceText
(
color,
board.whatPiece(moveInfo.end)->whatType(),
whatMovedCaptured + CAPTURINGPIECEINDEX
);
pieceText
(
OtherColor(color),
undoData.capturedPiece->whatType(),
whatMovedCaptured + CAPTUREDPIECEINDEX
);
delete undoData.capturedPiece;
if (!ChessCharUI.showMessage(whatMovedCaptured,
(char *) 0, (uint *) 0))
return(FALSE);
}
else
{
whatMoved[MOVECOLORINDEX] = colorText(color);
pieceText
(
color,
board.whatPiece(moveInfo.end)->whatType(),
whatMoved + MOVEPIECEINDEX
);
if (!ChessCharUI.showMessage(whatMoved,
(char *) 0, (uint *) 0))
return(FALSE);
}
if (moveInfo.promoteType != TYPENOPIECE)
{
board.promote(moveInfo.end, moveInfo.promoteType);
ChessCharUI.clearPiece(moveInfo.end);
ChessCharUI.showPiece
(
moveInfo.end,
pieceAbbrev(color, moveInfo.promoteType)
);
doPromotion[PROMOTECOLORINDEX] = colorText(color);
pieceText
(
color,
moveInfo.promoteType,
doPromotion + PROMOTEPIECEINDEX
);
if (!ChessCharUI.showMessage(doPromotion, (char *) 0,
(uint *) 0))
return(FALSE);
}
ChessCharUI.clearSelect(moveInfo.start);
ChessCharUI.clearSelect(moveInfo.end);
return(TRUE);
} // end of switch
return(FALSE);
} // end of function