forked from gaogaotiantian/fir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfir.c
226 lines (204 loc) · 7.12 KB
/
gfir.c
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
#include <gtk/gtk.h>
#include <assert.h>
#include <math.h>
#include "game.h"
#define BoardSize 19
#define LineWidth 1
#define PieceRadius 10
#define PieceGap 4
#define PieceSize (PieceRadius*2+LineWidth)
#define CanvasWidth ((PieceSize+PieceGap) * BoardSize + PieceGap)
int test = 0;
//enum NodeType {Black, White, Empty};
Game game;
typedef enum NodeType NodeType;
struct PieceInfo {
NodeType type;
int x;
int y;
};
typedef struct PieceInfo PieceInfo;
static cairo_surface_t *surface = NULL;
void InitializeGame();
static void DrawPiece(int, int, NodeType);
static void init_cb(GtkWidget *widget,
gpointer data)
{
InitializeGame();
if (game.blackAI.id != 10) {
Point p;
p = game.blackAI.func(game.board, Black);
game.Move(p);
DrawPiece(p.x, p.y, Black);
}
GtkWidget* draw_area = (GtkWidget*) data;
gtk_widget_queue_draw_area(draw_area, 0, 0, CanvasWidth, CanvasWidth);
}
static void init_rs_cb(GtkWidget *widget,
gpointer data)
{
InitializeGame();
int x = 0;
int y = 0;
NodeType t;
for (int i = 0; i <= 2; ++i) {
t = (i == 1) ? Black : White;
x = (BoardSize - 9) / 2 + rand() % 9;
y = (BoardSize - 9) / 2 + rand() % 9;
if (game.board[x][y] == Empty) {
game.board[x][y] = t;
DrawPiece(x, y, t);
}
}
if (game.blackAI.id != 10) {
Point p;
p = game.blackAI.func(game.board, Black);
game.Move(p);
DrawPiece(p.x, p.y, Black);
}
GtkWidget* draw_area = (GtkWidget*) data;
gtk_widget_queue_draw_area(draw_area, 0, 0, CanvasWidth, CanvasWidth);
}
static void DrawBoard()
{
cairo_t *cr;
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0.84, 0.69, 0.57);
cairo_paint(cr);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, LineWidth);
for (int i = 0; i < BoardSize; ++i) {
cairo_move_to(cr, PieceGap + PieceRadius + i * (PieceSize + PieceGap), PieceGap + PieceRadius);
cairo_line_to(cr, PieceGap + PieceRadius + i * (PieceSize + PieceGap), CanvasWidth - PieceGap - PieceRadius);
cairo_move_to(cr, PieceGap + PieceRadius, PieceGap + PieceRadius + i * (PieceSize + PieceGap));
cairo_line_to(cr, CanvasWidth - PieceGap - PieceRadius, PieceGap + PieceRadius + i * (PieceSize + PieceGap));
}
cairo_stroke(cr);
cairo_destroy(cr);
}
static gboolean configure_event_cb (GtkWidget *widget,
GdkEventConfigure *event,
gpointer data)
{
if (surface)
cairo_surface_destroy(surface);
surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget),
CAIRO_CONTENT_COLOR,
CanvasWidth,
CanvasWidth);
DrawBoard();
gtk_widget_queue_draw_area(widget, 0, 0, CanvasWidth, CanvasWidth);
return TRUE;
}
static void draw_cb(GtkWidget *widget,
cairo_t *cr,
gpointer data)
{
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
}
static inline int GetRowColPixel(int x) {
return PieceGap + PieceRadius + x * (PieceSize + PieceGap);
}
static inline int GetRowColFromPixel(int x) {
return (int)round((double)(x - PieceGap - PieceRadius) / (PieceSize + PieceGap));
}
static void DrawPiece(int x, int y, NodeType t)
{
cairo_t *cr;
//cairo_set_source_surface(cr, surface, 0, 0);
cr = cairo_create(surface);
if (t == Black)
cairo_set_source_rgb(cr, 0, 0, 0);
else if (t == White)
cairo_set_source_rgb(cr, 1, 1, 1);
else
assert(0);
cairo_arc(cr, GetRowColPixel(x), GetRowColPixel(y), PieceRadius, 0, 2*G_PI);
cairo_fill(cr);
cairo_stroke(cr);
cairo_destroy(cr);
}
static void PutPiece(GtkWidget *widget,
GdkEventMotion *event,
gpointer data)
{
if (game.winner == Empty && game.isBlackPlaying == (game.blackAI.id == 10)) {
Point p(GetRowColFromPixel(event->x), GetRowColFromPixel(event->y));
if (game.Move(p) == true) {
DrawPiece(GetRowColFromPixel(event->x), GetRowColFromPixel(event->y),
game.isBlackPlaying ? White : Black);
gtk_widget_queue_draw_area(widget, 0, 0, CanvasWidth, CanvasWidth);
if (game.CheckVictory() != Empty) {
return;
} else {
if (game.isBlackPlaying) {
p = game.blackAI.func(game.board, Black);
game.Move(p);
DrawPiece(p.x, p.y, Black);
} else {
p = game.whiteAI.func(game.board, White);
game.Move(p);
DrawPiece(p.x, p.y, White);
}
if (game.CheckVictory() != Empty)
return;
}
}
}
}
void InitializeGame()
{
game.Initialize();
srand(time(NULL));
game.playerIDList.push_back(2);
game.SetGamerAI(2, 10);
DrawBoard();
}
static void activate(GtkApplication* app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *button_rs;
GtkWidget *button_box;
GtkWidget *draw_area;
GtkWidget *fixed_container;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Five Son Chess");
gtk_window_set_default_size(GTK_WINDOW(window), 900, 900);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
fixed_container = gtk_fixed_new();
// Draw Area
draw_area = gtk_drawing_area_new();
gtk_widget_set_size_request(draw_area, CanvasWidth, CanvasWidth);
g_signal_connect(draw_area, "draw", G_CALLBACK(draw_cb), NULL);
g_signal_connect(draw_area, "configure-event", G_CALLBACK(configure_event_cb), NULL);
gtk_widget_add_events(draw_area, GDK_BUTTON_PRESS_MASK);
g_signal_connect(draw_area, "button_press_event", G_CALLBACK(PutPiece), draw_area);
gtk_fixed_put(GTK_FIXED(fixed_container), draw_area, 0, 0);
// Buttons
button_box = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
gtk_fixed_put(GTK_FIXED(fixed_container), button_box, CanvasWidth, 0);
button = gtk_button_new_with_label("从头再来");
g_signal_connect(button, "clicked", G_CALLBACK(init_cb), draw_area);
button_rs = gtk_button_new_with_label("随机开局");
g_signal_connect(button_rs, "clicked", G_CALLBACK(init_rs_cb), draw_area);
gtk_container_add(GTK_CONTAINER(button_box), button);
gtk_container_add(GTK_CONTAINER(button_box), button_rs);
gtk_container_add(GTK_CONTAINER(window), fixed_container);
gtk_widget_show_all(window);
// Game starts here!
InitializeGame();
}
int main (int argc, char**argv)
{
gtk_init(&argc, &argv);
GtkApplication *app;
int status;
app = gtk_application_new("gfir", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}