-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWindow.cpp
305 lines (274 loc) · 7.92 KB
/
Window.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
#include"Window.h"
static Window *window = nullptr;
VOID CALLBACK TimerProc(
HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime
)
{
switch (uMsg)
{
case WM_TIMER:
window->m_board->robot();
window->m_board->setAvailable();
break;
default:
break;
}
KillTimer(hwnd, 0);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_LBUTTONDOWN:
{
auto Pos = MAKEPOINTS(lParam);
int xPos = Pos.x;
int yPos = Pos.y;
if (window->m_board->isnoAvailable && (xPos >= 375 && xPos <= 425) && (yPos >= 450 && yPos < 500)) { //confirm button
window->m_board->robot();
}
if ((xPos>=25 && xPos<=425) && (yPos>=25 && yPos<=425) ) {
// in rect
xPos -= 25;
xPos /= 50;
yPos -= 25;
yPos /= 50;
if (window->m_board->getBoard()[yPos][xPos] != BlockStatus::AVAILABLE) {
return 0;
}
window->m_board->mouseClick(yPos, xPos);
SetTimer(hWnd, 0, 100, (TIMERPROC)TimerProc);
// window->m_board->robot();
// window->m_board->setAvailable();
}
}
break;
case WM_MOUSEMOVE:
{
auto Pos = MAKEPOINTS(lParam);
int xPos = Pos.x;
int yPos = Pos.y;
if ((xPos >= 25 && xPos <= 425) && (yPos >= 25 && yPos <= 425)) {
// in rect
xPos -= 25;
xPos /= 50;
yPos -= 25;
yPos /= 50;
if (window->m_board->getBoard()[yPos][xPos] == BlockStatus::EMPTY || window->m_board->getBoard()[yPos][xPos] ==BlockStatus::AVAILABLE) {
HCURSOR cur = LoadCursor(NULL, IDC_HAND);
SetCursor(cur);
}
}
else {
HCURSOR cur = LoadCursor(NULL, IDC_ARROW);
SetCursor(cur);
}
}
break;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
DestroyWindow(hWnd);
}
break;
case WM_CHAR:
if (wParam == 'R' || wParam == 'r') {
window->m_board->restart();
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Window::Window(HINSTANCE hInstance) {
// Firstly, Window
wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wndClass.hIconSm = wndClass.hIcon;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = L"Othello";
RegisterClassEx(&wndClass);
m_hWnd = CreateWindow(
L"Othello",
L"Othello",
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(m_hWnd, SW_SHOW);
UpdateWindow(m_hWnd);
// Secondly, Font
t_hFont = CreateFont(
20, // logical height of font height
0, // logical average character width
0, // angle of escapement
0, // base-line orientation angle
FW_LIGHT, // font weight
0, // italic attribute flag
0, // underline attribute flag
0, // strikeout attribute flag
DEFAULT_CHARSET, // character set identifier
OUT_DEFAULT_PRECIS, // output precision
CLIP_CHARACTER_PRECIS, // clipping precision
DEFAULT_QUALITY, // output quality
DEFAULT_PITCH | FF_DONTCARE, // pitch and family
L"¿¬Ìå" // pointer to typeface name string
);
m_hFont = CreateFont(
50, // logical height of font height
0, // logical average character width
0, // angle of escapement
0, // base-line orientation angle
FW_LIGHT, // font weight
0, // italic attribute flag
0, // underline attribute flag
0, // strikeout attribute flag
DEFAULT_CHARSET, // character set identifier
OUT_DEFAULT_PRECIS, // output precision
CLIP_CHARACTER_PRECIS, // clipping precision
DEFAULT_QUALITY, // output quality
DEFAULT_PITCH | FF_DONTCARE, // pitch and family
L"΢ÈíÑźÚ" // pointer to typeface name string
);
m_whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
m_blackBrush = CreateSolidBrush(RGB(0, 0, 0));
m_grayBrush = CreateSolidBrush(RGB(128, 128, 128));
m_greenBrush = CreateSolidBrush(RGB(0, 255, 00));
m_lightBlueBrush = CreateSolidBrush(RGB(0xBE, 0xEF, 0xFF));
m_board = new Board;
}
Window::~Window() {
delete m_board;
DeleteObject(m_hFont);
DeleteObject(t_hFont);
DeleteObject(m_blackBrush);
DeleteObject(m_whiteBrush);
DeleteObject(m_grayBrush);
DeleteObject(m_lightBlueBrush);
DeleteObject(m_greenBrush);
UnregisterClass(L"Othello", wndClass.hInstance);
}
void Window::drawText(HDC hDC,std::wstring str, RECT rc,UINT format) {
DrawText(hDC, str.c_str(), str.length(), &rc, format);
}
void Window::run() {
DWORD nowTime,prevRenderTime = GetTickCount();
MSG msg = { 0 };
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
nowTime = GetTickCount();
if (nowTime - prevRenderTime >= 100) {
prevRenderTime = nowTime;
// Get DC and create Buffer
HDC wDC = GetDC(m_hWnd);
HDC tDC = CreateCompatibleDC(wDC);
HBITMAP bufferBMP = CreateCompatibleBitmap(wDC, 800, 600);
RECT rc = { 0,0,800,600 };
SelectObject(tDC, bufferBMP);
FillRect(tDC, &rc, m_grayBrush);
// Start rendering
SelectObject(tDC, m_hFont);
SetBkMode(tDC, TRANSPARENT);
for (int i = 0; i < 9; ++i) {
MoveToEx(tDC, i * 50 + 25, 25, NULL);
LineTo(tDC, i * 50 + 25, 425);
}
for (int i = 0; i < 9; ++i) {
MoveToEx(tDC, 25, i * 50 + 25, NULL);
LineTo(tDC, 425, i * 50 + 25);
}
auto board = m_board->getBoard();
int blackCount = 0, whiteCount = 0;
SelectObject(tDC, m_blackBrush);
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
if (board[i][j] == BlockStatus::BLACK) {
Ellipse(tDC, j * 50 + 25, i * 50 + 25, j * 50 + 75, i * 50 + 75);
++blackCount;
}
}
}
SelectObject(tDC, m_whiteBrush);
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
if (board[i][j] == BlockStatus::WHITE) {
Ellipse(tDC, j * 50 + 25, i * 50 + 25, j * 50 + 75, i * 50 + 75);
++whiteCount;
}
}
}
SelectObject(tDC, m_lightBlueBrush);
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
if (board[i][j] == BlockStatus::AVAILABLE) {
Ellipse(tDC, j * 50 + 40, i * 50 + 40, j * 50 + 60, i * 50 + 60);
}
}
}
SetTextColor(tDC, RGB(0, 0, 0));
drawText(tDC, std::to_wstring(blackCount),{490,25,545,75}, DT_CENTER);
SetTextColor(tDC, RGB(0, 255, 0));
drawText(tDC, L":", { 545,25,555,75 }, DT_CENTER);
SetTextColor(tDC, RGB(255, 255, 255));
drawText(tDC, std::to_wstring(whiteCount), { 555,25,610,75 }, DT_CENTER);
if (m_board->isGameOver) {
SetTextColor(tDC, RGB(255, 0, 0));
drawText(tDC, L"Game Over!\nPress R to restart!", { 425,100,800,200 }, DT_CENTER);
if (m_board->isFull && whiteCount < blackCount || whiteCount == 0) {
SetTextColor(tDC, RGB(0, 0, 0));
drawText(tDC, L"Black Win!", { 500,200,700,300 }, DT_CENTER);
}
else if ( m_board->isFull && whiteCount == blackCount ) {
SetTextColor(tDC, RGB(80, 80, 80));
drawText(tDC, L"Draw!", { 500,200,700,300 }, DT_CENTER);
}
else if((m_board->isFull && whiteCount > blackCount) || blackCount == 0){
SetTextColor(tDC, RGB(255, 255, 255));
drawText(tDC, L"White Win!", { 500,200,700,300 }, DT_CENTER);
}
}
else if (m_board->isnoAvailable) {
SelectObject(tDC, t_hFont);
SetTextColor(tDC, RGB(255, 255, 255));
drawText(tDC, L"AI GO AGAIN: Confirm", { 200,450,425,500 }, DT_CENTER);
}
else {
drawText(tDC, L"", { 200,450,425,500 }, DT_CENTER);
}
// Blit buffer to screen
BitBlt(wDC, 0, 0,
800, 600, tDC, 0, 0, SRCCOPY);
DeleteObject(tDC);
DeleteObject(bufferBMP);
ReleaseDC(m_hWnd, wDC);
}
}
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) {
window = new Window(hInstance);
window->run();
delete window;
return 0;
}