-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
208 lines (169 loc) · 4.86 KB
/
Game.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
#include "StdAfx.h"
#include <iostream>
#pragma warning(disable : 4995)
#include "strsafe.h"
#include "Game.h"
#include "Piece.h"
#include "Board.h"
#include "King.h"
using namespace std;
extern HANDLE g_hQuit;
DWORD WINAPI gameThread( LPVOID lpParam );
void errorHandler(LPTSTR lpszFunction);
CGame::CGame(void)
{
DWORD dwThreadId = 0;
HANDLE hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
gameThread, // thread function name
NULL, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
// If CreateThread fails, terminate execution.
// This will automatically clean up threads and memory.
if (hThread == NULL)
{
errorHandler(TEXT("CreateThread"));
ExitProcess(3);
}
}
CGame::~CGame(void)
{
}
void errorHandler(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code.
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message.
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR) lpMsgBuf) + lstrlen((LPCTSTR) lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR) lpDisplayBuf, TEXT("Error"), MB_OK);
// Free error-handling buffer allocations.
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
// NOTE: REMOVE MAGIC NUMBERS THROUGHOUT CODE
DWORD WINAPI gameThread( LPVOID lpParam )
{
// get user input, do move, verify check status, display result on screen, repeat
bool bQuit = false;
EColour eCurrentColour = eWhite;
cout << endl << "CONSOLE CHESS BY OLIVER ERNSTER: Version 1.0" << endl << endl << endl;
// initialise game
CBoard* pBoard = CBoard::Instance();
std::string inputMove;
std::string sourcePos;
std::string targetPos;
sourcePos.reserve(2);
targetPos.reserve(2);
while (!bQuit)
{
// display board
pBoard->display();
// get move from user
cout << endl << "Please enter your move in the form <letter><number>-><letter><number>" << endl;
cout << "For example: b2->b3" << endl;
if (eCurrentColour == eWhite)
{
cout << endl << "WHITE to move:" << endl;
}
else
{
cout << endl << "BLACK to move:" << endl;
}
inputMove.clear();
cin >> inputMove;
if (strcmp(inputMove.c_str(), "quit") == 0)
{
bQuit = true;
break;
}
sourcePos.clear();
targetPos.clear();
sourcePos += *(inputMove.begin());
sourcePos += *(inputMove.begin()+1);
targetPos += *(inputMove.begin()+4);
targetPos += *(inputMove.begin()+5);
sourcePos[0] = toupper(sourcePos[0]);
sourcePos[1] = toupper(sourcePos[1]);
targetPos[0] = toupper(targetPos[0]);
targetPos[1] = toupper(targetPos[1]);
CPiece* pPiece = pBoard->getPiece(sourcePos);
if (pPiece == NULL)
{
cout << "No piece found at source position" << endl;
continue;
}
if (pPiece->getColour() == eCurrentColour)
{
// do move
if (!pPiece->move(sourcePos, targetPos))
{
cout << "Move failed! Invalid target position?" << endl;
}
if (eCurrentColour == eWhite)
{
eCurrentColour = eBlack;
}
else
{
eCurrentColour = eWhite;
}
}
else
{
cout << endl << "INVALID PIECE COLOUR!" << endl;
continue;
}
// does it place the nemesis player in check?
CKing* pKing = dynamic_cast<CKing*> (pPiece->getKing(eCurrentColour));
if (pKing != NULL)
{
if (pKing->inCheck())
{
if (eCurrentColour == eWhite)
{
cout << endl << "WHITE is in CHECK!" << endl << endl;
}
else
{
cout << endl << "BLACK is in CHECK!" << endl << endl;
}
}
// does it place the nemesis player in check mate?
// if it does, report winner and set bQuit = true;
if (pKing->checkMate())
{
if (eCurrentColour == eWhite)
{
cout << endl << "WHITE has been CHECK MATED! BLACK WINS!" << endl << endl;
}
else
{
cout << endl << "BLACK has been CHECK MATED! WHITE WINS!" << endl << endl;
}
//bQuit = true;
}
}
}
pBoard = NULL;
SetEvent(g_hQuit);
return 0;
}