Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
zmx0142857
GitHub Repository: zmx0142857/mini-games
Path: blob/master/c/go/main.c
364 views
1
#include "main.h"
2
3
char board[WIDTH][HEIGHT];
4
enum Color color = BLACK;
5
enum Rule rule = RULE_GO;
6
bool is_ai = false;
7
8
void print_board()
9
{
10
int lineno = HEIGHT;
11
printf("%2d┌", lineno--);
12
for (int j = 2; j < WIDTH; ++j)
13
printf("─┬");
14
puts("─┐");
15
for (int i = 2; i < HEIGHT; ++i) {
16
printf("%2d├", lineno--);
17
for (int j = 2; j < WIDTH; ++j)
18
printf("─┼");
19
puts("─┤");
20
}
21
printf("%2d└", lineno--);
22
for (int j = 2; j < WIDTH; ++j)
23
printf("─┴");
24
puts("─┘");
25
printf(" ");
26
for (int j = 0; j < WIDTH; ++j)
27
printf("%2c", j + 'A');
28
putchar('\n');
29
30
int star[] = {3, 9, 15};
31
int x, y;
32
for (int i = 0; i < 3; ++i) {
33
y = star[i];
34
for (int j = 0; j < 3; ++j) {
35
x = star[j];
36
mvprint(BOARD_Y, BOARD_X, STAR);
37
}
38
}
39
}
40
41
void set_board(int x, int y, int color)
42
{
43
board[x][y] = color;
44
if (color == BLANK) {
45
cursor_goto(BOARD_Y, BOARD_X);
46
if (x == 0) {
47
if (y == 0)
48
printf("└");
49
else if (y == HEIGHT-1)
50
printf("┌");
51
else
52
printf("├");
53
} else if (x == WIDTH-1) {
54
if (y == 0)
55
printf("┘");
56
else if (y == HEIGHT-1)
57
printf("┐");
58
else
59
printf("┤");
60
} else {
61
if (y == 0)
62
printf("┴");
63
else if (y == HEIGHT-1)
64
printf("┬");
65
else if (x % 6 == 3 && y % 6 == 3)
66
printf(STAR);
67
else
68
printf("┼");
69
}
70
} else {
71
mvprint(BOARD_Y, BOARD_X,
72
color == BLACK ? SYM_CIRCLE_BLACK : SYM_CIRCLE_WHITE);
73
}
74
}
75
76
enum Color flip(enum Color c)
77
{
78
return c == BLACK ? WHITE : BLACK;
79
}
80
81
bool bounded(int x, int y)
82
{
83
return x >= 0 && y >= 0 && x < WIDTH && y < HEIGHT;
84
}
85
86
// 试图在 (x,y) 落子
87
// 若游戏结束, 返回 true
88
bool move(int x, int y)
89
{
90
if (!bounded(x, y)) {
91
MSG("超出棋盘范围");
92
return false;
93
}
94
if (board[x][y]) {
95
MSG("此处已有落子");
96
return false;
97
}
98
if (rule == RULE_GO) {
99
return rule_go(x, y);
100
} else if (rule == RULE_GOMOKU) {
101
return rule_gomoku(x, y);
102
}
103
}
104
105
void play()
106
{
107
memset(board, BLANK, sizeof(board));
108
print_board();
109
MSG("%s: %s",
110
(rule == RULE_GO ? "围棋" : "五子棋"),
111
"方向键或 WASD 移动, 空格落子"
112
);
113
int x = WIDTH / 2;
114
int y = HEIGHT / 2;
115
cursor_goto(BOARD_Y, BOARD_X);
116
117
int key = getch();
118
while (key != -1) {
119
if (key >= 'A' && key <= 'Z') key += 32;
120
key = arrow_key(key);
121
switch (key) {
122
case 'w': if (bounded(x, y+1)) ++y; break;
123
case 's': if (bounded(x, y-1)) --y; break;
124
case 'a': if (bounded(x-1, y)) --x; break;
125
case 'd': if (bounded(x+1, y)) ++x; break;
126
case ' ': if (move(x, y)) return; break;
127
}
128
// MSG("%c%d", x+'A', y+1);
129
cursor_goto(BOARD_Y, BOARD_X);
130
key = getch();
131
}
132
}
133
134
int main(int argc, char **argv)
135
{
136
if (argc >= 2) {
137
// 启用五子棋规则
138
if (strcmp(argv[1], "-5") == 0) {
139
rule = RULE_GOMOKU;
140
}
141
}
142
if (argc >= 3) {
143
// 启用 AI
144
if (strcmp(argv[2], "-ai") == 0) {
145
is_ai = true;
146
}
147
148
}
149
150
srand(time_ms(NULL));
151
152
screen_size(HEIGHT + 3, WIDTH*2 + 10);
153
bool is_game_over = false;
154
while (!is_game_over) {
155
play();
156
is_game_over = true;
157
}
158
cursor_goto(MSG_LINE+1, 0);
159
return 0;
160
}
161
162