OpenCores
URL https://opencores.org/ocsvn/connect-6/connect-6/trunk

Subversion Repositories connect-6

[/] [connect-6/] [trunk/] [CONNECTK/] [connectk-2.0/] [src/] [dialogs.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 sumanta.ch
/*
2
 
3
connectk -- a program to play the connect-k family of games
4
Copyright (C) 2007 Michael Levin
5
 
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
 
20
*/
21
 
22
#include "config.h"
23
#include <gtk/gtk.h>
24
#include "shared.h"
25
#include "connectk.h"
26
 
27
/*
28
 *      New game dialog
29
 */
30
 
31
static GtkWidget *window = NULL, *new_game_dialog, *ngd_board_size,
32
                 *ngd_connect_k, *ngd_place_p, *ngd_start_q, *ngd_combo;
33
 
34
/* Clear the board and history for a new game */
35
void new_game(unsigned int size)
36
{
37
        tree_view_clear(1);
38
        clear_history(0);
39
        set_board_size(size);
40
        board = NULL;
41
        go_to_move(0);
42
        board->moves_left = start_q;
43
        board->turn = PIECE_BLACK;
44
        draw_board();
45
        stop_ai();
46
        setup_move();
47
}
48
 
49
// Close the new game dialog
50
static void new_game_dialog_cancel(GtkButton *button, gpointer user_data)
51
{
52
        gtk_widget_hide(new_game_dialog);
53
}
54
 
55
// Accept the new game dialog
56
static void new_game_dialog_ok(GtkButton *button, gpointer user_data)
57
{
58
        new_game_dialog_cancel(button, user_data);
59
        connect_k = gtk_spin_button_get_value_as_int(
60
                                                GTK_SPIN_BUTTON(ngd_connect_k));
61
        place_p = gtk_spin_button_get_value_as_int(
62
                                                  GTK_SPIN_BUTTON(ngd_place_p));
63
        start_q = gtk_spin_button_get_value_as_int(
64
                                                  GTK_SPIN_BUTTON(ngd_start_q));
65
        new_game(gtk_spin_button_get_value_as_int(
66
                                              GTK_SPIN_BUTTON(ngd_board_size)));
67
}
68
 
69
// Create a spin button and label
70
static GtkWidget *labeled_spin_new(const gchar *text, double spin_from,
71
                                   double spin_to, double spin_set,
72
                                   GtkWidget **spin, GCallback func)
73
{
74
        GtkWidget *hbox, *label;
75
 
76
        hbox = gtk_hbox_new(FALSE, 4);
77
        label = gtk_label_new(text);
78
        gtk_misc_set_alignment(GTK_MISC(label), 1., 0.5);
79
        *spin = gtk_spin_button_new_with_range(spin_from, spin_to, 1.);
80
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(*spin), spin_set);
81
        if (func)
82
                g_signal_connect(*spin, "value-changed", G_CALLBACK(func),
83
                                 NULL);
84
        gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
85
        gtk_box_pack_start(GTK_BOX(hbox), *spin, FALSE, FALSE, 0);
86
        return hbox;
87
}
88
 
89
static void new_game_set_custom(void)
90
/* Called when the user changes some parameter */
91
{
92
        gtk_combo_box_set_active(GTK_COMBO_BOX(ngd_combo), 3);
93
}
94
 
95
static void new_game_set_combo(GtkComboBox *combo)
96
/* Called when the combo box value changes */
97
{
98
        int new_size = 19, new_k = 6, new_p = 2, new_q = 1;
99
 
100
        switch(gtk_combo_box_get_active(combo)) {
101
        case 0:
102
                new_size = 3;
103
                new_k = 3;
104
                new_p = 1;
105
                break;
106
        case 1:
107
                new_k = 5;
108
                new_p = 1;
109
        case 2:
110
                break;
111
        default:
112
                return;
113
        }
114
 
115
        /* Keep the values from resetting the combo box to custom */
116
        g_signal_handlers_block_by_func(G_OBJECT(ngd_board_size),
117
                                        new_game_set_custom, NULL);
118
        g_signal_handlers_block_by_func(G_OBJECT(ngd_place_p),
119
                                        new_game_set_custom, NULL);
120
        g_signal_handlers_block_by_func(G_OBJECT(ngd_start_q),
121
                                        new_game_set_custom, NULL);
122
        g_signal_handlers_block_by_func(G_OBJECT(ngd_connect_k),
123
                                        new_game_set_custom, NULL);
124
 
125
        /* Set the preset values */
126
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ngd_board_size), new_size);
127
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ngd_place_p), new_p);
128
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ngd_start_q), new_q);
129
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(ngd_connect_k), new_k);
130
 
131
        /* Re-enable the signal handlers */
132
        g_signal_handlers_unblock_by_func(G_OBJECT(ngd_board_size),
133
                                          new_game_set_custom, NULL);
134
        g_signal_handlers_unblock_by_func(G_OBJECT(ngd_place_p),
135
                                          new_game_set_custom, NULL);
136
        g_signal_handlers_unblock_by_func(G_OBJECT(ngd_start_q),
137
                                          new_game_set_custom, NULL);
138
        g_signal_handlers_unblock_by_func(G_OBJECT(ngd_connect_k),
139
                                          new_game_set_custom, NULL);
140
}
141
 
142
// Create new game dialog
143
void new_game_dialog_init(void)
144
{
145
        GtkWidget *vbox, *hbox, *w;
146
 
147
        vbox = gtk_vbox_new(FALSE, 4);
148
 
149
        /* Stock games */
150
        ngd_combo = gtk_combo_box_new_text();
151
        gtk_combo_box_append_text(GTK_COMBO_BOX(ngd_combo), "Tic Tac Toe");
152
        gtk_combo_box_append_text(GTK_COMBO_BOX(ngd_combo), "Go Moku");
153
        gtk_combo_box_append_text(GTK_COMBO_BOX(ngd_combo), "Connect 6");
154
        gtk_combo_box_append_text(GTK_COMBO_BOX(ngd_combo), "Custom");
155
        gtk_combo_box_set_active(GTK_COMBO_BOX(ngd_combo), 2);
156
        gtk_box_pack_start(GTK_BOX(vbox), ngd_combo, FALSE, FALSE, 0);
157
        g_signal_connect(ngd_combo, "changed", G_CALLBACK(new_game_set_combo),
158
                         NULL);
159
 
160
        // Parameters
161
        hbox = labeled_spin_new("Board size:", 2., MAX_BOARD_SIZE, 19.,
162
                                &ngd_board_size, new_game_set_custom);
163
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
164
        hbox = labeled_spin_new("Connect to win:", 2., MAX_CONNECT_K, 6.,
165
                                &ngd_connect_k, new_game_set_custom);
166
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
167
        hbox = labeled_spin_new("Moves per turn:", 1., MAX_PLACE_P, 2.,
168
                                &ngd_place_p, new_game_set_custom);
169
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
170
        hbox = labeled_spin_new("Starting moves:", 1., MAX_START_Q, 1.,
171
                                &ngd_start_q, new_game_set_custom);
172
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
173
 
174
        /* Separator */
175
        w = gtk_vbox_new(FALSE, 0);
176
        gtk_box_pack_start(GTK_BOX(vbox), w, FALSE, FALSE, 4);
177
 
178
        /* Cancel/Ok buttons */
179
        hbox = gtk_hbox_new(TRUE, 4);
180
        w = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
181
        g_signal_connect(G_OBJECT(w), "clicked",
182
                         G_CALLBACK(new_game_dialog_cancel), NULL);
183
        gtk_box_pack_start(GTK_BOX(hbox), w, TRUE, TRUE, 0);
184
        w = gtk_button_new_from_stock(GTK_STOCK_OK);
185
        g_signal_connect(G_OBJECT(w), "clicked",
186
                         G_CALLBACK(new_game_dialog_ok), NULL);
187
        gtk_box_pack_end(GTK_BOX(hbox), w, TRUE, TRUE, 0);
188
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
189
 
190
        // Create dialog window
191
        new_game_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
192
        g_signal_connect(G_OBJECT(new_game_dialog), "delete-event",
193
                         G_CALLBACK(gtk_widget_hide_on_delete), NULL);
194
        gtk_window_set_title(GTK_WINDOW(new_game_dialog), "New game...");
195
        gtk_window_set_transient_for(GTK_WINDOW(new_game_dialog),
196
                                     GTK_WINDOW(window));
197
        gtk_window_set_modal(GTK_WINDOW(new_game_dialog), TRUE);
198
        gtk_window_set_resizable(GTK_WINDOW(new_game_dialog), FALSE);
199
        gtk_container_add(GTK_CONTAINER(new_game_dialog), vbox);
200
        gtk_container_border_width(GTK_CONTAINER(new_game_dialog), 12);
201
}
202
 
203
// Game -> New activated
204
void open_new_game_dialog(GtkMenuItem *item, gpointer user_data)
205
{
206
        gtk_widget_show_all(new_game_dialog);
207
}
208
 
209
/*
210
 *      Open/save as dialogs
211
 */
212
 
213
/* Game -> Open activated */
214
void open_file_dialog(GtkMenuItem *item, gpointer user_data)
215
{
216
        GtkWidget *dialog;
217
 
218
        halt_ai_thread();
219
        dialog = gtk_file_chooser_dialog_new("Open File",
220
                                             GTK_WINDOW(window),
221
                                             GTK_FILE_CHOOSER_ACTION_OPEN,
222
                                             GTK_STOCK_CANCEL,
223
                                             GTK_RESPONSE_CANCEL,
224
                                             GTK_STOCK_OPEN,
225
                                             GTK_RESPONSE_ACCEPT,
226
                                             NULL);
227
        if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
228
            char *filename;
229
 
230
            filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
231
            load_moves_list(filename);
232
            g_free(filename);
233
        }
234
        gtk_widget_destroy(dialog);
235
        start_ai_thread();
236
}
237
 
238
/* Game -> Save as activated */
239
void save_file_dialog(GtkMenuItem *item, gpointer user_data)
240
{
241
        GtkWidget *dialog;
242
 
243
        halt_ai_thread();
244
        dialog = gtk_file_chooser_dialog_new("Save File",
245
                                             GTK_WINDOW(window),
246
                                             GTK_FILE_CHOOSER_ACTION_SAVE,
247
                                             GTK_STOCK_CANCEL,
248
                                             GTK_RESPONSE_CANCEL,
249
                                             GTK_STOCK_SAVE,
250
                                             GTK_RESPONSE_ACCEPT,
251
                                             NULL);
252
        gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog),
253
                                                       TRUE);
254
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
255
            char *filename;
256
 
257
            filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
258
            save_moves_list(filename);
259
            g_free(filename);
260
        }
261
        gtk_widget_destroy(dialog);
262
        start_ai_thread();
263
}
264
 
265
/*
266
 *      Tournament dialog
267
 */
268
 
269
static GtkWidget *tourney_dialog = NULL, *tourney_games, *tourney_save,
270
                 *tourney_prefix, *tourney_view, *tourney_exec, *tourney_halt;
271
static GtkListStore *tourney_store;
272
static const char *tourney_prefix_str = NULL;
273
static int tourney_total, tourney_wins[PIECES];
274
int tournament = 0;
275
 
276
static void tourney_dialog_close(GtkWidget *button)
277
{
278
        gtk_widget_hide(tourney_dialog);
279
}
280
 
281
static void tourney_dialog_stop(GtkWidget *button)
282
{
283
        int total = tourney_wins[PIECE_BLACK] + tourney_wins[PIECE_WHITE] +
284
                    tourney_wins[PIECE_NONE];
285
 
286
        gtk_widget_hide(tourney_halt);
287
        gtk_widget_show(tourney_exec);
288
        tournament = 0;
289
        if (tourney_wins[PIECE_BLACK] > tourney_wins[PIECE_WHITE])
290
                window_status(va("Black won tournament (%d%% of games won)",
291
                                 tourney_wins[PIECE_BLACK] * 100 / total));
292
        else if (tourney_wins[PIECE_WHITE] > tourney_wins[PIECE_BLACK])
293
                window_status(va("White won tournament (%d%% of games won)",
294
                                 tourney_wins[PIECE_WHITE] * 100 / total));
295
        else
296
                window_status("Tournament was a draw");
297
        draw_board();
298
}
299
 
300
static void tourney_dialog_execute(GtkWidget *button)
301
{
302
        gtk_list_store_clear(tourney_store);
303
        gtk_widget_hide(tourney_exec);
304
        gtk_widget_show(tourney_halt);
305
        tourney_total = tournament =
306
               gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(tourney_games));
307
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(tourney_save)))
308
                tourney_prefix_str =
309
                                  gtk_entry_get_text(GTK_ENTRY(tourney_prefix));
310
        else
311
                tourney_prefix_str = NULL;
312
        new_game(board_size);
313
        tourney_wins[PIECE_NONE] = 0;
314
        tourney_wins[PIECE_BLACK] = 0;
315
        tourney_wins[PIECE_WHITE] = 0;
316
        window_status("Running tournament...");
317
}
318
 
319
void tourney_result(PIECE win, int moves)
320
{
321
        GtkTreeIter iter;
322
        GtkTreePath *path;
323
        int game = tourney_total - tournament + 1;
324
        char *filename;
325
 
326
        tourney_wins[win]++;
327
 
328
        /* Add game to list */
329
        gtk_list_store_append(tourney_store, &iter);
330
        gtk_list_store_set(tourney_store, &iter, 0, piece_to_string(win),
331
                           1, moves, 2, game, -1);
332
 
333
        /* Scroll games list down */
334
        path = gtk_tree_model_get_path(GTK_TREE_MODEL(tourney_store), &iter);
335
        gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(tourney_view), path,
336
                                     NULL, FALSE, 0., 0.);
337
        gtk_tree_path_free(path);
338
 
339
        /* Save the tournament game */
340
        if (tourney_prefix_str) {
341
                filename = g_strdup(va("%s%d", tourney_prefix_str, game));
342
                save_moves_list(filename);
343
                g_free(filename);
344
        }
345
 
346
        if (--tournament > 0) {
347
                new_game(board_size);
348
                return;
349
        }
350
        tourney_dialog_stop(NULL);
351
}
352
 
353
void tourney_dialog_init(void)
354
{
355
        GtkWidget *vbox, *hbox, *w, *scrolled;
356
        GtkTreeViewColumn *column;
357
 
358
        if (tourney_dialog)
359
                return;
360
 
361
        vbox = gtk_vbox_new(FALSE, 4);
362
 
363
        /* Games spin button */
364
        hbox = gtk_hbox_new(FALSE, 0);
365
        w = gtk_label_new("Number of games:");
366
        gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 0);
367
        tourney_games = gtk_spin_button_new_with_range(1., 1000., 1.);
368
        gtk_box_pack_start(GTK_BOX(hbox), tourney_games, FALSE, FALSE, 0);
369
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
370
 
371
        /* Games list */
372
        tourney_store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT,
373
                                           G_TYPE_INT);
374
        tourney_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(
375
                                                                tourney_store));
376
        gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tourney_view), TRUE);
377
        column = gtk_tree_view_column_new_with_attributes("Game",
378
                                                   gtk_cell_renderer_text_new(),
379
                                                          "text", 2, NULL);
380
        gtk_tree_view_append_column(GTK_TREE_VIEW(tourney_view), column);
381
        column = gtk_tree_view_column_new_with_attributes("Moves",
382
                                                   gtk_cell_renderer_text_new(),
383
                                                          "text", 1, NULL);
384
        gtk_tree_view_append_column(GTK_TREE_VIEW(tourney_view), column);
385
        column = gtk_tree_view_column_new_with_attributes("Winner",
386
                                                   gtk_cell_renderer_text_new(),
387
                                                          "text", 0, NULL);
388
        gtk_tree_view_append_column(GTK_TREE_VIEW(tourney_view), column);
389
 
390
        /* Scrolled container for tree view */
391
        scrolled = gtk_scrolled_window_new(NULL, NULL);
392
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
393
                                       GTK_POLICY_NEVER,
394
                                       GTK_POLICY_AUTOMATIC);
395
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled),
396
                                            GTK_SHADOW_IN);
397
        gtk_container_add(GTK_CONTAINER(scrolled), tourney_view);
398
        gtk_box_pack_start(GTK_BOX(vbox), scrolled, TRUE, TRUE, 0);
399
 
400
        /* Save games checkbox */
401
        hbox = gtk_hbox_new(FALSE, 0);
402
        tourney_save = gtk_check_button_new_with_label("Save games with "
403
                                                       "prefix:");
404
        gtk_box_pack_start(GTK_BOX(hbox), tourney_save, FALSE, FALSE, 0);
405
        tourney_prefix = gtk_entry_new();
406
        gtk_box_pack_start(GTK_BOX(hbox), tourney_prefix, TRUE, TRUE, 0);
407
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
408
 
409
        /* Execute button */
410
        hbox = gtk_hbox_new(TRUE, 0);
411
        tourney_exec = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
412
        g_signal_connect(G_OBJECT(tourney_exec), "clicked",
413
                         G_CALLBACK(tourney_dialog_execute), NULL);
414
        gtk_widget_set_no_show_all(tourney_exec, TRUE);
415
        gtk_widget_show(tourney_exec);
416
        gtk_box_pack_start(GTK_BOX(hbox), tourney_exec, TRUE, TRUE, 0);
417
 
418
        /* Halt button */
419
        tourney_halt = gtk_button_new_from_stock(GTK_STOCK_STOP);
420
        g_signal_connect(G_OBJECT(tourney_halt), "clicked",
421
                         G_CALLBACK(tourney_dialog_stop), NULL);
422
        gtk_widget_set_no_show_all(tourney_halt, TRUE);
423
        gtk_widget_hide(tourney_halt);
424
        gtk_box_pack_start(GTK_BOX(hbox), tourney_halt, TRUE, TRUE, 0);
425
 
426
        /* Close button */
427
        w = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
428
        g_signal_connect(G_OBJECT(w), "clicked",
429
                         G_CALLBACK(tourney_dialog_close), NULL);
430
        gtk_box_pack_start(GTK_BOX(hbox), w, TRUE, TRUE, 0);
431
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
432
 
433
        /* Create dialog window */
434
        tourney_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
435
        g_signal_connect(G_OBJECT(tourney_dialog), "delete-event",
436
                         G_CALLBACK(gtk_widget_hide_on_delete), NULL);
437
        gtk_window_set_title(GTK_WINDOW(tourney_dialog), "Tournament");
438
        gtk_window_set_transient_for(GTK_WINDOW(tourney_dialog),
439
                                     GTK_WINDOW(window));
440
        gtk_window_set_modal(GTK_WINDOW(tourney_dialog), TRUE);
441
        gtk_container_add(GTK_CONTAINER(tourney_dialog), vbox);
442
        gtk_container_border_width(GTK_CONTAINER(tourney_dialog), 12);
443
        gtk_window_resize(GTK_WINDOW(tourney_dialog), 256, 384);
444
}
445
 
446
void open_tourney_dialog(GtkWidget *menuitem)
447
{
448
        if (!players[PIECE_BLACK].ai || !players[PIECE_WHITE].ai) {
449
                GtkWidget * dialog;
450
 
451
                dialog = gtk_message_dialog_new(GTK_WINDOW(window),
452
                                                GTK_DIALOG_DESTROY_WITH_PARENT,
453
                                                GTK_MESSAGE_ERROR,
454
                                                GTK_BUTTONS_OK,
455
                                                "Cannot run a tournament with "
456
                                                "human players.");
457
                gtk_dialog_run(GTK_DIALOG(dialog));
458
                gtk_widget_destroy(dialog);
459
                return;
460
        }
461
        gtk_widget_show_all(tourney_dialog);
462
}
463
 
464
/*
465
 *      Player dialog
466
 */
467
 
468
PlayerDialog player_dialog[PIECES];
469
 
470
static void player_dialog_ok(GtkWidget *button, PlayerDialog *pd)
471
{
472
        int i, index;
473
 
474
        gtk_widget_hide(pd->window);
475
        index = gtk_combo_box_get_active(GTK_COMBO_BOX(pd->combo));
476
 
477
        /* Search options */
478
        pd->player->depth =
479
                   gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(pd->depth));
480
        pd->player->branch =
481
                  gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(pd->branch));
482
        pd->player->cache =
483
                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pd->cache));
484
        pd->player->tss =
485
                       gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pd->tss));
486
        for (i = 0; i < SEARCHES; i++)
487
                if (gtk_toggle_button_get_active(
488
                                            GTK_TOGGLE_BUTTON(pd->search[i]))) {
489
                        pd->player->search = i;
490
                        break;
491
                }
492
 
493
        /* Marker player */
494
        if (pd->player == players) {
495
                AIMoves *moves;
496
 
497
                if (index == PLAYER_HUMAN) {
498
                        draw_marks(NULL, FALSE);
499
                        return;
500
                }
501
                pd->player->ai = index;
502
                stop_ai();
503
                moves = run_ai(players);
504
                if (opt_print_u)
505
                        g_debug("AI %s utility %d (0x%x)", ai(index)->s_desc,
506
                                moves->utility, moves->utility);
507
                setup_move();
508
                draw_marks(moves, TRUE);
509
                return;
510
        }
511
 
512
        /* White or black player changed */
513
        if (pd->player->ai != index) {
514
                pd->player->ai = index;
515
                stop_ai();
516
                setup_move();
517
        }
518
}
519
 
520
void player_dialog_init(PlayerDialog *pd, PIECE player)
521
{
522
        GtkWidget *vbox, *hbox, *fbox, *w;
523
        GSList *group = NULL;
524
        int i, i_max;
525
        const char *label;
526
 
527
        pd->player = players + player;
528
        vbox = gtk_vbox_new(FALSE, 4);
529
 
530
        /* Controller combo box */
531
        hbox = gtk_hbox_new(FALSE, 0);
532
        w = gtk_label_new("Controller:");
533
        gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 8);
534
        w = gtk_combo_box_new_text();
535
        i_max = number_of_ais();
536
        for (i = 0; i < i_max; i++) {
537
                AI *a = ai(i);
538
 
539
                label = "Human";
540
                if (a->func)
541
                        label = player_to_string(i);
542
                gtk_combo_box_append_text(GTK_COMBO_BOX(w), label);
543
        }
544
        gtk_combo_box_set_active(GTK_COMBO_BOX(w), 0);
545
        pd->combo = w;
546
        gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 0);
547
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
548
 
549
        /* Search box frame */
550
        w = gtk_frame_new("Search");
551
        gtk_box_pack_start(GTK_BOX(vbox), w, FALSE, FALSE, 4);
552
        fbox = gtk_vbox_new(FALSE, 4);
553
        gtk_container_add(GTK_CONTAINER(w), fbox);
554
        gtk_container_border_width(GTK_CONTAINER(fbox), 8);
555
 
556
        /* Search radio buttons */
557
        for (i = 0; i < SEARCHES; i++) {
558
                w = gtk_radio_button_new_with_label(group, search_to_string(i));
559
                group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(w));
560
                gtk_box_pack_start(GTK_BOX(fbox), w, FALSE, FALSE, 0);
561
                pd->search[i] = w;
562
        }
563
 
564
        /* Cache toggle button */
565
        pd->cache = gtk_check_button_new_with_label("Use search cache");
566
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pd->cache), TRUE);
567
        gtk_box_pack_start(GTK_BOX(fbox), pd->cache, FALSE, FALSE, 0);
568
 
569
        /* Threat-space search toggle button */
570
        pd->tss = gtk_check_button_new_with_label("Threat-space search");
571
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pd->tss), FALSE);
572
        /*gtk_box_pack_start(GTK_BOX(fbox), pd->tss, FALSE, FALSE, 0);*/
573
 
574
        /* Search depth spin button */
575
        w = labeled_spin_new("Depth in moves:", 1., MAX_DEPTH, 1., &pd->depth,
576
                             NULL);
577
        gtk_box_pack_start(GTK_BOX(fbox), w, FALSE, FALSE, 0);
578
 
579
        /* Minimum branching factor spin button */
580
        w = labeled_spin_new("Minimum branches:", 0., MAX_BRANCH, 2.,
581
                             &pd->branch, NULL);
582
        gtk_box_pack_start(GTK_BOX(fbox), w, FALSE, FALSE, 0);
583
 
584
        /* Ok button */
585
        hbox = gtk_hbox_new(TRUE, 0);
586
        w = gtk_hbox_new(FALSE, 0);
587
        gtk_box_pack_start(GTK_BOX(hbox), w, FALSE, FALSE, 0);
588
        w = gtk_button_new_from_stock(GTK_STOCK_OK);
589
        g_signal_connect(G_OBJECT(w), "clicked",
590
                         G_CALLBACK(player_dialog_ok), pd);
591
        gtk_box_pack_start(GTK_BOX(hbox), w, TRUE, TRUE, 0);
592
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
593
 
594
        /* Create dialog window */
595
        pd->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
596
        g_signal_connect(G_OBJECT(pd->window), "delete-event",
597
                         G_CALLBACK(gtk_widget_hide_on_delete), NULL);
598
        label = "Mark Player";
599
        if (player)
600
                label = va("%s Player", piece_to_string(player));
601
        gtk_window_set_title(GTK_WINDOW(pd->window), label);
602
        gtk_window_set_transient_for(GTK_WINDOW(pd->window),
603
                                     GTK_WINDOW(window));
604
        gtk_window_set_modal(GTK_WINDOW(pd->window), TRUE);
605
        gtk_window_set_resizable(GTK_WINDOW(pd->window), FALSE);
606
        gtk_container_add(GTK_CONTAINER(pd->window), vbox);
607
        gtk_container_border_width(GTK_CONTAINER(pd->window), 12);
608
}
609
 
610
void open_player_dialog(GtkWidget *menuitem, PlayerDialog *pd)
611
{
612
        gtk_widget_show_all(pd->window);
613
}
614
 
615
/*
616
 *      Screenshot
617
 */
618
 
619
#ifndef NO_CAIRO_SVG
620
void screenshot_dialog(void)
621
/* Game -> Screenshot activated */
622
{
623
        GtkWidget *dialog;
624
 
625
        halt_ai_thread();
626
        dialog = gtk_file_chooser_dialog_new("Save Screenshot",
627
                                             GTK_WINDOW(window),
628
                                             GTK_FILE_CHOOSER_ACTION_SAVE,
629
                                             GTK_STOCK_CANCEL,
630
                                             GTK_RESPONSE_CANCEL,
631
                                             GTK_STOCK_SAVE,
632
                                             GTK_RESPONSE_ACCEPT,
633
                                             NULL);
634
        gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog),
635
                                                       TRUE);
636
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
637
                char *filename;
638
 
639
                filename =
640
                        gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
641
                draw_screenshot(filename);
642
                g_free(filename);
643
        }
644
        gtk_widget_destroy(dialog);
645
        start_ai_thread();
646
}
647
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.