-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttonActions.h
319 lines (295 loc) · 9.82 KB
/
buttonActions.h
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
GtkWidget * fillTable();
GtkWidget * MakeUI(GtkWidget*);
static void Restart (GtkWidget *widget, gpointer data) {
//gtk_window_close(window);
MakeUI(window);
g_print ("restart\n");
}
static void forfeitTurn (GtkWidget *widget, gpointer data) {
g_print ("player %d skipped their turn", turn);
if (turn == PLAYER1){
turn = PLAYER2;
gtk_label_set_text(Turn, " Player 2 Turn");
}
else {
turn = PLAYER1;
gtk_label_set_text(Turn, " Player 1 Turn");
}
g_print ("it is now player %d's turn.\n", turn);
}
static void forfeitGame (GtkWidget *widget, gpointer data) {
switch (turn){
//add get image to each case to check if peice has already been played on tile
case PLAYER1:
gtk_label_set_text(Turn, "Player 1 has forfeited the game, Player 2 Wins");
break;
case PLAYER2:
gtk_label_set_text(Turn, "Player 2 has forfeited the game, Player 2 Wins");
break;
}
gtk_window_close(window);
}
static void enter_callback( GtkWidget *widget,
GtkWidget *entry )
{
player_1_Name = gtk_entry_get_text(GTK_ENTRY (entry));
}
static void enter2_callback( GtkWidget *widget,
GtkWidget *entry )
{
player_2_Name = gtk_entry_get_text (GTK_ENTRY (entry));
}
/* Our callback.
* The data passed to this function is printed to stdout */
static void callback( GtkWidget *widget,
gpointer data )
{
GtkWidget *image;
Button b;
char * rc = (char *) data;
int r = *rc - '0';
int c = *(rc+1) - '0';
b.pair.row = r;
b.pair.col = c;
switch (turn){
//add get image to each case to check if peice has already been played on tile
case PLAYER1:
gtk_button_set_label (widget , NULL);
image = gtk_image_new_from_file("bc.png");
b.color = BLACK;
gtk_button_set_image (widget, image );
gtk_widget_set_sensitive(widget, FALSE);
b.current_status = DISABLED;
gtk_label_set_text(Turn, " Player 2's Turn");
turn = PLAYER2;
p1_Score++;
char buffer[20];
sprintf(buffer, "%d", p1_Score);
gtk_label_set_text(Player_1_Score, buffer);
break;
case PLAYER2:
image = gtk_image_new_from_file("wc.png");
b.color = WHITE;
gtk_button_set_image (widget, image );
gtk_widget_set_sensitive(widget, FALSE);
b.current_status = DISABLED;
turn = PLAYER1;
gtk_label_set_text(Turn, " Player 1's Turn");
p2_Score++;
char buffer2[20];
sprintf(buffer2, "%d", p2_Score);
gtk_label_set_text(Player_2_Score, buffer2);
break;
}
g_print ("Hello again - button%d-%d was pressed\n", r,c);
board[board_count] = b;
board_count += 1;
}
//function that, given a pair and a color, determines all valid neighbors of that color
static Pair * findNeighbors(Color color, Pair start, int * count)
{
Pair neighbors[8] = {{99,99},{99,99},{99,99},{99,99},{99,99},{99,99},{99,99},{99,99}};
int r = start.row;
int c = start.col;
int co = 0;
for (int i = 0; i < board_count+1; i++)
{
if ((board[i].pair.row != r || board[i].pair.col != c) && board[i].color == color)
{
if ((board[i].pair.row == r || board[i].pair.row == r-1 || board[i].pair.row == r+1) && (board[i].pair.col == c || board[i].pair.col == c-1 || board[i].pair.col == c+1))
{
g_print("BOARD: %d,%d\n", board[i].pair.row, board[i].pair.col);
neighbors[co].row = board[i].pair.row;
neighbors[co].col = board[i].pair.col;
co++;
}
}
}
g_print("%d\n", co);
*count = co;
g_print("%d,%d\n", neighbors[0].row, neighbors[0].col);
Pair * neighborss = neighbors;
g_print("%d,%d\n", neighborss->row, neighborss->col);
return neighborss;
}
/*function called to determine whether an area is closed by pieces, and which pieces create the enclosure.*/
static int closedArea(Color color, PairList * pairs, int count, Pair last, Pair first)
{
/* Pair start = pairs->data;
Pair next;
g_print("START BEFORE: %d,%d\n", start.row, start.col);
PairList *nextList;
pairs->next = nextList;
int co;
Pair * neighbors = findNeighbors(color, start, &co);
g_print("1st neighbor: %d,%d\n", neighbors->row, neighbors->col);
g_print("last: %d,%d\n", last.row, last.col);
if (co == 0 || (co == 1 && neighbors->row == last.row && neighbors->col == last.col))
{
return 0;
}
g_print("LASTTTTT %d,%d\n", last.row, last.col);
if (start.row == 0)
{
next.row = -1;
next.col = start.col;
}else if (start.col == 0)
{
next.row = start.row;
next.col = -1;
}else if (start.row == 8)
{
next.row = 9;
next.col = start.col;
}else if (start.col == 8)
{
next.row = start.row;
next.col = 9;
}else if (start.row == -1)
{
for (int i = 0; neighbors+i != NULL; i++)
{
nextList->data = *(neighbors+i);
if ((neighbors+i)->row != last.row || (neighbors+i)->col != last.col || last.row == 99)
{
if ((neighbors+i)->row == pairs->data.row && (neighbors+i)->col == pairs->data.col)
{
return 1;
}else
{
if (closedArea(color, ((*pairs).next), count+1, start))
return 1;
}
}
}
if (start.col == 9)
{
next.row = start.row+1;
next.col = start.col;
}else
{
next.row = start.row;
next.col = start.col+1;
}
}else if (start.col == -1)
{
for (int i = 0; neighbors+i != NULL; i++)
{
nextList->data = *(neighbors+i);
if ((neighbors+i)->row != last.row || (neighbors+i)->col != last.col || last.row == 99)
{
if ((neighbors+i)->row == pairs->data.row && (neighbors+i)->col == pairs->data.col)
{
return 1;
}else
{
if (closedArea(color, ((*pairs).next), count+1, start))
return 1;
}
}
}
next.row = start.row+1;
next.col = start.col;
}else if (start.row == 9)
{
for (int i = 0; neighbors+i != NULL; i++)
{
nextList->data = *(neighbors+i);
if ((neighbors+i)->row != last.row || (neighbors+i)->col != last.col || last.row == 99)
{
if ((neighbors+i)->row == pairs->data.row && (neighbors+i)->col == pairs->data.col)
{
return 1;
}else
{
if (closedArea(color, ((*pairs).next), count+1, start))
return 1;
}
}
}
next.row = start.row;
next.col = start.col-1;
}else if (start.col == 9)
{
for (int i = 0; neighbors+i != NULL; i++)
{
nextList->data = *(neighbors+i);
if ((neighbors+i)->row != last.row || (neighbors+i)->col != last.col || last.row == 99)
{
if ((neighbors+i)->row == pairs->data.row && (neighbors+i)->col == pairs->data.col)
{
return 1;
}else
{
if (closedArea(color, ((*pairs).next), count+1, start))
return 1;
}
}
}
if (start.row == 9)
{
next.row = start.row;
next.col = start.col-1;
}else
{
next.row = start.row-1;
next.col = start.col;
}
}
for (int i = 0; (neighbors+i) != NULL || (neighbors+i < 50); i++)
{
if ((neighbors+i)->row != last.row || (neighbors+i)->col != last.col || last.row == 99)
{
g_print("last %d,%d\n", last.row, last.col);
g_print("first %d,%d\n", first.row, first.col);
if (((neighbors+i)->row == first.row && (neighbors+i)->col == first.col) && (first.row != last.row || first.col != last.col))
{
g_print("h?\n");
return 1;
}else
{
g_print("here?\n");
g_print("%d,%d\n", (neighbors+i)->row, (neighbors+i)->col);
nextList->data = *(neighbors+i);
if (closedArea(color, nextList, count+1, start, first))
return 1;
g_print("oh shit");
}
}
}
nextList->data = next;
if (closedArea(color, nextList, count+1, start, first))
*/
return 0;
}
/*callback that checks for scoring after a button is pressed.*/
static void update(GtkWidget *widget, gpointer data)
{
char * rc = (char *) data;
int r = *rc - '0';
int c = *(rc+1) - '0';
Button current;
for(int i; i < board_count; i++)
{
if (board[i].pair.row == r && board[i].pair.col == c)
{
current = board[i];
}
}
PairList pairs;
pairs.data = current.pair;
Pair last = {99,99};
if (closedArea(current.color, &pairs, 0, last, current.pair))
{
//PUT SCORE FUNCTION ONE HERE
g_print("Closed Area Found!\n");
}
}
/* This callback quits the program */
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_main_quit ();
return FALSE;
}