示例程序代码Tictactoe@GTK+ 2.0 中文教程连载

示例程序代码

下面是前面手册中使用的一些示例程序代码。

Tictactoe

tictactoe.h

/* GTK - GIMP工具包

* 版权 (C) 1995-1997 Peter Mattis, Spencer Kimball 和 Josh MacDonald 所有

*

* 本程序是自由软件。你可以在自由软件基金发布的 GNU GPL 的条款下重新分发

* 或修改它。GPL 可以使用版本 2 或(由你选择)任何随后的版本。

*

* 本程序分发的目的是它可能对其他人有用,但不提供任何的担保,包括隐含的

* 和适合特定用途的保证。请查阅GNU通用公共许可证获得详细的信息。

*

* 你应该已经随该软件一起收到一份GNU通用公共许可。如果还没有,请写信给

* Free Software Foundation, Inc., 59 Temple Place - Suite 330,

* Boston, MA 02111-1307, USA.

*/



#ifndef __TICTACTOE_H__

#define __TICTACTOE_H__





#include <gdk/gdk.h>

#include <gtk/gtkvbox.h>





#ifdef __cplusplus

extern "C" {

#endif /* __cplusplus */



#define TICTACTOE(obj) GTK_CHECK_CAST (obj, tictactoe_get_type (), Tictactoe)

#define TICTACTOE_CLASS(klass) GTK_CHECK_CLASS_CAST (klass, tictactoe_get_type (), TictactoeClass)

#define IS_TICTACTOE(obj) GTK_CHECK_TYPE (obj, tictactoe_get_type ())





typedef struct _Tictactoe Tictactoe;

typedef struct _TictactoeClass TictactoeClass;



struct _Tictactoe

{

GtkVBox vbox;



GtkWidget *buttons[3][3];

};



struct _TictactoeClass

{

GtkVBoxClass parent_class;



void (* tictactoe) (Tictactoe *ttt);

};



GtkType tictactoe_get_type (void);

GtkWidget* tictactoe_new (void);

void tictactoe_clear (Tictactoe *ttt);



#ifdef __cplusplus

}

#endif /* __cplusplus */



#endif /* __TICTACTOE_H__ */

tictactoe.c

/* GTK - GIMP工具包

* 版权 (C) 1995-1997 Peter Mattis, Spencer Kimball 和 Josh MacDonald 所有

*

* 本程序是自由软件。你可以在自由软件基金发布的 GNU GPL 的条款下重新分发

* 或修改它。GPL 可以使用版本 2 或(由你选择)任何随后的版本。

*

* 本程序分发的目的是它可能对其他人有用,但不提供任何的担保,包括隐含的

* 和适合特定用途的保证。请查阅GNU通用公共许可证获得详细的信息。

*

* 你应该已经随该软件一起收到一份GNU通用公共许可。如果还没有,请写信给

* Free Software Foundation, Inc., 59 Temple Place - Suite 330,

* Boston, MA 02111-1307, USA.

*/



#include "gtk/gtksignal.h"

#include "gtk/gtktable.h"

#include "gtk/gtktogglebutton.h"

#include "tictactoe.h"



enum {

TICTACTOE_SIGNAL,

LAST_SIGNAL

};



static void tictactoe_class_init (TictactoeClass *klass);

static void tictactoe_init (Tictactoe *ttt);

static void tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt);



static gint tictactoe_signals[LAST_SIGNAL] = { 0 };



GType

tictactoe_get_type ()

{

static GType ttt_type = 0;



if (!ttt_type)

{

static const GTypeInfo ttt_info =

{

sizeof (TictactoeClass),

NULL,

NULL,

(GClassInitFunc) tictactoe_class_init,

NULL,

NULL,

sizeof (Tictactoe),

0,

(GInstanceInitFunc) tictactoe_init,

};



ttt_type = g_type_register_static (GTK_TYPE_VBOX, "Tictactoe", &ttt_info, 0);

}



return ttt_type;

}



static void

tictactoe_class_init (TictactoeClass *class)

{

GtkObjectClass *object_class;



object_class = (GtkObjectClass*) class;



tictactoe_signals[TICTACTOE_SIGNAL] = g_signal_new ("tictactoe",

G_TYPE_FROM_CLASS (object_class),

G_SIGNAL_RUN_FIRST,

0,

NULL,

NULL,

g_cclosure_marshal_VOID__VOID,

G_TYPE_NONE, 0, NULL);





class->tictactoe = NULL;

}



static void

tictactoe_init (Tictactoe *ttt)

{

GtkWidget *table;

gint i,j;



table = gtk_table_new (3, 3, TRUE);

gtk_container_add (GTK_CONTAINER (ttt), table);

gtk_widget_show (table);



for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

{

ttt->buttons[i][j] = gtk_toggle_button_new ();

gtk_table_attach_defaults (GTK_TABLE (table), ttt->buttons[i][j],

i, i+1, j, j+1);

g_signal_connect (G_OBJECT (ttt->buttons[i][j]), "toggled",

G_CALLBACK (tictactoe_toggle), ttt);

gtk_widget_set_size_request (ttt->buttons[i][j], 20, 20);

gtk_widget_show (ttt->buttons[i][j]);

}

}



GtkWidget*

tictactoe_new ()

{

return GTK_WIDGET (g_object_new (tictactoe_get_type (), NULL));

}



void

tictactoe_clear (Tictactoe *ttt)

{

int i,j;



for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

{

g_signal_handlers_block_by_func (G_OBJECT (ttt->buttons[i][j]),

NULL, ttt);

gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),

FALSE);

g_signal_handlers_unblock_by_func (G_OBJECT (ttt->buttons[i][j]),

NULL, ttt);

}

}



static void

tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)

{

int i,k;



static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },

{ 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },

{ 0, 1, 2 }, { 0, 1, 2 } };

static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },

{ 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },

{ 0, 1, 2 }, { 2, 1, 0 } };



int success, found;



for (k = 0; k < 8; k++)

{

success = TRUE;

found = FALSE;



for (i = 0; i < 3; i++)

{

success = success &&

GTK_TOGGLE_BUTTON (ttt->buttons[rwins[k][i]][cwins[k][i]])->active;

found = found ||

ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;

}



if (success && found)

{

g_signal_emit (G_OBJECT (ttt),

tictactoe_signals[TICTACTOE_SIGNAL], 0);

break;

}

}

}

ttt_test.c

#include <stdlib.h>

#include <gtk/gtk.h>

#include "tictactoe.h"



void win( GtkWidget *widget,

gpointer data )

{

g_print ("Yay!/n");

tictactoe_clear (TICTACTOE (widget));

}



int main( int argc,

char *argv[] )

{

GtkWidget *window;

GtkWidget *ttt;



gtk_init (&argc, &argv);



window = gtk_window_new (GTK_WINDOW_TOPLEVEL);



gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");



g_signal_connect (G_OBJECT (window), "destroy",

G_CALLBACK (exit), NULL);



gtk_container_set_border_width (GTK_CONTAINER (window), 10);



ttt = tictactoe_new ();



gtk_container_add (GTK_CONTAINER (window), ttt);

gtk_widget_show (ttt);



g_signal_connect (G_OBJECT (ttt), "tictactoe",

G_CALLBACK (win), NULL);



gtk_widget_show (window);



gtk_main ();



return 0;

}


<<< Previous Home Next >>>
GDK 事件类型 GtkDial
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你修改TicTacToe程序并补充判断游戏结束的代码。在TicTacToe游戏中,判断游戏结束的条件通常有以下几种: 1. 棋盘填满,平局结束 2. 一方获胜,游戏结束 3. 玩家选择退出游戏 下面是修改后的TicTacToe程序,包含了判断游戏结束的代码: ```python import random # 初始化棋盘 board = [' '] * 10 # 随机决定玩家先手或电脑先手 def choose_first(): if random.randint(0, 1) == 0: return 'computer' else: return 'player' # 打印棋盘 def display_board(board): print(' | |') print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) print(' | |') print('-----------') print(' | |') print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) print(' | |') print('-----------') print(' | |') print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) print(' | |') # 玩家选择棋子位置 def player_choice(board): position = 0 while position not in range(1, 10) or not space_check(board, position): try: position = int(input('请选择你要下棋的位置(1-9): ')) except ValueError: print('输入错误,请重新选择。') return position # 判断棋盘是否还有空位 def full_board_check(board): return ' ' not in board[1:] # 判断棋子位置是否可用 def space_check(board, position): return board[position] == ' ' # 在棋盘上放置棋子 def place_marker(board, marker, position): board[position] = marker # 判断是否有一方获胜 def win_check(board, mark): return ((board[7] == mark and board[8] == mark and board[9] == mark) or (board[4] == mark and board[5] == mark and board[6] == mark) or (board[1] == mark and board[2] == mark and board[3] == mark) or (board[7] == mark and board[4] == mark and board[1] == mark) or (board[8] == mark and board[5] == mark and board[2] == mark) or (board[9] == mark and board[6] == mark and board[3] == mark) or (board[7] == mark and board[5] == mark and board[3] == mark) or (board[9] == mark and board[5] == mark and board[1] == mark)) # 主游戏流程 print('欢迎来到井字棋游戏!') while True: # 初始化棋盘 board = [' '] * 10 player_marker = '' computer_marker = '' game_on = True # 玩家选择棋子 while player_marker not in ['X', 'O']: player_marker = input('请选择你要使用的棋子(X或O):').upper() # 设置电脑使用的棋子 if player_marker == 'X': computer_marker = 'O' else: computer_marker = 'X' turn = choose_first() print(turn + ' 先手。') while game_on: if turn == 'player': # 玩家回合 display_board(board) position = player_choice(board) place_marker(board, player_marker, position) # 判断是否获胜 if win_check(board, player_marker): display_board(board) print('恭喜你,你获胜了!') game_on = False else: # 判断是否平局 if full_board_check(board): display_board(board) print('平局!') break else: turn = 'computer' else: # 电脑回合 display_board(board) position = random.randint(1, 9) # 随机选择棋子位置 while not space_check(board, position): position = random.randint(1, 9) place_marker(board, computer_marker, position) # 判断是否获胜 if win_check(board, computer_marker): display_board(board) print('很遗憾,你输了。') game_on = False else: # 判断是否平局 if full_board_check(board): display_board(board) print('平局!') break else: turn = 'player' # 询问是否继续游戏 if input('是否继续游戏(Y/N)?').lower() != 'y': break ``` 在主游戏流程中,添加了游戏结束的判断,如果游戏结束,会提示玩家输赢或者平局,并询问是否继续游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值