-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
824 lines (725 loc) · 19.6 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#include <windows.h>
#include <chrono>
#include <stdint.h>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include "Game.h"
#define CmdWidth 1200
#define CmdHeight 650
#define MainMenuLineColor 9
#define MainMenuChoosenColor 240
#define InitColor 7
#define BlackColor 128
#define RedColor 132
#define BoardColor 240
#define BoardNumColor 253
#define eibleColor 16
#define movableColor 128
#define HintColor 3
#define UpArrowKey 38
#define LeftArrowKey 37
#define RightArrowKey 39
#define DownArrowKey 40
#define EnterKey 13
#define EscKey 27
#define OKey 79
#define PKey 80
#define MainMenuTextInitX 63
#define MainMenuTextInitY 12
#define BoardInitX 38
#define BoardInitY 2
#define WindowBottomY 29
#define BoardRow 10
#define BoardCol 9
using namespace std;
namespace fs = std::experimental::filesystem;
const string savePath = "./save";
const string chessText = "將士象車馬包卒帥仕相車傌炮兵";
const string mainMenuText[3] = {"開新遊戲", "讀取存檔", "離開遊戲"};
const string gameMenuText[4] = {"繼續遊戲", "保存遊戲", "重新開始", "退出遊戲"};
//const string BoardText[8] = {"123456789", "車馬象士將士象馬車", "砲", "卒", "兵", "炮", "車傌相仕帥仕相傌車", "九八七六五四三二一" };
string *Board = new string[23];
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
bool running = true;
bool gameRunning = true;
bool gameMenuOpen = false;
bool holdChess = false;
int mainMenuPosition = 0;
int gameMenuPosition = 0;
Coord cursor;
void init();
void GoToXY(int, int);
void delay(int);
DWORD getKey();
void storeSavedFilename(int &, vector<string> &);
void initCmdWindow(int, int);
void colorBoard(ChessBoard);
void colorMainMenuCursor();
void drawMainMenu();
void moveMainMenu(int);
void loadGame();
void showHoldHint(int);
void drawGameMenu(Game &);
void printGameFormat();
void moveCursor(int);
void startNewGame();
void mainMenuAction();
void gameMenuAction(Game &);
void testColor();
void init()
{
SetConsoleTextAttribute(hConsole, InitColor);
ios_base::sync_with_stdio(false);
cin.tie(0);
// system("chcp 65001");
system("CHCP 950"); // BIG-5
}
void GoToXY(int column, int line)
{
COORD coord;
coord.X = column;
coord.Y = line;
if (!SetConsoleCursorPosition(hConsole, coord))
{
cout << "移動游標error";
}
}
void delay(int time)
{
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
DWORD getKey()
{ // get user keydown
INPUT_RECORD irInput;
DWORD InputsRead = 0;
ReadConsoleInput(hInput, &irInput, 1, &InputsRead);
if (irInput.Event.KeyEvent.bKeyDown)
return irInput.Event.KeyEvent.wVirtualKeyCode;
else
return 0;
}
void storeSavedFilename(int &counter, vector<string> &filename)
{
cout << "目前存檔:\n";
int i = 1;
for (auto &p : fs::directory_iterator(savePath))
{
fs::path px = p.path();
if (px.extension() == ".txt")
{
cout << i++ << " : " << px.stem() << endl;
filename.push_back(px.string());
counter++;
}
}
}
void initCmdWindow(int width = CmdWidth, int height = CmdHeight)
{ // set cmd window's width and height
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, 100, 100, width, height, TRUE);
}
void showReport(vector<string> reports)
{
const int report_index_x = 20;
int size = reports.size();
int start_index = (reports.size()) > 19 ? reports.size() - 19 : 0;
//while (reports.size() > 19)
// reports.erase(reports.begin());
for (int i = 0, j = start_index; i < 19 && j < size; i++, j++)
{
GoToXY(10, i + 2);
SetConsoleTextAttribute(hConsole, InitColor);
cout << j+1 << " ";
if (j % 2 == 1)
{
SetConsoleTextAttribute(hConsole, BlackColor);
cout << "黑";
}
else
{
SetConsoleTextAttribute(hConsole, RedColor);
cout << "紅";
}
SetConsoleTextAttribute(hConsole, InitColor);
cout << ":" << reports[j];
}
}
void drawPrompt(vector<Coord> move, vector<Coord> capture, Coord origin)
{
int start_index_x = 37;
int end_x = 70;
int start_y = BoardInitY;
int end_y = BoardInitY + BoardRow;
Coord coo;
SetConsoleTextAttribute(hConsole, movableColor);
for (vector<Coord>::iterator it = move.begin(); it != move.end(); ++it)
{
GoToXY(BoardInitX - 1 + it->x * 4, BoardInitY + it->y * 2);
cout << " ";
}
SetConsoleTextAttribute(hConsole, eibleColor);
for (vector<Coord>::iterator it = capture.begin(); it != capture.end(); ++it)
{
GoToXY(BoardInitX - 1 + it->x * 4, BoardInitY + it->y * 2);
cout << " ";
}
GoToXY(4 * origin.x + BoardInitX, 2 * origin.y + BoardInitY);
}
void showHint(unsigned short player) {
int x = 87;
int y = 4;
GoToXY(x, y);
if (player == PLAYER_BLACK)
{
SetConsoleTextAttribute(hConsole, BlackColor);
cout << "黑色方";
}
else
{
SetConsoleTextAttribute(hConsole, RedColor);
cout << "紅色方";
}
}
void colorBoard(ChessBoard obj)
{ // color Board
int start_index_x = 37;
int end_x = 70;
int start_y = BoardInitY;
int end_y = BoardInitY + BoardRow;
Coord coo;
int kindCode;
//GoToXY()
SetConsoleTextAttribute(hConsole, BoardNumColor);
GoToXY(BoardInitX - 1, BoardInitY - 1);
SetConsoleTextAttribute(hConsole, BoardNumColor);
for (int i = start_index_x; i <= end_x; i++)
cout << Board[1][i];
for (int i = 2, y = 0; i < 21; i++)
{
GoToXY(BoardInitX - 1, BoardInitY + i - 2);
for (int j = start_index_x, x = 0; j <= end_x; j += 4)
{
coo.x = x;
coo.y = y;
kindCode = obj.getChess(coo).getKind();
if (kindCode >= 1 && kindCode <= 7) // black
SetConsoleTextAttribute(hConsole, BlackColor);
else if (kindCode >= 8 && kindCode <= 14) // red
SetConsoleTextAttribute(hConsole, RedColor);
else
SetConsoleTextAttribute(hConsole, BoardColor);
if (i % 2 == 1)
SetConsoleTextAttribute(hConsole, BoardColor);
if (kindCode != -1 && i % 2 == 0)
{
for (int k = 2 * (kindCode - 1); k < 2 * kindCode; k++)
cout << chessText[k];
}
else
{
for (int k = j; k < j + 2; k++)
cout << Board[i][k];
}
SetConsoleTextAttribute(hConsole, BoardColor);
for (int k = j + 2; k < j + 4 && k <= end_x; k++)
cout << Board[i][k];
if (i != 1 && i != 21 && i % 2 == 0)
x++;
}
if (i % 2 == 0)
{
y++;
}
GoToXY(BoardInitX - 1, BoardInitY + 19);
SetConsoleTextAttribute(hConsole, BoardNumColor);
for (int i = start_index_x; i <= end_x; i++)
cout << Board[21][i];
}
GoToXY(BoardInitX, BoardInitY);
}
void colorMainMenuCursor()
{ // color menu's cursor
SetConsoleTextAttribute(hConsole, MainMenuLineColor);
for (int i = MainMenuTextInitY, j = 0; i <= MainMenuTextInitY + 4; i += 2, j++)
{
GoToXY(MainMenuTextInitX, i);
cout << mainMenuText[j];
}
SetConsoleTextAttribute(hConsole, MainMenuChoosenColor);
GoToXY(MainMenuTextInitX, MainMenuTextInitY + mainMenuPosition * 2);
cout << mainMenuText[mainMenuPosition];
GoToXY(0, WindowBottomY);
}
void colorGameMenuCursor()
{ // color game menu's cursor
SetConsoleTextAttribute(hConsole, MainMenuLineColor);
for (int i = MainMenuTextInitY, j = 0; i <= MainMenuTextInitY + 4; i += 2, j++)
{
GoToXY(MainMenuTextInitX, i);
cout << gameMenuText[j];
}
SetConsoleTextAttribute(hConsole, MainMenuChoosenColor);
GoToXY(MainMenuTextInitX, MainMenuTextInitY + mainMenuPosition * 2);
cout << gameMenuText[gameMenuPosition];
GoToXY(0, WindowBottomY);
}
void showWinning(int num) {
int x = 86;
int y = 6;
GoToXY(x, y);
if (num == PLAYER_BLACK + 1)
{
SetConsoleTextAttribute(hConsole, BlackColor);
cout << "黑方獲勝";
}
else if (num == PLAYER_RED + 1)
{
SetConsoleTextAttribute(hConsole, RedColor);
cout << "紅方獲勝";
}
else
{
cout << " ";
}
}
void drawMainMenu()
{
int index = mainMenuPosition;
int space = 30;
int blank_row = 11;
system("CLS");
SetConsoleTextAttribute(hConsole, MainMenuLineColor);
for (int i = 0; i < blank_row; i++)
cout << "\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "┌─────┐\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ " << mainMenuText[0] << " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "├─────┤\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ " << mainMenuText[1] << " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "├─────┤\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ " << mainMenuText[2] << " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "└─────┘\n";
for (int i = 0; i < blank_row; i++)
cout << "\n";
colorMainMenuCursor();
}
void moveMainMenu(int keydown)
{ // move menu's cursor
if (keydown == UpArrowKey)
{
mainMenuPosition--;
}
if (keydown == DownArrowKey)
{
mainMenuPosition++;
}
if (mainMenuPosition > 2)
mainMenuPosition = 0;
if (mainMenuPosition < 0)
mainMenuPosition = 2;
colorMainMenuCursor();
}
void moveGameMenu(int keydown)
{ // move game menu's cursor
if (keydown == UpArrowKey)
{
gameMenuPosition--;
}
if (keydown == DownArrowKey)
{
gameMenuPosition++;
}
if (gameMenuPosition > 3)
gameMenuPosition = 0;
if (gameMenuPosition < 0)
gameMenuPosition = 3;
colorGameMenuCursor();
}
void loadGame()
{
int counter = 0;
vector<string> filename;
string cmd = "";
storeSavedFilename(counter, filename);
if (counter > 0)
{
while (atoi(cmd.c_str()) < 1 || atoi(cmd.c_str()) > counter)
{
cout << "請輸入編號(輸入 0 離開) : ";
cin >> cmd;
if (cmd[0] == '0' && cmd.length() == 1)
{
return;
}
}
system("cls");
cout << "載入 \"" << filename[atoi(cmd.c_str()) - 1] << "\" 中 ...... \n";
delay(1000);
// call gaming process
Game newGame(filename[atoi(cmd.c_str()) - 1]);
ChessBoard &board = newGame.getboard();
system("cls");
cursor.x = 0;
cursor.y = 0;
printGameFormat();
colorBoard(board);
showHint(newGame.playerNow());
while (gameRunning)
{
GoToXY(4 * cursor.x + BoardInitX, 2 * cursor.y + BoardInitY);
int keydown = getKey();
Coord select;
if (keydown != 0)
{
// move the cursor by arrow key
if (keydown >= LeftArrowKey && keydown <= DownArrowKey)
moveCursor(keydown);
else if (keydown == EnterKey && !holdChess)
{
int kindCode = board.getChess(cursor).getKind();
if (kindCode != -1) // not null chess
{
if ((newGame.playerNow() == PLAYER_BLACK && kindCode >= 1 && kindCode <= 7) ||
(newGame.playerNow() == PLAYER_RED && kindCode >= 8 && kindCode <= 14))
{
select.x = cursor.x;
select.y = cursor.y;
vector<Coord> temp = newGame.promptMovement(select);
vector<Coord> temp2 = newGame.promptCapture(select);
drawPrompt(temp, temp2, select);
showHoldHint(kindCode);
holdChess = true;
}
}
}
// has chess -> move
else if (keydown == EnterKey && holdChess)
{
Chess& c = board.getChess(select);
if (select.x == cursor.x && select.y == cursor.y)
{
holdChess = false;
colorBoard(board);
showHoldHint(-1);
}
else if (board.isMovable(cursor, select, newGame.playerNow()))
{
if (board.moveChess(select, cursor, newGame.playerNow()))
{
holdChess = false;
newGame.writeHistory(board.getArea());
newGame.writeReport(select, cursor);
colorBoard(board);
newGame.switchPlayer();
showHint(newGame.playerNow());
showHoldHint(-1);
showReport(newGame.getReport());
}
}
}
else if (keydown == EscKey)
{
drawGameMenu(newGame);
}
}
}
}
else
{
cout << "無存檔!";
delay(3000);
drawMainMenu();
}
}
void drawGameMenu(Game &g)
{
int index = gameMenuPosition;
int space = 30;
int blank_row = 11;
system("CLS");
SetConsoleTextAttribute(hConsole, MainMenuLineColor);
for (int i = 0; i < blank_row; i++)
cout << "\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "┌─────┐\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ "
<< "繼續遊戲"
<< " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "├─────┤\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ "
<< "保存遊戲"
<< " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "├─────┤\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ "
<< "重新開始"
<< " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "├─────┤\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "│ "
<< "退出遊戲"
<< " │\n";
for (int i = 0; i < space; i++)
cout << " ";
cout << "└─────┘\n";
for (int i = 0; i < blank_row; i++)
cout << "\n";
colorGameMenuCursor();
gameMenuOpen = true;
while (gameMenuOpen)
{
int keydown = getKey();
if (keydown != 0)
{
if (keydown == UpArrowKey || keydown == DownArrowKey)
{
moveGameMenu(keydown);
colorGameMenuCursor();
}
else if (keydown == EnterKey)
{
gameMenuAction(g);
}
}
}
}
void gameMenuAction(Game &g)
{
gameMenuOpen = false;
// get system time
time_t now = time(0);
string fileName = to_string(now);
if (gameMenuPosition == 0) // continue
return;
else if (gameMenuPosition == 1) // save
g.saveFile(fileName + ".txt");
else if (gameMenuPosition == 2) // restart
startNewGame();
else if (gameMenuPosition == 3) // quit
{
g.saveFile(fileName + ".txt");
exit(0);
}
}
void printGameFormat()
{
ifstream in;
in.open("./Format/blank.ya");
string line;
for (int i = 0; getline(in, line); i++)
{
Board[i] = line;
cout << line << "\n";
}
}
void moveCursor(int keydown)
{
// int max_x = BoardCol * 2 + BoardInitX;
// int max_y = BoardRow * 2 + BoardInitY;
if (keydown == UpArrowKey)
{
Game::controll(0, -1);
}
else if (keydown == DownArrowKey)
{
Game::controll(0, 1);
}
else if (keydown == LeftArrowKey)
{
Game::controll(-1, 0);
}
else if (keydown == RightArrowKey)
{
Game::controll(1, 0);
}
cursor = Game::getController();
if (cursor.x < 0)
Game::controll(1, 0);
if (cursor.x >= BoardCol)
Game::controll(-1, 0);
if (cursor.y < 0)
Game::controll(0, 1);
if (cursor.y >= BoardRow)
Game::controll(0, -1);
cursor = Game::getController();
GoToXY(4 * cursor.x + BoardInitX, 2 * cursor.y + BoardInitY);
}
void showHoldHint(int kindCode) {
int x = 84;
int y = 6;
GoToXY(x, y);
SetConsoleTextAttribute(hConsole, InitColor);
if (kindCode == -1) {
cout << " ";
}
else
{
cout << "您選擇了 ";
if (kindCode >= 1 && kindCode <= 8)
{
SetConsoleTextAttribute(hConsole, BlackColor);
}
else if (kindCode >= 9 && kindCode <= 14)
{
SetConsoleTextAttribute(hConsole, RedColor);
}
for (int i = 2 * (kindCode - 1); i < 2 * kindCode; i++)
cout << chessText[i];
}
}
// main gaming process
void startNewGame()
{
GoToXY(MainMenuTextInitX, MainMenuTextInitY);
cout << "Game Start!\n";
Game newGame("./save/Initial.txt");
ChessBoard &board = newGame.getboard();
delay(1000);
system("cls");
cursor.x = 0;
cursor.y = 0;
printGameFormat();
colorBoard(board);
showHint(newGame.playerNow());
while (gameRunning)
{
GoToXY(4 * cursor.x + BoardInitX, 2 * cursor.y + BoardInitY);
int keydown = getKey();
Coord select;
if (keydown != 0)
{
// move the cursor by arrow key
if (keydown >= LeftArrowKey && keydown <= DownArrowKey)
moveCursor(keydown);
else if (keydown == EnterKey && !holdChess)
{
int kindCode = board.getChess(cursor).getKind();
if (kindCode != -1) // not null chess
{
if ((newGame.playerNow() == PLAYER_BLACK && kindCode >= 1 && kindCode <= 7) ||
(newGame.playerNow() == PLAYER_RED && kindCode >= 8 && kindCode <= 14))
{
select.x = cursor.x;
select.y = cursor.y;
vector<Coord> temp = newGame.promptMovement(select);
vector<Coord> temp2 = newGame.promptCapture(select);
drawPrompt(temp, temp2, select);
showHoldHint(kindCode);
holdChess = true;
}
}
}
// has chess -> move
else if (keydown == EnterKey && holdChess)
{
Chess &c = board.getChess(select);
if (select.x == cursor.x && select.y == cursor.y)
{
holdChess = false;
colorBoard(board);
showHoldHint(-1);
}
else if (board.isMovable(cursor, select, newGame.playerNow()))
{
if (board.moveChess(select, cursor, newGame.playerNow()))
{
holdChess = false;
newGame.writeHistory(board.getArea());
newGame.writeReport(select, cursor);
colorBoard(board);
newGame.switchPlayer();
showHint(newGame.playerNow());
showHoldHint(-1);
showReport(newGame.getReport());
showWinning(newGame.isCheckmate());
}
}
}
else if (keydown == EscKey)
{
drawGameMenu(newGame);
}
}
}
}
void mainMenuAction()
{
SetConsoleTextAttribute(hConsole, InitColor);
system("CLS");
if (mainMenuPosition == 0)
{
startNewGame();
}
else if (mainMenuPosition == 1)
{
loadGame();
}
else if (mainMenuPosition == 2)
{
exit(0);
}
drawMainMenu();
}
void testColor()
{
for (int i = 1; i <= 255; i++)
{
SetConsoleTextAttribute(hConsole, i);
cout << i << " : weqweqweqwe\n";
}
}
int main()
{
init();
initCmdWindow();
drawMainMenu();
//testColor();
while (running)
{
int keydown = getKey();
if (keydown != 0)
{
if (keydown == UpArrowKey || keydown == DownArrowKey)
{
moveMainMenu(keydown);
}
else if (keydown == EnterKey)
{
mainMenuAction();
}
}
}
return 0;
}