📄 game.c
字号:
/*Game TicTacToeGTK ver 2.5 Copyright (C) 2006 Obada Denis (obadadenis@gmail.com)Project home page : http://tictactoegtk.sourceforge.netThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA*/#include <gtk/gtk.h>#include <stdlib.h>#include <stdio.h>#include <time.h>#include <string.h>//Header files#include "cpu.h"#include "draw.h"#include "login_window.h"#include "menu.h"#include "build.h" //Build counter #define debug 1 //Debug Switch : 0 - Debug messages OFF; 1 - Debug messages ON#define release_version "TicTacToeGTK 2.5"#define window_title release_version//End Messages#define cpu_win "CPU win !"#define no_win "No winners !"#define end_title "The End"//Login Dialog#define login_title "Login"#define name_msg "Enter your name : "#define choice_msg "Choice char : "#define error_name_msg "Please enter your name !"//Confirm Exit Dialog#define exit_title "Confirm exit "#define exit_label "Please confirm exit "//Confirm NEW Game dialog#define new_game_title "New Game confirm"#define new_game_str "Confirm new game"//About dialog#define about_title "About"#define about_str "<span size=\"x-large\">"release_version"</span>\n<tt>Build : "build"\nCopyright (C) 2006 Obada Denis\nTicTacToeGTK is free software :\nsee LICENSE file for license information</tt>\nProject home page : <b>http://tictactoegtk.sourceforge.net</b>"GtkWidget *window;//Main windowGtkWidget *ebox;//Read events form userGtkWidget *darea;//Drawing areaGtkWidget *status_bar;GtkStatusbar *statusbar;int stbar_id;GdkGC *Xcolor,*Ocolor;//Colors for X and OGdkColor color;//Temp colorGtkWidget *menu_bar; //Menu barGtkWidget *vbox;//Main window Box : store menu and eboxgint scaleX=40;//X scalegint scaleO=40;//O scalegint scaleGrid=100;//table scalegchar grid[3][3];//Main table : ' ' - Free Zone; 'X' - Zone used by X; 'O'- Zone used by Ogchar usrchar='X',cpuchar='O';//Players Chars (default values)//Functions prototypesint checkwin(void);gint delete_event(GtkWidget *,GdkEvent *,gpointer);int confirm_exit_dialog(void);void init_game(void);#include "draw.c" //Draw objects #include "cpu.c" //CPU move #include "menu.c" //Menu file#include "login_window.c" //Login Windowint confirm_exit_dialog(void){//Confirm exit dialogGtkWidget *dialog;GtkWidget *label;gint result;//Confirm Exit from GAME#if debugg_printf("Show confirmation dialog !\n");#endifdialog = gtk_dialog_new_with_buttons (exit_title,GTK_WINDOW(window),GTK_DIALOG_MODAL,GTK_STOCK_YES,GTK_RESPONSE_ACCEPT,GTK_STOCK_NO,GTK_RESPONSE_REJECT,NULL);gtk_window_set_policy(GTK_WINDOW(dialog),FALSE,FALSE,TRUE);//Add text in dialoglabel=gtk_label_new(exit_label);gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),label);//Show dialolggtk_widget_show_all (dialog);if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_ACCEPT) result=1; else result=0;gtk_widget_destroy(dialog);return result;}gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data){//Confirm Exit from GAME#if debugg_printf("Delete event !\n");#endifif (confirm_exit_dialog()) return FALSE; else return TRUE;}void destroy(GtkWidget *widget,gpointer data)//Stop program{#if debugg_printf("Exit\n");#endifgtk_main_quit();}gboolean redraw(GtkWidget *widget,GdkEventExpose *event,gpointer data)//Redraw objects{drawGrid();return TRUE;}gboolean user_action (GtkWidget *event_box,GdkEventButton *event,gpointer data)//User Action { gint i,j,x; GtkWidget *dialog,*label; //Calc coords i=(gint)(event->x)/scaleGrid; j=(gint)(event->y)/scaleGrid; #if debug g_printf("Click on : %d %d\n",i,j); #endif if (grid[i][j]==' ')//Check if cell is free { grid[i][j]=usrchar;//Marc cell as used drawGrid();//Show on modifications x=checkwin();//Check if win #if debug g_printf("Check win 1, check value : %d\n",x); #endif if (x!=0)//If win show dialog { dialog = gtk_dialog_new_with_buttons (end_title,GTK_WINDOW(window),GTK_DIALOG_MODAL,GTK_STOCK_OK,GTK_RESPONSE_NONE,NULL); gtk_window_set_policy(GTK_WINDOW(dialog),FALSE,FALSE,TRUE); gtk_widget_set_size_request(dialog,120,100); //Set a mesage if (x==1) label=gtk_label_new (cpu_win); if (x==2) label=gtk_label_new (usr_win); if (x==3) label=gtk_label_new (no_win); //Connect signals g_signal_connect(dialog,"response", G_CALLBACK (destroy),NULL); //Add text in dialog gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),label); //Show dialog gtk_widget_show_all (dialog); } if (x==0) //If CPU can move cpumove();//Move :) drawGrid();//Show modifications if(x==0) { x=checkwin();//Check if CPU win #if debug g_printf("Check win 2, check value : %d\n",x); #endif if (x!=0) { dialog = gtk_dialog_new_with_buttons (end_title,GTK_WINDOW(window),GTK_DIALOG_MODAL,GTK_STOCK_OK,GTK_RESPONSE_NONE,NULL); gtk_window_set_policy(GTK_WINDOW(dialog),FALSE,FALSE,TRUE); gtk_widget_set_size_request(dialog,120,100); if (x==1) label = gtk_label_new (cpu_win); if (x==2) label = gtk_label_new (usr_win); if (x==3) label = gtk_label_new (no_win); g_signal_connect(dialog,"response", G_CALLBACK (destroy),NULL); gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),label); gtk_widget_show_all (dialog); } } } return TRUE; }int checkwin(void){/*Check game status0 - Game continue1 - CPU Win2 - User Win3 - No win */gint i,j,n;gchar s1[4],cpustr[4],usrstr[4];cpustr[0]=cpuchar;cpustr[1]=cpuchar;cpustr[2]=cpuchar;cpustr[3]='\0';usrstr[0]=usrchar;usrstr[1]=usrchar;usrstr[2]=usrchar;usrstr[3]='\0';//Check linesfor(i=0;i<3;i++){s1[0]='\0';for(j=0;j<3;j++) s1[j]=grid[i][j];s1[3]='\0';if (strcmp(s1,cpustr)==0) return 1;if (strcmp(s1,usrstr)==0) return 2;}//Check columnsfor(i=0;i<3;i++){s1[0]='\0';for(j=0;j<3;j++) s1[j]=grid[j][i];s1[3]='\0';if (strcmp(s1,cpustr)==0) return 1;if (strcmp(s1,usrstr)==0) return 2;}//Verificam Check diagonalss1[0]='\0';for(i=0;i<3;i++) s1[i]=grid[i][i];s1[3]='\0';if (strcmp(s1,cpustr)==0) return 1;if (strcmp(s1,usrstr)==0) return 2;s1[0]='\0';for(i=0;i<3;i++) s1[i]=grid[i][2-i];s1[3]='\0';if (strcmp(s1,cpustr)==0) return 1;if (strcmp(s1,usrstr)==0) return 2;//Check if exist Free celsn=0;for (i=0;i<3;i++)for (j=0;j<3;j++) if (grid[i][j]!=' ') n++;#if debugprintf("Total fill cell : %d\n",n);#endifif (n==9) return 3;return 0;}void init_game(void){int i,j;//Initializing . . .for(i=0;i<3;i++)for(j=0;j<3;j++)grid[i][j]=' ';//Init random generatorsrand(time(NULL));}void initcolors(void){//Init Colors //Gen colors for X and O//Color for XXcolor=gdk_gc_new(darea->window);color.red=65535;//Red colorcolor.green=0;color.blue=0;gdk_gc_set_rgb_fg_color (Xcolor, &color);//Color for OOcolor=gdk_gc_new(darea->window);color.red=0;color.green=0;color.blue=65535;//Color bluegdk_gc_set_rgb_fg_color (Ocolor, &color);}//***Main***main(int argc,char *argv[]){g_printf("***\n\n");g_printf("%s Build ",release_version);g_printf(build);g_printf(" Copyright (C) 2006 Obada Denis (obadadenis@gmail.com)\nProject home page : http://tictactoegtk.sourceforge.net\n");g_printf("TicTacToeGTK is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by \nthe Free Software Foundation; either version 2 of the License, or \n (at your option) any later version.\n\nTicTacToeGTK is distributed in the hope that it will be useful, \nbut WITHOUT ANY WARRANTY; without even the implied warranty of \nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License \nalong with Nautilus; if not, write to the Free Software Foundation, Inc., \n51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA");g_printf("\n\n***\n\n");//Debug message#if debugg_printf(window_title);g_printf("\nDebug version\n");#endif//GTK initgtk_init(&argc,&argv);//Init Gameinit_game();window=gtk_window_new(GTK_WINDOW_TOPLEVEL);//Create Main Windowgtk_widget_set_size_request(window,305,370);//Set size of Main Windowgtk_window_set_title(GTK_WINDOW(window),window_title);//Set WIndow titlegtk_window_set_policy(GTK_WINDOW(window),FALSE,FALSE,TRUE);//Set Window Policy//Config destroy signalg_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy),NULL);//Create Event boxebox=gtk_event_box_new();darea=gtk_drawing_area_new();//Config Signalsg_signal_connect (G_OBJECT (darea), "expose_event",G_CALLBACK (redraw), NULL);//Redraw signalg_signal_connect (G_OBJECT (ebox),"button_press_event",G_CALLBACK (user_action),darea);//Signal form user//Status Barstatus_bar=gtk_statusbar_new();gtk_widget_set_size_request(status_bar,300,20);stbar_id=gtk_statusbar_get_context_id(GTK_STATUSBAR(status_bar),"Info");//Menumenu_bar=get_menubar_menu(window);//VBoxvbox=gtk_vbox_new(FALSE,10);//packing widgets//Adding Menugtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 0);//Adding DAREAgtk_container_add(GTK_CONTAINER(ebox),darea);gtk_container_add(GTK_CONTAINER(vbox),ebox);gtk_box_pack_start(GTK_BOX(vbox),status_bar,FALSE,FALSE,0);//Adding VBox on main windowgtk_container_add(GTK_CONTAINER(window),vbox);//Show widget on screengtk_widget_show_all(window);gtk_statusbar_push(GTK_STATUSBAR(status_bar),stbar_id,release_version);//Show login screen :)login();//Init Colorsinitcolors();gtk_main();return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -