关闭

数字滑块益智游戏

590人阅读 评论(0) 收藏 举报

 

源码如下:

 

 

#include <math.h>
#include <gtk/gtk.h>
//#include "sliders.h"//uunubt
//////////////////////////////////////////uunubt

GtkWidget *main_window, *vbox1;
GtkWidget *table1;
GtkWidget *buttons[15];
GtkWidget *statusbar1;
GtkWidget *alignment1;
GtkWidget *image;

gint move_no;
gint vacant_pos;


GtkWidget* create_main_window (void);

void swap_buttons (int p, int q);

void on_new_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_open1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_save1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_save_as1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_quit1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_cut1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_copy1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_paste1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_delete1_activate (GtkMenuItem *menuitem, gpointer user_data);

void on_about1_activate (GtkMenuItem *menuitem, gpointer user_data);

/////////////////////////////////////////uunubt
int main (int argc, char *argv[])
{

    gtk_init (&argc, &argv);

    main_window = create_main_window ();
    gtk_widget_show_all (main_window);

    gtk_main ();
    return 0;

}

gboolean is_legal_slide (gint current_pos, gint vacant_pos)
{

    int current_x_pos, current_y_pos, vacant_x_pos, vacant_y_pos;
    int a, b;

    vacant_x_pos=vacant_pos%4 ; vacant_y_pos=(int)(vacant_pos/4);
    current_x_pos=current_pos%4 ; current_y_pos=(int)(current_pos/4);

    a=abs(vacant_x_pos - current_x_pos);
    b=abs(vacant_y_pos - current_y_pos);

    if ( ( (a==1 && b==0) || (a==0 && b==1) ) && !(a==1 && b==1) ) // a XOR b
    return TRUE;
    else
    return FALSE;

}

gboolean has_player_won (void)
{

    int x;
    for (x=0; x<15; x++)
    if (x!= (gint) gtk_object_get_data(GTK_OBJECT(buttons[x]),"current_pos"))
    return FALSE;
    return TRUE;

}

void on_button_clicked (GtkWidget *button, gpointer user_data)
{

    int x_pos, y_pos;
    int current_pos;

    current_pos = (gint) gtk_object_get_data (GTK_OBJECT(button), "current_pos");

    if ( is_legal_slide(current_pos, vacant_pos) )
    {

        x_pos = vacant_pos%4; y_pos = (int)(vacant_pos/4);
        gtk_container_remove (GTK_CONTAINER(table1), button);
        gtk_table_attach (GTK_TABLE (table1),
        button, x_pos, x_pos + 1, y_pos, y_pos + 1,
        (GtkAttachOptions) (GTK_FILL),
        (GtkAttachOptions) (0), 0, 0);
        gtk_object_set_data (GTK_OBJECT(button), "current_pos",
        (gpointer) vacant_pos);
        vacant_pos = current_pos;

        move_no++;

        if ( has_player_won() )
        {

            gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "Win!");

            gtk_widget_hide (table1);
            image = gtk_image_new ();
            gtk_image_set_from_file (GTK_IMAGE(image), "smiley.xpm");
            gtk_object_ref(GTK_OBJECT(table1));
            gtk_container_remove (GTK_CONTAINER(alignment1), table1);
            gtk_container_add (GTK_CONTAINER(alignment1), image);
            gtk_widget_show (image);

        }
        else gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, g_strdup_printf("Move no.: %i", move_no));

    }
    else gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "/aIllegal move!");

}


GtkWidget *create_main_window (void)
{

    GtkWidget *main_window, *menubar1,*menuitem1,*menuitem1_menu,*new;
    GtkWidget *separatormenuitem1,*quit1,*menuitem4;
    GtkWidget *menuitem4_menu,*about1,*toolbar1;
    GtkWidget *new_button,*toolbutton5,*toolbutton6;

    gint tmp_toolbar_icon_size, x, y, c=0;
    GtkAccelGroup *accel_group = gtk_accel_group_new ();

    vacant_pos = 15; move_no=0;

    main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (main_window), "Sliders");
    vbox1 = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (main_window), vbox1);

    menubar1 = gtk_menu_bar_new ();
    menuitem1 = gtk_menu_item_new_with_mnemonic ("_File");
    menuitem1_menu = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menuitem1_menu);
    new = gtk_image_menu_item_new_from_stock ("gtk-new", accel_group);
    separatormenuitem1 = gtk_separator_menu_item_new ();
    gtk_widget_set_sensitive (separatormenuitem1, FALSE);
    quit1 = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
    menuitem4 = gtk_menu_item_new_with_mnemonic ("_Help");
    menuitem4_menu = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menuitem4_menu);
    about1 = gtk_menu_item_new_with_mnemonic ("_About");

    gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);
    gtk_container_add (GTK_CONTAINER (menubar1), menuitem1);
    gtk_container_add (GTK_CONTAINER (menuitem1_menu), new);
    gtk_container_add (GTK_CONTAINER (menuitem1_menu), separatormenuitem1);
    gtk_container_add (GTK_CONTAINER (menuitem1_menu), quit1);
    gtk_container_add (GTK_CONTAINER (menubar1), menuitem4);
    gtk_container_add (GTK_CONTAINER (menuitem4_menu), about1);

    toolbar1 = gtk_toolbar_new ();
    gtk_box_pack_start (GTK_BOX (vbox1), toolbar1, FALSE, FALSE, 0);
    gtk_toolbar_set_style (GTK_TOOLBAR (toolbar1), GTK_TOOLBAR_BOTH);
    tmp_toolbar_icon_size = gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar1));

    new_button = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-new");
    gtk_container_add (GTK_CONTAINER (toolbar1), new_button);

    toolbutton5 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-open");
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton5);

    toolbutton6 = (GtkWidget*) gtk_tool_button_new_from_stock ("gtk-save");
    gtk_container_add (GTK_CONTAINER (toolbar1), toolbutton6);

    alignment1 = gtk_alignment_new (0.5, 0.5, 1, 1);
    gtk_box_pack_start (GTK_BOX (vbox1), alignment1, TRUE, TRUE, 0);
    gtk_alignment_set_padding (GTK_ALIGNMENT (alignment1), 20, 20, 40, 40);

    table1 = gtk_table_new (4, 4, TRUE);
    gtk_container_add (GTK_CONTAINER (alignment1), table1);

    for (x=0; x<15; x++)
    {
    gchar *temp = g_strdup_printf ("%i", x+1);
    buttons[x] = gtk_button_new_with_mnemonic (temp);
    gtk_object_set_data (GTK_OBJECT(buttons[x]), "current_pos", (gpointer) x);
    g_free (temp);
    }

    for (x=0; x<4; x++)
    for (y=0; y<4; y++)
    if (!(x==3 && y==3))
    gtk_table_attach (GTK_TABLE (table1),
    buttons[c++], y,y+1,x,x+1,
    (GtkAttachOptions) (GTK_FILL),
    (GtkAttachOptions) (0), 0, 0);

    for (x=0; x<15; x++)
    gtk_signal_connect(GTK_OBJECT(buttons[x]), "clicked", GTK_SIGNAL_FUNC(on_button_clicked), (gpointer)x);

    statusbar1 = gtk_statusbar_new ();
    gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(statusbar1), FALSE);
    gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "Welcome to Sliders");
    gtk_box_pack_start (GTK_BOX (vbox1), statusbar1, FALSE, FALSE, 0);

    g_signal_connect ((gpointer) main_window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect ((gpointer) new, "activate", G_CALLBACK (on_new_activate), NULL);
    g_signal_connect ((gpointer) new_button, "clicked", G_CALLBACK (on_new_activate), NULL);
    g_signal_connect ((gpointer) quit1, "activate", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect ((gpointer) about1, "activate", G_CALLBACK (on_about1_activate), NULL);

    gtk_window_add_accel_group (GTK_WINDOW (main_window), accel_group);

    return main_window;

}

void swap_buttons (int p, int q)
{

    gint p_pos = (gint) gtk_object_get_data (GTK_OBJECT(buttons[p]), "current_pos");
    gint q_pos = (gint) gtk_object_get_data (GTK_OBJECT(buttons[q]), "current_pos");
    int p_x_pos = p_pos%4;
    int p_y_pos = (int)(p_pos/4);
    int q_x_pos = q_pos%4;
    int q_y_pos = (int)(q_pos/4);

    gtk_object_ref (GTK_OBJECT(buttons[p]));
    gtk_object_ref (GTK_OBJECT(buttons[q]));
    gtk_container_remove (GTK_CONTAINER(table1), buttons[p]);
    gtk_container_remove (GTK_CONTAINER(table1), buttons[q]);

    gtk_table_attach (GTK_TABLE (table1),
    buttons[p], q_x_pos, q_x_pos + 1, q_y_pos, q_y_pos + 1,
    (GtkAttachOptions) (GTK_FILL),
    (GtkAttachOptions) (0), 0, 0);
    gtk_table_attach (GTK_TABLE (table1),
    buttons[q], p_x_pos, p_x_pos + 1, p_y_pos, p_y_pos + 1,
    (GtkAttachOptions) (GTK_FILL),
    (GtkAttachOptions) (0), 0, 0);

    gtk_object_set_data (GTK_OBJECT(buttons[p]), "current_pos",
    (gpointer) q_pos);
    gtk_object_set_data (GTK_OBJECT(buttons[q]), "current_pos",
    (gpointer) p_pos);

}

void on_new_activate (GtkMenuItem *menuitem, gpointer user_data)
{

    int rand, x;

    gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "Welcome to sliders");
    move_no=0;

    if (GTK_IS_WIDGET(image) && GTK_IS_WIDGET(image->parent))
    {
    gtk_object_ref (GTK_OBJECT(image));
    gtk_container_remove (GTK_CONTAINER(alignment1), image);
    gtk_container_add (GTK_CONTAINER(alignment1), table1);
    gtk_widget_show (table1);
    }

    for (x=0; x<=14; x++)
    {
    rand = abs(g_rand_int ( g_rand_new () )) % 15;
    if (x!=rand)
    swap_buttons (x, rand);
    }

}


void on_about1_activate (GtkMenuItem *menuitem, gpointer user_data)
{

    GtkWidget *dialog;

    dialog = gtk_message_dialog_new_with_markup (GTK_WINDOW(main_window),
    GTK_DIALOG_MODAL,
    GTK_MESSAGE_INFO,
    GTK_BUTTONS_OK,
    "<b>Sliders</b>/n/nExample program by:/nIshan Chattopadhyaya and Muthiah Annamalai/nfor/n<i>A Journey through GTK+</i>");

    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);

}

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:302615次
    • 积分:3885
    • 等级:
    • 排名:第8116名
    • 原创:42篇
    • 转载:389篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论