Ejemplo n.º 1
0
// 对鼠标点击进行反映的监听函数。
// 输入:被监听控件(draw_window)、事件源(鼠标点击)、data。
// 返回值:FALSE
static gboolean when_key_pressed(GtkWidget * widget,GdkEventButton * button,gpointer data)
{
    printf("click at (%d,%d)\n",(int)button->x,(int)button->y);
    int i;
    int temp_x = button->x, temp_y = button->y;
    for(i=0;i<N;i++)
    {
        if(temp_x>tell_position[i]&&temp_x<tell_position[i+1])
            break;
    }
    temp_x = i;
    for(i=0;i<N;i++)
    {
        if(temp_y>tell_position[i+(N+1)]&&temp_y<tell_position[i+(N+1)+1])
            break;
    }
    temp_y = i;
    // 采用玩家-电脑放置棋子的更新方式。每次玩家下完棋后电脑立即下棋并更新界面
    // 如果电脑先手,可以在选择“computer first”后立即由电脑下棋,之后的步骤仍然是玩家-电脑下棋
    if(start_flag&&chess_board[temp_y*N+temp_x]==0)
    {
        if(who_first==1)
        {
            place_piece_on_chess_board(chess_board,temp_y,temp_x,A);
            gtk_widget_queue_draw(widget);
            if(test_who_win(chess_board)==1)
            {
                start_flag = false;
                message_box_you_win();
            }
            int min = MAX+1;
            int step;
            for(int i=0;i<N*N;i++)
            {
                if(chess_board[i]!=0)
                    continue;
                int temp_chess_board[N*N];
                copy_chess_board(temp_chess_board,chess_board);
                int ancestor[N*N];
                init_ancestor(ancestor,test_node_type(temp_chess_board));
                place_piece_on_chess_board(temp_chess_board,i/N,i%N,B);
                int temp = get_backed_up_value_of_node(temp_chess_board,0,6,ancestor,test_node_type(temp_chess_board));
                printf("temp is %d at (%d,%d)\n",temp,i/N,i%N);
                if(temp<min)
                {
                    min = temp;
                    step = i;
                }
            }
            if(test_who_win(chess_board)==0&&test_if_chess_board_full(chess_board)!=true)
                place_piece_on_chess_board(chess_board,step/N,step%N,B);
            gtk_widget_queue_draw(widget);
            if(test_who_win(chess_board)==-1)
            {
                start_flag = false;
                message_box_you_lose();
            }
            if(test_if_chess_board_full(chess_board)&&test_who_win(chess_board)==0)
            {
                start_flag = false;
                message_box_draw();
            }
        }
        else
        {
            place_piece_on_chess_board(chess_board,temp_y,temp_x,B);
            gtk_widget_queue_draw(widget);
            if(test_who_win(chess_board)==-1)
            {
                start_flag = false;
                message_box_you_win();
            }
            if(test_if_chess_board_full(chess_board)&&test_who_win(chess_board)==0)
            {
                start_flag = false;
                message_box_draw();
            }
            int max = MIN-1;
            int step;
            for(int i=0;i<N*N;i++)
            {
                if(chess_board[i]!=0)
                    continue;
                int temp_chess_board[N*N];
                copy_chess_board(temp_chess_board,chess_board);
                int ancestor[N*N];
                init_ancestor(ancestor,test_node_type(temp_chess_board));
                place_piece_on_chess_board(temp_chess_board,i/N,i%N,A);
                int temp = get_backed_up_value_of_node(temp_chess_board,0,6,ancestor,test_node_type(temp_chess_board));
                printf("temp is %d at (%d,%d)\n",temp,i/N,i%N);
                if(temp>max)
                {
                    max = temp;
                    step = i;
                }
            }
            if(test_who_win(chess_board)==0&&test_if_chess_board_full(chess_board)!=true)
                place_piece_on_chess_board(chess_board,step/N,step%N,A);
            gtk_widget_queue_draw(widget);
            if(test_who_win(chess_board)==1)
            {
                start_flag = false;
                message_box_you_lose();
            }
        }
    }
    return FALSE;
}
Ejemplo n.º 2
0
u8 *cmd_save_game(u8 *c)
{
	u8 newline_orig;
	FILE *save_stream;	// file handle
	u8 *msg;
	
	clock_state = 1;
	newline_orig = msgstate.newline_char;
	msgstate.newline_char = '@';
		
	decrypt_string(inv_obj_string, inv_obj_string+inv_obj_string_size);
	
	if (save_dir == 0)
		save_dir = vstring_new(0, 200);
	if (save_filename == 0)
		save_filename = vstring_new(0, 250);
	
	if ( state_get_info('s') != 0)// select the game
	{
		if (strlen(save_filename->data) > strlen(save_dir->data))
			msg = alloca(200 + strlen(save_filename->data));
		else
			msg = alloca(200 + strlen(save_dir->data));
		if (state_name_auto[0] == 0)
		{
			sprintf(msg, "About to save the game\ndescribed as:\n\n%s\n\nin file:\n %s\n\n%s",
					save_description, save_filename->data,
					"Press ENTER to continue.\nPress ESC to cancel.");
			message_box_draw(msg, 0, 0x23, 0);
			
			if ( user_bolean_poll() == 0)
				goto save_end;
		}
		dir_preset_change(DIR_PRESET_GAME);
		save_stream = fopen(save_filename->data, "wb");
		if ( save_stream == 0)
		{
			sprintf(msg, "The directory\n   %s\n is full or the disk is write-protected.\nPress ENTER to continue."
				, save_dir->data);
			message_box(msg);
		}
		else
		{
			if (fwrite(save_description, sizeof(u8), 0x1f, save_stream) != 0x1f)
				goto save_err;
			if (state_write(save_stream, &state, sizeof(AGI_STATE)) == 0)
				goto save_err;
			if (state_write(save_stream, objtable, objtable_size) == 0)
				goto save_err;
			if (state_write(save_stream, inv_obj_table, inv_obj_table_size*sizeof(INV_OBJ)) == 0)
				goto save_err;
			if (state_write(save_stream, inv_obj_string, inv_obj_string_size) == 0)
				goto save_err;
			if (state_write(save_stream, script_head, state.script_size<<1) == 0)
				goto save_err;
			if (state_write(save_stream, (void *)scan_start_list, logic_save_scan_start()) != 0)
				goto save_close;
		save_err:
			fclose(save_stream);
			remove(save_filename->data);
			message_box("The disk is full.\nPress ENTER to continue.");
			goto save_end;
		save_close:
			fclose(save_stream);
		}
	}
save_end:
	cmd_close_window(0);
	msgstate.newline_char = newline_orig;
	clock_state = 0;
	decrypt_string(inv_obj_string, inv_obj_string+inv_obj_string_size);
	return c;
}
Ejemplo n.º 3
0
// return 0-(size-1)
// return -1 if canceled.
// list comprises of pointers to strings.. they will be cut to fit into screen
// only scrolls up and down
// first pointer = heading for listbox.. 
// size refers to the number of strings OTHER than the header
// init is the first item to point at.
int list_box(u8 **list, int size, int init)
{
	AGISIZE list_size;
	TPOS list_pos;
	
	int item_cur;
	int item_top;
	
	AGI_EVENT *state_event;
	
	// what can i do?
	if (list == 0)
		return -1;
	
	text_attrib_push();
	push_row_col();
	text_colour(0, 0x0F);
	
	item_cur = init;
	if (item_cur >= size)
		item_cur = size-1;
	if (item_cur < 0)
		item_cur = 0;
	
	// draw message box
	message_box_draw(list[0], LISTBOX_H, LISTBOX_W, 1);
	
	// get size of remaining msgbox
	list_size.h = msgstate.tpos_edge.row - msgstate.tpos.row - msgstate.printed_height;

	list_size.w = msgstate.tsize.w;
	if (size > list_size.h)
	{
		list_size.w -= 2;

	}
	if (size < list_size.h)
		list_size.h = size;

	list_pos.row = msgstate.tpos.row + msgstate.printed_height + 1;
	list_pos.col = msgstate.tpos.col;
	
	if (size <= list_size.h)
		item_top = 0;
	else
	{
		item_top = item_cur - ((list_size.h-1) / 2);
		if (item_top < 0)
			item_top = 0;
		if (item_top > (size-list_size.h)) 
			item_top = size-list_size.h;	
	}
	
	
	list_print(list+1+item_top, &list_pos, &list_size, 
			(size>list_size.h), (item_top > 0), ((item_top + list_size.h) < size));
	
	list_box_draw_arrow(list_pos.row + item_cur - item_top);

	// later on:
	// letters.. sort  / left/right/pgup/pgdn/home/end/mouse/
	
	for(;;)
	{
		int force;
		ch_update();
		state_event = event_wait();
		//joy_butt_map(state_event);
		switch (state_event->type)
		{
			case 1:
				switch (state_event->data)
				{
					case 0xD:
						pop_row_col();
						text_attrib_pop();
						return item_cur;
						
					case 0x1B:
						pop_row_col();
						text_attrib_pop();
						return -1;
				}
				break;
				
			case 2:
				list_box_draw_blank(list_pos.row + item_cur - item_top);
				force = 0;
				switch(state_event->data)
				{
					case 1:
							item_cur--;
						break;
						
					case 5:
							item_cur++;
						break;
						
					case 4:
							item_cur = item_top + list_size.h-1;
							force = 4;
						break;
					
					case 2:
							item_cur = item_top;
							force = 2;
						break;
						
					default:
						;
				}
				
				if (item_cur < 0)
					item_cur = 0;
				else if (item_cur >= size)
					item_cur = size-1;
				
				if ((item_cur > (item_top+list_size.h-1)) || (force==4))// down below
				{
					item_top = item_cur - (list_size.h/4);
					
					if (item_top < 0)
						item_top = 0;

					if ((item_top + list_size.h) > size)
						item_top = size - list_size.h;

					list_print(list+1+item_top, &list_pos, &list_size, 
						(size>list_size.h), (item_top > 0), ((item_top + list_size.h) < size));
				}
				else 	if ((item_cur < item_top) || (force==2)) // up above
				{
					item_top = item_cur - (list_size.h*3)/ 4;
					if (item_top < 0)
						item_top = 0;
					if ((item_top + list_size.h) > size)
						item_top = size - list_size.h;

					list_print(list+1+item_top, &list_pos, &list_size, 
							(size>list_size.h), (item_top > 0), ((item_top + list_size.h) < size));
				}
				
				list_box_draw_arrow(list_pos.row + item_cur - item_top);

				break;
		}
	}
	
	pop_row_col();
	text_attrib_pop();
	return -1;
}
Ejemplo n.º 4
0
u8 *cmd_restore_game(u8 *c)
{
	u8 *code_ret;	// result to pass at the end
	u8 newline_orig;	// d0d orig
	FILE *rest_stream;
	//u8 msg[200];
	u8 *msg;
	
	clock_state = 1;
	code_ret = c;
	newline_orig = msgstate.newline_char;
	msgstate.newline_char = '@';
	
	if (save_dir == 0)
		save_dir = vstring_new(0, 200);
	if (save_filename == 0)
		save_filename = vstring_new(0, 250);
	
	if (state_get_info('r') != 0)
	{
		if (strlen(save_filename->data) > strlen(save_dir->data))
			msg = alloca(200 + strlen(save_filename->data));
		else
			msg = alloca(200 + strlen(save_dir->data));
		if (state_name_auto[0] == 0)
		{
			sprintf(msg, "About to restore the game\ndescribed as:\n\n%s\n\nfrom file:\n %s\n\n%s",
				save_description, save_filename->data, "Press ENTER to continue.\nPress ESC to cancel.");
			message_box_draw(msg, 0, 0x23, 0);
			if ( user_bolean_poll() == 0)
				goto rest_end;
		}
		dir_preset_change(DIR_PRESET_GAME);
		rest_stream = fopen(save_filename->data, "rb");
		if ( rest_stream == 0)
		{
			sprintf(msg, "Can't open file:\n %s", save_filename->data);
			message_box(msg);
		}
		else
		{
			fseek(rest_stream, 0x1F, SEEK_SET);
			if (state_read(rest_stream, &state) == 0)
				goto loc2630;
			if (state_read(rest_stream, objtable) == 0)
				goto loc2630;
			if (state_read(rest_stream, inv_obj_table) == 0)
				goto loc2630;
			if (state_read(rest_stream, inv_obj_string) == 0)
				goto loc2630;
			if (state_read(rest_stream, script_head) == 0)
				goto loc2630;
			if (state_read(rest_stream, scan_start_list) != 0)
				goto loc2647;
		loc2630:
			fclose(rest_stream);
			message_box("Error in restoring game.\nPress ENTER to quit.");
			agi_exit();  // can't recover.. we possibly just overwrote some of the
					// structures with stuff
		loc2647:
			fclose(rest_stream);
			decrypt_string(inv_obj_string, inv_obj_string+inv_obj_string_size);
			state.var[V20_COMPUTER] = computer_type;
			state.var[V26_MONITORTYPE] = display_type;
			state.var[V08_FREEMEM] = 10;
			
			if (computer_type == 0)
				state.var[V22_SNDTYPE] = 1;
			else
			{
				state.var[V22_SNDTYPE] = 3;
				flag_set(F11_NEWLOGIC0);
			}
			state_reload();
			control_state_clear();
			flag_set(F12_RESTORE);
			volumes_close();
			code_ret = 0;
			menu_enable_all();
		}
	}
rest_end:
	cmd_close_window(0);
	msgstate.newline_char = newline_orig;
	clock_state = 0;
	return code_ret;
}