-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
149 lines (134 loc) · 3.52 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
#include "game.h"
#include "common.h"
Game::Game() : auto_play(true) {
p_board = new Board;
p_draw = new Draw;
}
Game::~Game() {
delete p_board;
delete p_draw;
}
Board *Game::gb() { return p_board; }
Draw *Game::gd() { return p_draw; }
char Game::main_menu() {
clear();
mvprintw(LINES / 2 - 8, COLS / 2 - 20, "SOFIA WELCOMES YOU.");
mvprintw(LINES / 2 - 6, COLS / 2 - 20, "[1] PLAYING WITH HUMAN");
mvprintw(LINES / 2 - 4, COLS / 2 - 20, "[2] PLAYING WITH SOFIAI");
mvprintw(LINES / 2 - 2, COLS / 2 - 20, "[3] PLAYING WITH MARIAI");
mvprintw(LINES / 2 + 0, COLS / 2 - 20, "[4] WATCHING A GAME BETWEEN AIs");
mvprintw(LINES / 2 + 2, COLS / 2 - 20, "[q] EXIT");
mvprintw(LINES / 2 + 4, COLS / 2 - 20, "PRESS THE KEY TO CONTINUE");
char ch = getch();
return ch;
}
void Game::select_menu(char ch) {
// playing with human
if (ch == '1')
gb()->set_player(HUMAN, HUMAN);
// playing with sofia
if (ch == '2') {
while (1) {
move(LINES / 2 + 6, COLS / 2 - 20);
printw("DO YOU ALLOW SOFIAI TO PLAY FIRST? [y/n]: ");
ch = getch();
if (ch == 'y' || ch == 'Y') {
gb()->set_player(SOFIAI, HUMAN);
return;
} else if (ch == 'n' || ch == 'N') {
gb()->set_player(HUMAN, SOFIAI);
return;
}
}
}
// playing with maria
if (ch == '3') {
while (1) {
move(LINES / 2 + 6, COLS / 2 - 20);
printw("DO YOU ALLOW MARIAI TO PLAY FIRST? [y/n]: ");
ch = getch();
if (ch == 'y' || ch == 'Y') {
gb()->set_player(MARIAI, HUMAN);
return;
} else if (ch == 'n' || ch == 'N') {
gb()->set_player(HUMAN, MARIAI);
return;
}
}
}
// AIs playing with each other
if (ch == '4') {
while (1) {
move(LINES / 2 + 6, COLS / 2 - 20);
printw("DO YOU LET AIs PAUSE BETWEEN EACH THEIR MOVE? [y/n]: ");
gb()->set_player(SOFIAI, MARIAI);
ch = getch();
if (ch == 'y' || ch == 'Y') {
auto_play = false;
return;
} else if (ch == 'n' || ch == 'N') {
auto_play = true;
return;
}
}
}
}
void Game::play_game() {
bool quit = false;
int code = 0;
Player player;
Coords coords;
Sofiai sofia(gb());
Mariai maria(gb(), gd());
gd()->init_screen_win(gb());
while (!quit) {
coords = make_pair(-1, -1);
if (gb()->whose_turn() == BLACK) {
player = gb()->get_player(BLACK);
} else {
player = gb()->get_player(WHITE);
}
// get next move: who plays
if (player == HUMAN) {
coords = gd()->get_move();
if (coords.first == -1)
return;
}
if (player == SOFIAI) {
coords = sofia.next_move();
if (!auto_play)
gd()->pause_auto_play();
}
if (player == MARIAI) {
coords = maria.next_move();
if (!auto_play)
gd()->pause_auto_play();
}
// when missed next move
if (coords.first == -1) {
gd()->wipe_out_msg();
gd()->dump_msg(
"Fatal error occurred. next move not found. Press q to exit");
gd()->wait_for_q();
return;
}
code = gb()->make_move(coords.first, coords.second);
Board b = *gb();
#if PRINT_CANDY
b.toggle_turn();
VecCoords candy = maria.gen_candy(b);
for (auto q : candy) {
int i, j;
tie(i, j) = q;
b.set_stone(i, j, CANDY);
}
b.toggle_turn();
#endif
gd()->update_screen(code, coords.second, 2 * coords.first, &b);
if (!code) {
quit = gb()->check_quit(coords.first, coords.second);
gb()->toggle_turn();
}
}
gd()->dump_who_won(gb());
}