-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitFormSearchAndDestroyChess2.cpp
424 lines (401 loc) · 13.7 KB
/
UnitFormSearchAndDestroyChess2.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
//---------------------------------------------------------------------------
/*
SearchAndDestroyChess 2, Kriegspiel/Dark Chess game
Copyright (C) 2008 Richel Bilderbeek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// From http://www.richelbilderbeek.nl
//---------------------------------------------------------------------------
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <vector>
#include <stdexcept>
#include <boost/shared_ptr.hpp>
#include "UnitChessBoard.h"
#include "UnitChessPiece.h"
#include "UnitCoordinatGetter.h"
#include <vcl.h>
#pragma hdrstop
#include "UnitFormSearchAndDestroyChess2.h"
#include "UnitFormPressKey.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormSearchAndDestroyChess2 *FormSearchAndDestroyChess2;
//---------------------------------------------------------------------------
__fastcall TFormSearchAndDestroyChess2::TFormSearchAndDestroyChess2(
TComponent* Owner,
const bool isWhiteHuman,
const bool isBlackHuman)
: TForm(Owner),
mIsWhiteHuman(isWhiteHuman),
mIsBlackHuman(isBlackHuman),
mCursorX(3),
mCursorY(3),
mSelectX(-1),
mSelectY(-1)
{
RandomizeTimer();
this->Width = Screen->Width / 2;
this->Height = Screen->Height / 2;
this->Left = Screen->Width / 4;
this->Top = Screen->Height / 4;
ImageSelected->Picture->Bitmap->TransparentColor = clLime;
ImageSelected->Transparent = true;
ImageCursor->Picture->Bitmap->TransparentColor = clLime;
ImageCursor->Transparent = true;
ImageCross->Picture->Bitmap->TransparentColor = clLime;
ImageCross->Transparent = true;
OnResize(0);
if (mIsWhiteHuman)
{
const String s = "Player white, please press a key to start game.";
boost::shared_ptr<TFormPressKey> form(new TFormPressKey(0,s));
form->ShowModal();
}
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFormSearchAndDestroyChess2::FormResize(TObject *Sender)
{
ImageBuffer->Picture->Bitmap->Width = ClientWidth;
ImageBuffer->Picture->Bitmap->Height = ClientHeight;
DrawChessBoard();
}
//---------------------------------------------------------------------------
void TFormSearchAndDestroyChess2::DrawChessBoard()
{
const std::vector<std::vector<bool> > inSight = mGame.GetInSight();
const double squareWidth = static_cast<double>(ClientWidth ) / 8.0;
const double squareHeight = static_cast<double>(ClientHeight) / 8.0;
//The CoordinatGetter transforms the coordinats of the board,
//according to the player whose turn it is
const CoordinatGetter coordinatGetter ( mGame.GetWhoseTurn());
//Draw squares, question marks and pieces
{
const ChessBoard board = mGame.GetBoard();
//Draw all squares on buffer (clearing it)
for (int y=0; y!=8; ++y)
{
for (int x=0; x!=8; ++x)
{
const TImage * const image
= ( (x + y) % 2 == 0 ? ImageWhiteSquare : ImageBlackSquare);
//Draw it
ImageBuffer->Canvas->StretchDraw(
TRect( TPoint( ( coordinatGetter.GetX(x) + 0 ) * squareWidth ,
( coordinatGetter.GetY(y) + 0 ) * squareHeight),
TPoint( ( coordinatGetter.GetX(x) + 1 ) * squareWidth ,
( coordinatGetter.GetY(y) + 1 ) * squareHeight)
), image->Picture->Graphic);
}
}
//Draw all question marks and pieces
for (int y=0; y!=8; ++y)
{
for (int x=0; x!=8; ++x)
{
const TImage * const image = GetImage(inSight[y][x],board.GetPiece(x,y));
if( image == 0) continue;
//Draw it
ImageBuffer->Canvas->StretchDraw(
TRect( TPoint( ( coordinatGetter.GetX(x) + 0 ) * squareWidth ,
( coordinatGetter.GetY(y) + 0 ) * squareHeight),
TPoint( ( coordinatGetter.GetX(x) + 1 ) * squareWidth ,
( coordinatGetter.GetY(y) + 1 ) * squareHeight)
), image->Picture->Graphic);
}
}
}
//Draw selected
if (mSelectX != -1)
{
//Draw selection cursor
ImageBuffer->Canvas->StretchDraw(
TRect( TPoint( ( coordinatGetter.GetX(mSelectX) + 0 ) * squareWidth ,
( coordinatGetter.GetY(mSelectY) + 0 ) * squareHeight),
TPoint( ( coordinatGetter.GetX(mSelectX) + 1 ) * squareWidth ,
( coordinatGetter.GetY(mSelectY) + 1 ) * squareHeight)
), ImageSelected->Picture->Graphic);
//If a chesspiece is selected, draw the crosses on the areas which
//are valid moves
if (mGame.GetBoard().GetPiece(mSelectX, mSelectY).IsNull() == false)
{
const std::vector<ChessMove> possibleMoves
= mGame.GetBoard().GetAllValidMoves(mSelectX,mSelectY);
typedef std::vector<ChessMove>::const_iterator Iterator;
const Iterator lastMove = possibleMoves.end();
for (Iterator possibleMove = possibleMoves.begin();
possibleMove != lastMove;
++possibleMove)
{
ImageBuffer->Canvas->StretchDraw(
TRect(
TPoint( ( coordinatGetter.GetX(possibleMove->x2) + 0 ) * squareWidth ,
( coordinatGetter.GetY(possibleMove->y2) + 0 ) * squareHeight),
TPoint( ( coordinatGetter.GetX(possibleMove->x2) + 1 ) * squareWidth ,
( coordinatGetter.GetY(possibleMove->y2) + 1 ) * squareHeight)
), ImageCross->Picture->Graphic);
}
}
}
//Draw cursor
ImageBuffer->Canvas->StretchDraw(
TRect( TPoint( ( coordinatGetter.GetX(mCursorX) + 0 ) * squareWidth ,
( coordinatGetter.GetY(mCursorY) + 0 ) * squareHeight),
TPoint( ( coordinatGetter.GetX(mCursorX) + 1 ) * squareWidth ,
( coordinatGetter.GetY(mCursorY) + 1 ) * squareHeight)
), ImageCursor->Picture->Graphic);
//Draw board and pieces to screen
this->Canvas->Draw(0,0,ImageBuffer->Picture->Graphic);
//ImageBuffer->Visible = true;
}
//---------------------------------------------------------------------------
const TImage * const TFormSearchAndDestroyChess2::GetImage(
const bool inSight,
const ChessPiece& piece) const
{
//Visible?
if (inSight== false) return ImageQuestionMark;
//Piece located there?
//No piece present? Then return null
if (piece.IsNull()==true) { return 0; }
//Piece present, draw a piece
//Which color and type?
const EnumChessPieceColor pieceColor = piece.GetColor();
switch ( piece.GetType() )
{
case king : return ( pieceColor == white ? ImageWhiteKing : ImageBlackKing );
case queen : return ( pieceColor == white ? ImageWhiteQueen : ImageBlackQueen );
case rook : return ( pieceColor == white ? ImageWhiteRook : ImageBlackRook );
case bishop: return ( pieceColor == white ? ImageWhiteBishop : ImageBlackBishop);
case knight: return ( pieceColor == white ? ImageWhiteKnight : ImageBlackKnight);
case pawn : return ( pieceColor == white ? ImageWhitePawn : ImageBlackPawn );
}
assert(!"Should not get here. Unknown piece type");
throw std::logic_error("Unknown piece type");
}
//---------------------------------------------------------------------------
void __fastcall TFormSearchAndDestroyChess2::FormKeyDown(TObject *Sender,
WORD &Key, TShiftState Shift)
{
switch (Key)
{
case VK_LEFT: case 65: //A
if (mGame.GetWhoseTurn()==white)
{
--mCursorX; if (mCursorX < 0) mCursorX = 0;
}
else
{
++mCursorX; if (mCursorX > 7) mCursorX = 7;
}
break;
case VK_RIGHT: case 68: //D
if (mGame.GetWhoseTurn()==white)
{
++mCursorX; if (mCursorX > 7) mCursorX = 7;
}
else
{
--mCursorX; if (mCursorX < 0) mCursorX = 0;
}
break;
case VK_DOWN: case 83: //W
if (mGame.GetWhoseTurn()==white)
{
--mCursorY; if (mCursorY < 0) mCursorY = 0;
}
else
{
++mCursorY; if (mCursorY > 7) mCursorY = 7;
}
break;
case VK_UP: case 87: //S
if (mGame.GetWhoseTurn()==white)
{
++mCursorY; if (mCursorY > 7) mCursorY = 7;
}
else
{
--mCursorY; if (mCursorY < 0) mCursorY = 0;
}
break;
case VK_RETURN: case VK_SPACE:
DoSelect(mCursorX, mCursorY);
default: return;
}
this->DrawChessBoard();
}
//---------------------------------------------------------------------------
void TFormSearchAndDestroyChess2::DoMove()
{
const int x1 = mSelectX;
const int y1 = mSelectY;
const int x2 = mCursorX;
const int y2 = mCursorY;
if (mSelectX == -1) return;
if (mCursorX == -1) return;
assert(x1 >=0);
assert(x2 >=0);
assert(x1 < 8);
assert(x2 < 8);
assert(y1 >=0);
assert(y2 >=0);
assert(y1 < 8);
assert(y2 < 8);
const ChessPiece piece = mGame.GetBoard().GetPiece(x1,y1);
assert(piece.IsNull()==false
&& "User must not be able to select an empty square do do a move from");
const EnumChessPieceType type = piece.GetType();
const ChessPiece victim = mGame.GetBoard().GetPiece(x2,y2);
const bool capture = (victim.IsNull()==true ? false : true);
const ChessMove move(type,x1,y1,capture,x2,y2);
this->DoMove(move);
mSelectX = -1;
mSelectY = -1;
}
//---------------------------------------------------------------------------
void TFormSearchAndDestroyChess2::DoMove(const ChessMove& move)
{
if (mGame.CanDoMove(move)==false)
{
//Nothing happens anymore...
}
else
{
//A move takes place
mGame.DoMove(move);
//Change player
//Turn off timer
Timer1->Enabled = false;
//Clear the buffer
ImageBuffer->Canvas->StretchDraw(
TRect(TPoint(0,0),TPoint(ClientWidth,ClientHeight)),
ImageQuestionMark->Picture->Graphic );
//Show the background
this->Canvas->StretchDraw(
TRect(TPoint(0,0),TPoint(ClientWidth,ClientHeight)),
ImageQuestionMark->Picture->Graphic );
//Game over?
if (mGame.IsGameOver()==true)
{
const String s
= "Player "
+ String(mGame.GetWinner() == white ? "white" : "black")
+ " has won the game!";
boost::shared_ptr<TFormPressKey> form(new TFormPressKey(0,s));
form->ShowModal();
mGame = ChessGame();
}
else
{
if (IsCurrentPlayerHuman()==true)
{
//Ask for another player
const String s
= "Player "
+ String(mGame.GetWhoseTurn() == white ? "white" : "black")
+ ", please press a key to make your move.";
boost::shared_ptr<TFormPressKey> form(new TFormPressKey(0,s));
form->ShowModal();
}
}
//Turn back timer on
Timer1->Enabled = true;
if (IsCurrentPlayerHuman()==true)
{
//On to the next player
mCursorX = std::rand() % 8;
mCursorY = std::rand() % 8;
//Draw the chessboard
DrawChessBoard();
}
}
}
//---------------------------------------------------------------------------
const bool TFormSearchAndDestroyChess2::IsCurrentPlayerHuman() const
{
return ( (mGame.GetWhoseTurn() == white && mIsWhiteHuman == true)
|| (mGame.GetWhoseTurn() == black && mIsBlackHuman == true) );
}
//---------------------------------------------------------------------------
void __fastcall TFormSearchAndDestroyChess2::Timer1Timer(TObject *Sender)
{
if (IsCurrentPlayerHuman()==false)
{
const ChessMove move = mGame.SuggestMove();
this->DoMove(move);
}
else
{
DrawChessBoard();
}
}
//---------------------------------------------------------------------------
void __fastcall TFormSearchAndDestroyChess2::FormMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if (mGame.GetWhoseTurn()==white)
{
mCursorX = 0 + (X / (ClientWidth / 8));
mCursorY = 7 - (Y / (ClientHeight / 8));
}
else
{
mCursorX = 7 - (X / (ClientWidth / 8));
mCursorY = 0 + (Y / (ClientHeight / 8));
}
if (mCursorX < 0) mCursorX = 0;
if (mCursorY < 0) mCursorY = 0;
if (mCursorX > 7) mCursorX = 7;
if (mCursorY > 7) mCursorY = 7;
DoSelect(mCursorX, mCursorY);
}
//---------------------------------------------------------------------------
void TFormSearchAndDestroyChess2::DoSelect(const int cursorX, const int cursorY)
{
if ( mSelectX == -1
&& mSelectY == -1
&& mGame.GetBoard().GetPiece(cursorX,cursorY).IsNull()==false)
{
//Nothing selected until now
mSelectX = cursorX;
mSelectY = cursorY;
}
else if (
mSelectX != -1
&& mSelectY != -1
&& mGame.GetBoard().GetPiece(mSelectX,mSelectY).IsNull() == false
&& mGame.GetBoard().GetPiece(mSelectX,mSelectY).GetColor() == mGame.GetWhoseTurn()
&& mGame.GetBoard().GetPiece(cursorX,cursorY).IsNull() == false
&& mGame.GetBoard().GetPiece(cursorX,cursorY).GetColor() == mGame.GetWhoseTurn() )
{
//Selected a piece of own color and now selecting another piece
//of own color
mSelectX = cursorX;
mSelectY = cursorY;
}
else
{
DoMove();
}
}
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppRandomizeTimer.htm
void RandomizeTimer()
{
std::srand(std::time(0));
}
//---------------------------------------------------------------------------