-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
389 lines (359 loc) · 9.22 KB
/
Main.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
#ifndef UNICODE
#define UNICODE
#endif
#define DEBUG 0
#include <windows.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#include <stack>
#include "Reversi.h"
using namespace std;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
const string uBLACK = "●", uWHITE = "○", uEMPTY = "-", uPLAYABLE = "*";
int depth = 0, delayTime = 1000;
Reversi reversi;
bool key_reg[16] = { 0 };
bool isAIMode = false, aiPlayer = true, aiSetting = false;
bool isHello = true;
void paintPanel();
pair<int, int> aiMove();
int aiVal = 0;
const char helloScreen[30][121] = {
"",
"",
"",
"",
" _______ _______ _______ _______ _______ _________ ",
" ( ____ )( ____ \\|\\ /|( ____ \\( ____ )( ____ \\\\__ __/",
" | ( )|| ( \\/| ) ( || ( \\/| ( )|| ( \\/ ) (",
" | (____)|| (__ | | | || (__ | (____)|| (_____ | |",
" | __)| __) ( ( ) )| __) | __)(_____ ) | |",
" | (\\ ( | ( \\ \\_/ / | ( | (\\ ( ) | | |",
" | ) \\ \\__| (____/\\ \\ / | (____/\\| ) \\ \\__/\\____) |___) (___",
" |/ \\__/(_______/ \\_/ (_______/|/ \\__/\\_______)\\_______/",
" B10515009ChengYao(Ver 1.0 9/24/2017)",
"",
"",
"",
"",
"",
" [Press shift to play]"
};
char aiSettingScreen[30][121] = {
" _ _ ____ ",
" /\\ | | | | | _ \\ ",
" / \\ | |_ __ | |__ __ _| |_) | __ _ _ __ __ _ ",
" / /\\ \\ | | '_ \\| '_ \\ / _` | _ < / _` | '_ \\ / _` |",
" / ____ \\| | |_) | | | | (_| | |_) | (_| | | | | (_| |",
"/_/ \_\\_| .__/|_| |_|\\__,_|____/ \\__,_|_| |_|\\__, |",
" | | __/ |",
" |_| |___/ ",
" Select Level",
"",
" (1)Easy",
" (2)Middle",
" (3)Hard(not that hard)",
" Select COLOR",
"",
" (1)●",
" (2)○"
};
stack<Reversi> past, future;
void keyinput() {
int x = -1, y = -1;
for (int i = 0; i < 8; i++) {
if (key_reg[i]) {
y = i + 1;
}
if (key_reg[i + 8]) {
x = i + 1;
}
}
if (x != -1 && y != -1) {
while (!future.empty()) {
future.pop();
}
past.push(reversi);
if (reversi.check(x, y)) {
reversi.setBW(x, y);
}
}
}
pair<int, int> aiMove() {
Reversi tmp;
tmp = reversi;
reversi.isEnd();// for playable
float value = aiPlayer ? -inf : inf;
pair<int, int> move;
for (int x = 1; x <= 8; ++x) {
for (int y = 1; y <= 8; ++y) {
if (reversi.getBW(x, y) == reversi.ePLAYABLE) {
tmp.setBW(x, y);
float itmp = minmax(tmp, depth, -inf, inf);
if (aiPlayer) {
if (itmp > value) {
value = itmp;
move.first = x;
move.second = y;
}
}
else {
if (itmp < value) {
value = itmp;
move.first = x;
move.second = y;
}
}
tmp = reversi;
}
}
}
aiVal = value;
return move;
}
void KeyDownEvent(WPARAM wParam)
{
//==== 英文字母或數字 ====//
if (wParam >= '1' &&wParam <= '8') {
key_reg[wParam - '1' + 8] = true;
}
else if (wParam >= 'A'&&wParam <= 'H') {
key_reg[wParam - 'A'] = true;
}
else if (wParam >= 'a' && wParam <= 'h') {
key_reg[wParam - 'a'] = true;
}
keyinput();
system("CLS");
paintPanel();
}
void KeyUpEvent(WPARAM wParam)
{
if (isHello && wParam != VK_SHIFT) {
return;
}
//==== Tab、Enter、Ctrl... ====//
if (wParam == VK_TAB) {
// redo
if (isAIMode) return;
if (!future.empty()) {
past.push(reversi);
reversi = future.top();
future.pop();
}
}
else if (wParam == VK_BACK) {
// undo
if (isAIMode && !past.empty()) {
past.pop();
if (!past.empty()) {
future.push(reversi);
reversi = past.top();
past.pop();
}
}
else if (!past.empty()) {
future.push(reversi);
reversi = past.top();
past.pop();
}
}
else if (wParam == VK_RETURN) {
reversi.init();
reversi.isEnd();// for hint
}
else if (wParam == VK_SPACE) {
if (!isAIMode && !aiSetting) {
aiSetting = true;
reversi.init();
//isAIMode = true;
//
//ai is black
}
else if (isAIMode) {
isAIMode = false;
depth = 0;
}
else {
aiSetting = false;
depth = 0;
}
}
else if (wParam == VK_SHIFT) {
isHello = false;
}
else if (wParam >= '1' &&wParam <= '8') {
key_reg[wParam - '1' + 8] = false;
if (aiSetting) {
if (!depth) {
if (wParam < '4') {
depth = 5 + wParam - '1';
}
}
else {
if (wParam == '1') {
aiPlayer = true;
}
else if (wParam == '2') {
aiPlayer = false;
}
isAIMode = true;
aiSetting = false;
}
}
}
else if (wParam >= 'A'&&wParam <= 'H') {
key_reg[wParam - 'A'] = false;
}
else if (wParam >= 'a' && wParam <= 'h') {
key_reg[wParam - 'a'] = false;
}
system("CLS");
paintPanel();
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
//==== 註冊視窗類別 ====//
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
//==== 創建視窗 ====//
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // 視窗類別
L"Reversi", // 視窗標題
WS_OVERLAPPEDWINDOW, // 視窗風格
0, 0, 5, 5, // 視窗大小及位置
NULL, // Parent 視窗
NULL, // 選單
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
//==== 創建 console 視窗 ====//
AllocConsole();
freopen("CONOUT$", "wb", stdout);
ShowWindow(hwnd, nCmdShow);
//==== 執行訊息迴圈 ====//
reversi.init();
reversi.isEnd();
MSG msg = {};
system("CLS");
paintPanel();
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (isAIMode && reversi.isBW() == aiPlayer) {
//ai move
cout << "AI thinking . . ." << endl;
cout.flush();
pair<int, int> move = aiMove();
past.push(reversi);
reversi.setBW(move.first, move.second);
system("CLS");
paintPanel();
}
//SetForegroundWindow(hwnd);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
}
return 0;
case WM_KEYDOWN:
KeyDownEvent(wParam);
break;
case WM_KEYUP:
KeyUpEvent(wParam);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void paintPanel() {
bool isEnd = reversi.isEnd();
if (isHello) {
for (int i = 0; i <= 18; ++i) {
cout << helloScreen[i] << endl;
}
return;
}
else if (aiSetting) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
for (int i = 0; i < 8; ++i) {
cout << aiSettingScreen[i] << endl;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
for (int i = 8; i < 13 && !depth; ++i) {
cout << aiSettingScreen[i] << endl;
}
for (int i = 13; i < 17 && depth; ++i) {
cout << aiSettingScreen[i] << endl;
}
return;
}
string coordinate = "abcdefgh12345678";// 2 char wide for each
cout << " " << coordinate.substr(0, 16) << endl;//1~8
for (int x = 1; x <= 8; ++x) {
cout << coordinate.substr(16 + 2 * (x - 1), 2);
for (int y = 1; y <= 8; ++y) {
if (key_reg[x + 7] || key_reg[y - 1])
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_BLUE | FOREGROUND_INTENSITY);
char state = reversi.getBW(x, y);
if (state == reversi.eBLACK) {
cout << uBLACK;
}
else if (state == reversi.eWHITE) {
cout << uWHITE;
}
else if (state == reversi.ePLAYABLE) {
cout << uPLAYABLE;
}
else {
cout << uEMPTY;
}
if (key_reg[x + 7] || key_reg[y - 1])
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
cout << (key_reg[7 + x] ? "←" : " ") << endl;
}
cout << " ";
for (int i = 0; i < 8; ++i) {
cout << (key_reg[i] ? "↑" : " ");
}
cout << endl;
cout << "Now: " << (reversi.isBW() ? uBLACK : uWHITE) << endl;
cout << uBLACK << ": " << reversi.BCount() << " " << uWHITE << ": " << reversi.WCount() << endl;
cout << "undo: backspace redo: tab\nAI Mode(" << (isAIMode ? "off" : "on") << "): space reset: enter" << endl;
if (isAIMode) cout << "AI Mode is on, redo is disable! AI plays " << (aiPlayer ? uBLACK : uWHITE) << endl;
if (isEnd) {
int result = reversi.BCount() - reversi.WCount();
if (result != 0) {
cout << "Winner is " << (result > 0 ? uBLACK : uWHITE) << "!" << endl;
}
else {
cout << "This game was drawn~" << endl;
}
}
if (DEBUG) cout << "AI value: " << aiVal << "\nSteps: " << reversi.getSteps() << endl;
}