Digital slider puzzle game

Source: Internet
Author: User

 

The source code is as follows:

 

 

# Include <math. h>
# Include <GTK/GTK. h>
// # Include "scyclers. 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,
(Gtkattacexceptions) (gtk_fill ),
(Gtkattacexceptions) (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), "scyclers ");
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,
(Gtkattacexceptions) (gtk_fill ),
(Gtkattacexceptions) (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 scyclers ");
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,
(Gtkattacexceptions) (gtk_fill ),
(Gtkattacexceptions) (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,
(Gtkattacexceptions) (gtk_fill ),
(Gtkattacexceptions) (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 scyclers ");
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> scyclers </B>/n/nexample program: /Nishan chattopadhyaya and Muthiah Annamalai/Nfor/n <I> A journey through GTK + </I> ");

Gtk_dialog_run (gtk_dialog (DIALOG ));
Gtk_widget_destroy (DIALOG );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.