-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessCtrl.cpp
More file actions
442 lines (398 loc) · 13.4 KB
/
Copy pathChessCtrl.cpp
File metadata and controls
442 lines (398 loc) · 13.4 KB
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
/* Copyright (C) 2024-2026 Stefan-Mihai MOGA
This file is part of ChessCtrl application developed by Stefan-Mihai MOGA.
Fully featured Chess Control written in C++ with the help of the MFC library.
ChessCtrl is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Open
Source Initiative, either version 3 of the License, or any later version.
ChessCtrl 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
ChessCtrl. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/
// ChessCtrl.cpp : implementation file
//
#include "pch.h"
#include "ChessDemo.h"
#include "ChessCtrl.h"
#include "memdc.h"
// CChessCtrl
IMPLEMENT_DYNAMIC(CChessCtrl, CStatic)
CChessCtrl::CChessCtrl()
{
m_pColorStatic = nullptr;
m_ctrlProgress = nullptr;
m_rgbText = GetSysColor(COLOR_BTNTEXT);
m_rgbBackground = GetSysColor(COLOR_BTNFACE);
m_pCtrlBrush = new CBrush(m_rgbBackground);
m_pRedPen = new CPen(PS_SOLID, 2, RGB(0xA0, 0x00, 0x00));
m_pGreenPen = new CPen(PS_SOLID, 2, RGB(0x00, 0xA0, 0x00));
m_pBluePen = new CPen(PS_SOLID, 2, RGB(0x00, 0x00, 0xA0));
m_nSquareLength = 0;
m_nAdjustmentX = 0;
m_nAdjustmentY = 0;
m_bChessPiece = false;
m_nPreviousPoint = CPoint(-1, -1);
m_nCurrentSquare = CPoint(-1, -1);
m_bComputerPlayer = false;
}
CChessCtrl::~CChessCtrl()
{
if (m_pTextFont.GetSafeHandle())
VERIFY(m_pTextFont.DeleteObject());
if (m_pDrawFont.GetSafeHandle())
VERIFY(m_pDrawFont.DeleteObject());
if (m_pCtrlBrush != nullptr)
{
VERIFY(m_pCtrlBrush->DeleteObject());
delete m_pCtrlBrush;
m_pCtrlBrush = nullptr;
}
if (m_pRedPen != nullptr)
{
VERIFY(m_pRedPen->DeleteObject());
delete m_pRedPen;
m_pRedPen = nullptr;
}
if (m_pGreenPen != nullptr)
{
VERIFY(m_pGreenPen->DeleteObject());
delete m_pGreenPen;
m_pGreenPen = nullptr;
}
if (m_pBluePen != nullptr)
{
VERIFY(m_pBluePen->DeleteObject());
delete m_pBluePen;
m_pBluePen = nullptr;
}
}
BEGIN_MESSAGE_MAP(CChessCtrl, CStatic)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
// CChessCtrl message handlers
void CChessCtrl::PreSubclassWindow()
{
// get current font
CFont* pFont = GetFont();
if (!pFont)
{
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == NULL)
hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
if (hFont)
pFont = CFont::FromHandle(hFont);
}
ASSERT(pFont);
ASSERT(pFont->GetSafeHandle());
// create the font for this control
LOGFONT lf;
pFont->GetLogFont(&lf);
VERIFY(m_pTextFont.CreateFontIndirect(&lf));
lf.lfHeight = -MulDiv(35, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72);
VERIFY(m_pDrawFont.CreateFontIndirect(&lf));
}
BOOL CChessCtrl::OnEraseBkgnd(CDC* pDC)
{
UNREFERENCED_PARAMETER(pDC);
/*CRect cr;
GetClientRect(cr);
pDC->FillRect(&cr, m_pCtrlBrush);
// return CStatic::OnEraseBkgnd(pDC);*/
return TRUE;
}
void CChessCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Do not call CStatic::OnPaint() for painting messages
CodeProject::CMemDC pDC(&dc);
pDC.SaveDC();
CRect pRect;
GetClientRect(pRect);
m_nSquareLength = min(pRect.Width() / 10, pRect.Height() / 10);
m_nAdjustmentX = (pRect.Width() - 10 * m_nSquareLength) / 2;
m_nAdjustmentY = (pRect.Height() - 10 * m_nSquareLength) / 2;
for (int nLineIndex = 0; nLineIndex < 8; nLineIndex++)
{
const int nOffsetX = pRect.Width() - m_nAdjustmentX - (9 - nLineIndex) * m_nSquareLength;
for (int nColumnIndex = 0; nColumnIndex < 8; nColumnIndex++)
{
const int nOffsetY = pRect.Height() - m_nAdjustmentY - (9 - nColumnIndex) * m_nSquareLength;
CRect pFillRect(CPoint(nOffsetX, nOffsetY), CSize(m_nSquareLength, m_nSquareLength));
// pDC.FillSolidRect(pFillRect, (((nLineIndex + nColumnIndex) % 2) == 0) ? RGB(0xB8, 0x86, 0x0B) : RGB(0xEE, 0xE8, 0xAA));
pDC.FillSolidRect(pFillRect, (((nLineIndex + nColumnIndex) % 2) != 0) ? RGB(0xB0, 0xB0, 0xB0) : RGB(0xFF, 0xFF, 0xFF));
}
}
pDC.SetTextColor(m_rgbText);
pDC.SetBkColor(m_rgbBackground);
pDC.SetBkMode(TRANSPARENT);
pDC.SelectObject(m_pCtrlBrush);
pDC.SelectObject(&m_pTextFont);
for (int nLineIndex = 0; nLineIndex <= 8; nLineIndex++)
{
const int nOffsetX = pRect.Width() - m_nAdjustmentX - (9 - nLineIndex) * m_nSquareLength;
const int nOffsetY = pRect.Height() - m_nAdjustmentY - 9 * m_nSquareLength;
CPoint pStartPoint(nOffsetX, nOffsetY);
pDC.MoveTo(pStartPoint);
CPoint pEndPoint(nOffsetX, nOffsetY + 8 * m_nSquareLength);
pDC.LineTo(pEndPoint);
if (nLineIndex < 8)
{
CString strTextLine;
strTextLine.Format(_T("%c"), _T('A') + nLineIndex);
CRect pTextRect(CPoint(nOffsetX, nOffsetY - m_nSquareLength), CSize(m_nSquareLength, m_nSquareLength));
pDC.DrawText(strTextLine, pTextRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
pTextRect = CRect(CPoint(nOffsetX, nOffsetY + 8 * m_nSquareLength), CSize(m_nSquareLength, m_nSquareLength));
pDC.DrawText(strTextLine, pTextRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
for (int nColumnIndex = 0; nColumnIndex <= 8; nColumnIndex++)
{
const int nOffsetX = pRect.Width() - m_nAdjustmentX - 9 * m_nSquareLength;
const int nOffsetY = pRect.Height() - m_nAdjustmentY - (9 - nColumnIndex) * m_nSquareLength;
CPoint pStartPoint(nOffsetX, nOffsetY);
pDC.MoveTo(pStartPoint);
CPoint pEndPoint(nOffsetX + 8 * m_nSquareLength, nOffsetY);
pDC.LineTo(pEndPoint);
if (nColumnIndex < 8)
{
CString strTextColumn;
strTextColumn.Format(_T("%d"), 8 - nColumnIndex);
CRect pTextRect(CPoint(nOffsetX - m_nSquareLength, nOffsetY), CSize(m_nSquareLength, m_nSquareLength));
pDC.DrawText(strTextColumn, pTextRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
pTextRect = CRect(CPoint(nOffsetX + 8 * m_nSquareLength, nOffsetY), CSize(m_nSquareLength, m_nSquareLength));
pDC.DrawText(strTextColumn, pTextRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
if ((m_pChessBoard.IsWhiteTurn() || !m_bComputerPlayer) && !m_pChessBoard.HasGameEnded())
{
if ((m_nCurrentSquare.x >= 0) && (m_nCurrentSquare.x <= 7) &&
(m_nCurrentSquare.y >= 0) && (m_nCurrentSquare.y <= 7))
{
if (m_bChessPiece)
{
CPen* pOldPen = pDC.SelectObject(m_pBluePen);
if ((m_nPreviousPoint.x >= 0) && (m_nPreviousPoint.x <= 7) &&
(m_nPreviousPoint.y >= 0) && (m_nPreviousPoint.y <= 7))
{
const int nMinX = pRect.Width() - m_nAdjustmentX - 10 * m_nSquareLength + (1 + m_nPreviousPoint.x) * m_nSquareLength + 1;
const int nMinY = pRect.Height() - m_nAdjustmentY - 10 * m_nSquareLength + (1 + m_nPreviousPoint.y) * m_nSquareLength + 1;
pDC.MoveTo(CPoint(nMinX + 1, nMinY + 1));
pDC.LineTo(CPoint(nMinX + m_nSquareLength - 2, nMinY + 1));
pDC.LineTo(CPoint(nMinX + m_nSquareLength - 2, nMinY + m_nSquareLength - 2));
pDC.LineTo(CPoint(nMinX + 1, nMinY + m_nSquareLength - 2));
pDC.LineTo(CPoint(nMinX + 1, nMinY + 1));
}
if ((m_nCurrentSquare.x != m_nPreviousPoint.x) ||
(m_nCurrentSquare.y != m_nPreviousPoint.y))
{
pDC.SelectObject(IsValidMove() ? m_pGreenPen : m_pRedPen);
const int nMinX = pRect.Width() - m_nAdjustmentX - 10 * m_nSquareLength + (1 + m_nCurrentSquare.x) * m_nSquareLength + 1;
const int nMinY = pRect.Height() - m_nAdjustmentY - 10 * m_nSquareLength + (1 + m_nCurrentSquare.y) * m_nSquareLength + 1;
pDC.MoveTo(CPoint(nMinX + 1, nMinY + 1));
pDC.LineTo(CPoint(nMinX + m_nSquareLength - 2, nMinY + 1));
pDC.LineTo(CPoint(nMinX + m_nSquareLength - 2, nMinY + m_nSquareLength - 2));
pDC.LineTo(CPoint(nMinX + 1, nMinY + m_nSquareLength - 2));
pDC.LineTo(CPoint(nMinX + 1, nMinY + 1));
}
pDC.SelectObject(pOldPen);
}
else
{
CPen* pOldPen = pDC.SelectObject(m_pBluePen);
const int nMinX = pRect.Width() - m_nAdjustmentX - 10 * m_nSquareLength + (1 + m_nCurrentSquare.x) * m_nSquareLength + 1;
const int nMinY = pRect.Height() - m_nAdjustmentY - 10 * m_nSquareLength + (1 + m_nCurrentSquare.y) * m_nSquareLength + 1;
pDC.MoveTo(CPoint(nMinX + 1, nMinY + 1));
pDC.LineTo(CPoint(nMinX + m_nSquareLength - 2, nMinY + 1));
pDC.LineTo(CPoint(nMinX + m_nSquareLength - 2, nMinY + m_nSquareLength - 2));
pDC.LineTo(CPoint(nMinX + 1, nMinY + m_nSquareLength - 2));
pDC.LineTo(CPoint(nMinX + 1, nMinY + 1));
pDC.SelectObject(pOldPen);
}
}
}
pDC.SelectObject(&m_pDrawFont);
for (TCHAR i = ChessInfo::MAX_RANK; i >= ChessInfo::MIN_RANK; i--)
{
for (TCHAR j = ChessInfo::MIN_FILE; j <= ChessInfo::MAX_FILE; j++)
{
try
{
wstring fileRank({ j, i });
Piece* piece = m_pChessBoard._board->at(fileRank);
if (piece != nullptr)
{
const int nOffsetX = pRect.Width() - m_nAdjustmentX - (2 + (ChessInfo::MAX_FILE - j)) * m_nSquareLength;
const int nOffsetY = pRect.Height() - m_nAdjustmentY - (2 + (i - ChessInfo::MIN_RANK)) * m_nSquareLength;
CRect pTextRect(CPoint(nOffsetX, nOffsetY), CSize(m_nSquareLength, m_nSquareLength));
pDC.DrawText(piece->toGraphics().c_str(), pTextRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
}
}
}
pDC.RestoreDC(-1);
}
void CChessCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
UNREFERENCED_PARAMETER(nFlags);
m_nCurrentSquare = CPoint(-1, -1);
const int nMinX = m_nAdjustmentX + m_nSquareLength;
const int nMinY = m_nAdjustmentY + m_nSquareLength;
const int nMaxX = nMinX + 8 * m_nSquareLength;
const int nMaxY = nMinY + 8 * m_nSquareLength;
if ((m_pChessBoard.IsWhiteTurn() || !m_bComputerPlayer) && !m_pChessBoard.HasGameEnded())
{
if ((nMinX < point.x) && (point.x < nMaxX) &&
(nMinY < point.y) && (point.y < nMaxY))
{
m_nCurrentSquare.x = (point.x - nMinX) / m_nSquareLength;
m_nCurrentSquare.y = (point.y - nMinY) / m_nSquareLength;
if (!m_bChessPiece)
{
if ((m_nCurrentSquare.x != m_nPreviousPoint.x) ||
(m_nCurrentSquare.y != m_nPreviousPoint.y))
{
m_nPreviousPoint = m_nCurrentSquare;
RedrawWindow();
UpdateWindow();
if (m_pColorStatic != nullptr)
{
m_pColorStatic->RedrawWindow();
m_pColorStatic->UpdateWindow();
}
if (m_ctrlProgress != nullptr)
{
m_ctrlProgress->RedrawWindow();
m_ctrlProgress->UpdateWindow();
}
}
}
else
{
if ((m_nCurrentSquare.x != m_nPreviousPoint.x) ||
(m_nCurrentSquare.y != m_nPreviousPoint.y))
{
RedrawWindow();
UpdateWindow();
if (m_pColorStatic != nullptr)
{
m_pColorStatic->RedrawWindow();
m_pColorStatic->UpdateWindow();
}
if (m_ctrlProgress != nullptr)
{
m_ctrlProgress->RedrawWindow();
m_ctrlProgress->UpdateWindow();
}
}
}
}
}
CStatic::OnMouseMove(nFlags, point);
}
void CChessCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
if ((m_pChessBoard.IsWhiteTurn() || !m_bComputerPlayer) && !m_pChessBoard.HasGameEnded())
{
if ((m_nCurrentSquare.x >= 0) && (m_nCurrentSquare.x <= 7) &&
(m_nCurrentSquare.y >= 0) && (m_nCurrentSquare.y <= 7))
{
if (!m_bChessPiece)
{
try
{
wstring fileRank({ (TCHAR)(ChessInfo::MIN_FILE + m_nCurrentSquare.x), (TCHAR)(ChessInfo::MAX_RANK - m_nCurrentSquare.y) });
Piece* piece = m_pChessBoard._board->at(fileRank);
m_strMoveFrom = fileRank;
if (piece != nullptr)
{
m_bChessPiece = true;
RedrawWindow();
UpdateWindow();
if (m_pColorStatic != nullptr)
{
m_pColorStatic->RedrawWindow();
m_pColorStatic->UpdateWindow();
}
if (m_ctrlProgress != nullptr)
{
m_ctrlProgress->RedrawWindow();
m_ctrlProgress->UpdateWindow();
}
}
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
}
}
else
{
m_bChessPiece = false;
// Move piece
try
{
wstring fileRank({ (TCHAR)(ChessInfo::MIN_FILE + m_nCurrentSquare.x), (TCHAR)(ChessInfo::MAX_RANK - m_nCurrentSquare.y) });
m_strMoveTo = fileRank;
m_pChessBoard.submitMove(m_strMoveFrom.c_str(), m_strMoveTo.c_str());
RedrawWindow();
UpdateWindow();
if (m_pColorStatic != nullptr)
{
m_pColorStatic->RedrawWindow();
m_pColorStatic->UpdateWindow();
}
if (m_ctrlProgress != nullptr)
{
m_ctrlProgress->RedrawWindow();
m_ctrlProgress->UpdateWindow();
}
if (m_bComputerPlayer && !m_pChessBoard.IsWhiteTurn() && !m_pChessBoard.HasGameEnded())
{
m_pChessBoard.ComputerPlayer();
/* RedrawWindow();
UpdateWindow();
if (m_pColorStatic != nullptr)
{
m_pColorStatic->RedrawWindow();
m_pColorStatic->UpdateWindow();
}
if (m_ctrlProgress != nullptr)
{
m_ctrlProgress->RedrawWindow();
m_ctrlProgress->UpdateWindow();
}*/
}
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
}
}
}
}
CStatic::OnLButtonDown(nFlags, point);
}
bool CChessCtrl::IsValidMove()
{
try
{
wstring fileRank({ (TCHAR)(ChessInfo::MIN_FILE + m_nCurrentSquare.x), (TCHAR)(ChessInfo::MAX_RANK - m_nCurrentSquare.y) });
Piece* piece = m_pChessBoard._board->at(m_strMoveFrom);
if (piece != nullptr)
{
return (ChessErrHandler::CHESS_NO_ERROR == piece->isValidMove(m_strMoveFrom, fileRank, m_pChessBoard._board));
}
}
catch (const std::out_of_range& err)
{
UNREFERENCED_PARAMETER(err);
}
return false;
}