| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 738 人关注过本帖
标题:关于四子棋 电脑智慧函数问题
只看楼主 加入收藏
YSYN恰少年
Rank: 1
来 自:河北保定
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-9-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
关于四子棋 电脑智慧函数问题
这是我编写的 四子棋 其实 与五子棋类似  但是 在电脑函数那一片 电脑 没有什么智慧  请帮我解决一下
代码如下:

其中diannao 是电脑的 函数
/*
    功能:四子棋
    作者:卢山
    日期:2013-5-5
*/


//The headers
#include "SDL.h"
#include "SDL_image.h"
#include <string>

//Screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 640;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Surface *bai = NULL;
SDL_Surface *hei = NULL;
SDL_Surface *daixia = NULL;
SDL_Surface *win_bg = NULL;
SDL_Surface *lose_bg = NULL;

//The event structure
SDL_Event event;

int qipan[15][15] = {0};
int who = 1;//判断谁下子
int winner = 0;

int temp[2] = {7, 7};//初始一开始的棋子位置
//The timer
class Timer
{
    private:
    //The clock time when the timer started
    int startTicks;
   
    //The ticks stored when the timer was paused
    int pausedTicks;
   
    //The timer status
    bool paused;
    bool started;
   
    public:
    //Initializes variables
    Timer();
   
    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();
   
    //Get the number of ticks since the timer started
    //or gets the number of ticks when the timer was paused
    int get_ticks();
   
    //Checks the status of the timer
    bool is_started();
    bool is_paused();   
};

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;
   
    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;
   
    //Load the image
    loadedImage = IMG_Load( filename.c_str() );
   
    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old surface
        SDL_FreeSurface( loadedImage );
        
        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }
   
    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;
   
    //Get offsets
    offset.x = x;
    offset.y = y;
   
    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;   
    }
   
    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
   
    //If there was in error in setting up the screen
    if( screen == NULL )
    {
        return false;   
    }
   
    //Set the window caption
    SDL_WM_SetCaption( "Frame Rate Test", NULL );
   
    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the background image
    bg = load_image( "bg.jpg" );
    bai = load_image( "bai.jpg" );
    hei = load_image( "hei.jpg" );
    daixia = load_image( "daixia.jpg" );
    win_bg = load_image( "win_bg.jpg" );
    lose_bg = load_image( "lose_bg.jpg" );
   
    //If there was a problem in loading the background
    if( bg == NULL )
    {
        return false;   
    }
   
    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface( bg );
    SDL_FreeSurface( bai );
    SDL_FreeSurface( hei );
    SDL_FreeSurface( daixia );
    SDL_FreeSurface( win_bg );
    SDL_FreeSurface( lose_bg );
   
    //Quit SDL
    SDL_Quit();
}
   
Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;   
}

void Timer::start()
{
    //Start the timer
    started = true;
   
    //Unpause the timer
    paused = false;
   
    //Get the current clock time
    startTicks = SDL_GetTicks();   
}

void Timer::stop()
{
    //Stop the timer
    started = false;
   
    //Unpause the timer
    paused = false;   
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;
   
        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;
   
        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;
        
        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }   
    }
   
    //If the timer isn't running
    return 0;   
}

bool Timer::is_started()
{
    return started;   
}

bool Timer::is_paused()
{
    return paused;   
}

//画棋盘  函数   
void hua(){
    int i;
    int j;
    for(i  = 0; i < 15; i++){
        for( j = 0; j < 15; j++){
            if(qipan[i][j] == 1){
                apply_surface( 21 + i * 51, 13 + j * 41, bai, screen );//将白棋子 渲染到界面上
            }else if(qipan[i][j] == 2){
                apply_surface( 21 + i * 51, 13 + j * 41, hei, screen );//将黑棋子 渲染到界面上
            }
            
            
        }
    }
    apply_surface( 21 + temp[0] * 51, 13 + temp[1] * 41, daixia, screen );//你将要下棋子的位置 的提示框
}

int xiaqi(){
    //键盘按下
    if( event.type == SDL_KEYDOWN )
    {
        //判断按下的是哪一个
        switch( event.key.keysym.sym )
        {
            case SDLK_UP:
                if(temp[1] > 0) temp[1]--;
                break;
            case SDLK_DOWN:
                if(temp[1] < 14)temp[1]++;
                break;
            case SDLK_LEFT:
                if(temp[0] > 0) temp[0]--;
                break;
            case SDLK_RIGHT:
                if(temp[0] < 14)temp[0]++;
                break;   
            case SDLK_RETURN:
                if(qipan[temp[0]][temp[1]] == 0){
                    qipan[temp[0]][temp[1]] = 1;
                    who = 2;
                }
        }
    }
    return 0;
}

void diannao(){
    if(who == 1) return;
    //电脑判断位置
    int i,j;
    int n=0,m=0;
    int a = 0;//循环结束
    for(i = 0; i < 15; i++){
        for(j = 0; j < 15; j++){
            if((qipan[i][j]==1&&qipan[i-1][j]!=2&&qipan[i-1][j]!=1)||(qipan[i][j]==1&&qipan[i+1][j]!=2&&qipan[i+1][j]!=2)||(qipan[i][j]==1&&qipan[i][j+1]!=2&&qipan[i][j+1]!=2)||(qipan[i][j]==1&&qipan[i][j-1]!=2&&qipan[i][j-1]!=2))
            {   
                if((qipan[i][j]==1&&qipan[i+1][j+1]!=2&&qipan[i+1][j+1]!=2)||(qipan[i][j]==1&&qipan[i-1][j-1]!=2&&qipan[i-1][j-1]!=2)||(qipan[i][j]==1&&qipan[i+1][j-1]!=2&&qipan[i+1][j-1]!=2)||(qipan[i][j]==1&&qipan[i-1][j+1]!=2&&qipan[i-1][j+1]!=2))
                {
                    if((qipan[i][j]==1&&qipan[i+1][j]==1)||(qipan[i][j]==1&&qipan[i][j+1]==1))
                    {
                        qipan[i-1][j]=2;
                        a=1;
                    }
                    if((qipan[i][j]==1&&qipan[i+1][j-1]==1)||(qipan[i][j]==1&&qipan[i-1][j+1]==1))
                    {
                        qipan[i+1][j+1]=2;
                        a=1;
                    }
                    if(qipan[i][j]==2&&qipan[i-1][j]==2&&qipan[i-1][j]!=1)
               
                    {
                        qipan[i-1][j]=2;
                        a=1;
                    }
                    else
                    {
                        qipan[i][j+1]=2;
                        a=1;
                    }
                }
                /*if((qipan[i][j]==1&&qipan[i+1][j]==1)||(qipan[i][j]==1&&qipan[i][j+1]==1))
                {
                    qipan[i-1][j]=2;
                    a=1;
                }
                if((qipan[i][j]==1&&qipan[i+1][j-1]==1)||(qipan[i][j]==1&&qipan[i-1][j+1]==1))
                {
                    qipan[i+1][j+1]=2;
                    a=1;
                    
                }
                else
                {
                    qipan[i][j+1]=2;
                    a=1;
                    
                }*/
                /*a=1;
                break;*/
            }
            
         /*   if(qipan[i][j] == 1){
                qipan[i-1][j] = 2;
                a = 1;
                break;
            }*/
        }
        if(a == 1) break;
    }
    who = 1;

}
/*、
    函数名字:win
    函数功能:判断输赢
    函数参数:无
    函数返回值:1||2

*/
int win(){

    int i,j;
    for(i=0;i<14;i++)
    {
        for(j=0;j<14;j++)
        {
            if((qipan[i][j]==1&&qipan[i+1][j]==1&&qipan[i+2][j]==1&&qipan[i+3][j])==1||(qipan[i][j]==1&&qipan[i][j+1]==1&&qipan[i][j+2]==1&&qipan[i][j+3]==1))
            {
                winner = 1;
                return 1;
            }
            if((qipan[i][j]==2&&qipan[i+1][j]==2&&qipan[i+2][j]==2&&qipan[i+3][j])==2||(qipan[i][j]==2&&qipan[i][j+1]==2&&qipan[i][j+2]==2&&qipan[i][j+3]==2))
            {
                winner = 2;
                return 2;
            }
            if((qipan[i][j]==1&&qipan[i+1][j+1]==1&&qipan[i+2][j+2]==1&&qipan[i+3][j+3])==1||(qipan[i][j]==1&&qipan[i-1][j-1]==1&&qipan[i-2][j-2]==1&&qipan[i-3][j-3]==1))
            {
                winner = 1;
                return 1;
            }
            if((qipan[i][j]==2&&qipan[i-1][j-1]==2&&qipan[i-2][j-2]==2&&qipan[i-3][j-3])==2||(qipan[i][j]==2&&qipan[i+1][j+1]==2&&qipan[i+2][j+2]==2&&qipan[i+3][j+3]==2))
            {
                winner = 2;
                return 2;
            }
            if((qipan[i][j]==1&&qipan[i+1][j-1]==1&&qipan[i+2][j-2]==1&&qipan[i+3][j-3])==1||(qipan[i][j]==1&&qipan[i-1][j+1]==1&&qipan[i-2][j+2]==1&&qipan[i-3][j+3]==1))
            {
                winner = 1;
                return 1;
            }

        }
    }

  /*  if(qipan[3][3] == 1){
        winner = 1;
        return 1;
    }*/
    return 0;
}

int main( int argc, char* args[] )
{
    //Quit flag
    bool quit = false;

   
    //Timer used to calculate the frames per second
    Timer fps;
   

   
    //Initialize
    if( init() == false )
    {
        return 1;
    }
   
    //Load the files
    if( load_files() == false )
    {
        return 1;
    }
   
   
    //Start the frame timer
   
   
    //While the user hasn't quit
    while( quit == false )
    {
        
        fps.start();
        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        
        //Apply the background
        xiaqi();
        diannao();
        apply_surface( 0, 0, bg, screen );
        
        hua();
        quit = win() || quit;
        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;   
        }


        //定义屏幕刷新时间
        //15为每秒刷新15次
        while( fps.get_ticks() < 1000 / 15 )
        {
            //wait   
        }
      
    }
   
    if(winner == 1){
        apply_surface( 0, 0, win_bg, screen );
        hua();
        SDL_Flip( screen );
        SDL_Delay( 3000 );
    }else if(winner == 2){
        apply_surface( 0, 0, lose_bg, screen );
        hua();
        SDL_Flip( screen );
        SDL_Delay( 3000 );
    }
   


    //Clean up
    clean_up();
   
    return 0;   
}
搜索更多相关主题的帖子: include 五子棋 screen 
2013-09-20 12:28
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:4 
智慧的定义是啥,你可以参考下五子棋的代码

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-09-24 18:00
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:4 
学习
2013-09-24 18:13
幸运的狼
Rank: 1
等 级:新手上路
帖 子:3
专家分:4
注 册:2013-9-23
收藏
得分:4 
学习
2013-09-24 22:15
喝水的鱼
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:50
专家分:113
注 册:2012-9-10
收藏
得分:4 
学习下
2013-09-24 23:05
小小程序猿
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:1
帖 子:755
专家分:2785
注 册:2013-7-18
收藏
得分:4 
我只想说,代码真长,其他的就帮不了你了,学习学习

孤独与寂寞是催化一个人迅速成长的良药,没有之一
2013-09-25 12:48
czz5242199
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:660
专家分:2400
注 册:2011-10-26
收藏
得分:4 
四子棋?不是先手必胜吗。。有什么好下的
2013-09-25 16:31
快速回复:关于四子棋 电脑智慧函数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018357 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved