数字滑块益智游戏

转载 2011年01月13日 14:28:00

 

源码如下:

 

 

#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);

}

相关文章推荐

滑块拼图游戏(PuzzleX)

  • 2014-09-07 22:39
  • 1.55MB
  • 下载

HDU 6048 Puzzle(滑块游戏)

Puzzle Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total...

顺丰科技2017笔试 小C负责设计一种新的益智数字游戏

小C负责设计一种新的益智游戏。游戏分A、B两方,规则如下: 1.A方在一个纸片上写一个不超过6位的数值N,同时给出一个目标数M; 2.B方对写有数值N的纸片进行分割,分割成的每个纸片上有一个数,所...

猜数字益智小游戏

  • 2013-07-11 21:34
  • 120KB
  • 下载

Cocos2d-x-Lua 开发简单的小游戏(记数字踩白块)

Cocos2d-x-Lua 开发简单的小游戏(记数字踩白块) 本篇博客来给大家介绍如何使用Lua这门语言来开发一个简单的小游戏—记数字踩白块。 游戏的流程是这样的:在界面上生成5个数1~5字并显示在随...

益智游戏——猜数字 Ver1.2

  • 2007-02-12 13:27
  • 2.96MB
  • 下载

Cocos2d-x-Lua 开发简单的小游戏(记数字踩白块)

Cocos2d-x-Lua 开发简单的小游戏(记数字踩白块) 本篇博客来给大家介绍如何使用Lua这门语言来开发一个简单的小游戏—记数字踩白块。 游戏的流程是这样的:在界面上生成5个数1~5...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)