zoukankan      html  css  js  c++  java
  • 五子棋——C

    人生第一个图形界面程序,拉出来纪念纪念- -

    数据共享上基本上全是全局变量,也没有任何FPS限制。。

    当然没有图片你也是运行不了的,就当作参考吧,AI部分很简单的按照一个计分表来算分

    //SDL头文件
    #include <stdio.h>
    #include <windows.h>
    #include <string>
    #include <sstream>
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include "SDL/SDL_ttf.h"
    #include "SDL/SDL_mixer.h"
    //#program comment(lib,"SDL.lib")
    #define PIAN_YI 30//偏移像素
    
    //画面参数
    const int SCREEN_WIDTH = 800;
    const int SCREEN_HEIGHT = 600;
    const int SCREEN_BPP = 32;
    
    //按钮参数
    const int CLIP_MOUSEOUT = 0;
    const int CLIP_MOUSEOVER = 1;
    
    //定义画面
    SDL_Surface* screen = NULL;//主屏幕
    SDL_Surface* Player_1 = NULL;//玩家二走棋
    SDL_Surface* Player_2 = NULL;//玩家一走棋
    SDL_Surface* Chess_Piece_Black = NULL;//黑棋
    SDL_Surface* Chess_Piece_White = NULL;//白棋
    SDL_Surface* Chess_Piece_Green = NULL;//走步提醒
    SDL_Surface* Chess_Board = NULL;//棋盘
    SDL_Surface* Background = NULL;//背景
    SDL_Surface* Button_Sur = NULL;//按钮
    SDL_Surface* Seconds = NULL;//时间
    SDL_Surface* Welcome = NULL;//欢迎界面
    
    //定义音效
    Mix_Chunk* low = NULL;
    
    //定义指向检错文件的指针
    FILE* error=fopen("error.txt","at+");
    
    //定义事件结构
    SDL_Event event;
    
    //定义字体
    TTF_Font* font = NULL;
    
    //字体颜色
    SDL_Color textColor = {0x00,0x00,0x00};
    
    //定义棋局数组
    int State[16][16]={0};
    
    //用于表示玩家1或2
    int Player_Flag=0;
    //表示第几个按钮
    int Button_Flag=0;
    
    //定义棋子的xy坐标
    int Chess_x,Chess_y;
    
    Uint32 P1_Time,P2_Time;//定义双方所使用的时间
    Uint32 P1_Time_Start=0,P2_Time_Start=0;//计时起始时间
    
    SDL_Rect clips[4][2];//4个按钮的属性
    
    class Button
    {
        private:
        SDL_Rect box;//按钮的属性
        SDL_Rect* clip;//按钮图要显示的部分
    
        public:
        //初始化变量
        Button(int x,int y,int w,int h,int Button_Flag);
        //处理事件并设置传输区域
        bool Handle_Events();
        //显示按钮
        void Show();
    };
    
    Button PvAI_Button(500,425,100,50,0);
    Button PvP_Button(500,475,100,50,1);
    Button Again_Button(175,560,100,50,2);
    Button End_Button(500,560,100,50,3);
    
    
    //功能函数
    bool Init();//初始化
    void Clean_Up();//善后,释放内存
    void LOCK(SDL_Surface* screen);//锁屏函数
    void ULOCK(SDL_Surface* screen);//解锁函数
    SDL_Surface* Load_Image(char* filename,Uint8 R,Uint8 G,Uint8 B,bool flag);//图片加载&附带去背景色,flag标志选择是否去色
    bool Load_Files();//整合加载函数
    void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface* destination,SDL_Rect* clip);//图片显示
    void Set_Clips();//设置按钮
    
    //效果函数
    int Start();//欢迎界面,并绘制棋盘
    void Game_Control();//PvP游戏运行主体程序
    void Game_Control_AI();//PvAI游戏运行主体程序
    void Show_Player(bool flag);//更新提醒走棋的图片
    int Judge_Luozi_PE(int x,int y);//PvP判断当前xy坐标是否可以落子,并落子
    int Judge_Luozi_AI(int x,int y);//PvAI判断当前xy坐标是否可以落子,并落子
    int Judge_Shengfu(int x,int y);//判断胜负
    void Timer();//计时器
    void Draw_Chess_Board();//画棋盘
    int Draw_End();//画再次/结束
    
    //AI
    int AI();
    int Return_Score(int AI_n);
    
    int main(int argc,char* args[])
    {
        if(Init()==false)
            return 1;
        atexit(SDL_Quit);
        if(Load_Files()==false)
            return 1;
        
        Set_Clips();
    
        int Flag;
        Flag=Start();
        if(Flag==1)
            {
                Draw_Chess_Board();
                Game_Control_AI();
            }
        else if(Flag==2)
            {
                Draw_Chess_Board();
                Game_Control();
            }
        //Game_Control();
    
        //SDL_Delay(2000);
        Clean_Up();
    
        return 0;
    }
    
    
    
    
    //SDL & SDL_image & SDL_ttf 初始化
    bool Init()
    {
        if( SDL_Init( SDL_INIT_VIDEO|SDL_INIT_AUDIO ) == -1 )
        {
            fprintf(error,"Could not initialize SDL:%s
    ",SDL_GetError());
            return false;
        }
        
        screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);//SDL_NOFRAME
        if( screen == NULL )
        {
            fprintf(error,"Could not set %d*%d*%d video mode:%s
    ",SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_GetError());
            return false;
        }
    
        if( TTF_Init() == -1 )
        {
            fprintf(error,"Could not initialize SDL_ttf:%s
    ",SDL_GetError());
            return false;
        }
    
        if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
        {
            fprintf(error,"Could not initialize SDL_mixer:%s
    ",SDL_GetError());
            return false;
        }
    
        //设置窗口标题
        SDL_WM_SetCaption("Link Five","Link Five");
    
        return true;
    }
    
    
    //清理函数
    void Clean_Up()
    {
        //释放画面
        SDL_FreeSurface(Chess_Piece_Black);
        //SDL_FreeSurface(Chess_Piece_Black_);
        SDL_FreeSurface(Chess_Piece_White);
        SDL_FreeSurface(Chess_Piece_Green);
        SDL_FreeSurface(Chess_Board);
        SDL_FreeSurface(Background);
        SDL_FreeSurface(Welcome);
        SDL_FreeSurface(Button_Sur);
        SDL_FreeSurface(Player_1);
        SDL_FreeSurface(Player_2);
    
        //关闭字体
        TTF_CloseFont(font);
        //退出字体驱动
        TTF_Quit();
    
        //释放音效内存
        Mix_FreeChunk(low);
        //退出SDL_mixer
        Mix_CloseAudio();
        
        //退出图形驱动
        SDL_Quit();
    
        //关闭文件
        fclose(error);
    }
    
    
    //把锁屏函数打包
    void LOCK(SDL_Surface* screen)
    {
        if(SDL_MUSTLOCK(screen))
            if(SDL_LockSurface(screen)<0)
                {
                    printf("Could not lock: %s
    
    ",SDL_GetError());
                    return;
                }
    }
    
    
    //把解锁函数打包
    void ULOCK(SDL_Surface* screen)
    {
        if(SDL_MUSTLOCK(screen))
            SDL_UnlockSurface(screen);
    }
    
    
    //图片加载函数&附带去背景色,flag标志选择是否去色
    SDL_Surface* Load_Image(char* filename,Uint8 R,Uint8 G,Uint8 B,bool flag)
    {
        SDL_Surface* loadedImage = NULL;
        SDL_Surface* optimizedImage = NULL;
    
        loadedImage = IMG_Load(filename);
    
        if(loadedImage!=NULL)
        {
            optimizedImage=SDL_DisplayFormat(loadedImage);
            SDL_FreeSurface(loadedImage);
    
            if(optimizedImage!=NULL && flag)
            {
                //设置色键,把背景去掉
                SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY,SDL_MapRGB(optimizedImage->format,R,G,B));
            }
        }
        return optimizedImage;
    }
    
    
    //图片显示函数
    void Apply_Surface(int x,int y,SDL_Surface* source,SDL_Surface* destination,SDL_Rect* clip=NULL)//设置clip默认为NULL
    {
        SDL_Rect offset;
    
        offset.x = x;
        offset.y = y;
    
        SDL_BlitSurface( source, clip, destination, &offset );
        //SDL_Flip(screen);
    }
    
    
    //整合加载函数
    bool Load_Files()
    {
        //加载各种图片
        Chess_Piece_Black = Load_Image("PIC/black.png",0xFF,0xFF,0xFF,true);
        Chess_Piece_White = Load_Image("PIC/white.png",0x00,0x00,0x00,true);
        Chess_Piece_Green = Load_Image("PIC/green.png",0xFF,0xFF,0xFF,true);
        Chess_Board = Load_Image("PIC/board.png",0x00,0x00,0x00,false);
        Background = Load_Image("PIC/background.png",0x00,0x00,0x00,false);
        Welcome = Load_Image("PIC/start.png",0x00,0x00,0x00,false);
        Button_Sur = Load_Image("PIC/button.png",0x00,0x00,0x00,false);
    
        Player_1 = Load_Image("PIC/player1.png",0x00,0x00,0x00,false);
        Player_2 = Load_Image("PIC/player2.png",0x00,0x00,0x00,false);
    
        //打开字体库
        font = TTF_OpenFont("arial.ttf",30);
    
        //加载音效
        low = Mix_LoadWAV( "low.wav" );
    
        if(low==NULL)
            fprintf(error,"%s
    ",SDL_GetError());
    
    
        if(Chess_Piece_Black==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                //return false;
            }
        if(Chess_Piece_White==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                //return false;
            }
        if(Chess_Piece_Green==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                //return false;
            }
        if(Chess_Board==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                return false;
            }
        if(Player_1==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                return false;
            }
        if(Player_2==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                return false;
            }
        if(Button_Sur==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                return false;
            }
        if(Background==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                return false;
            }
        if(Welcome==NULL)
            {
                fprintf(error,"%s
    ",SDL_GetError());
                return false;
            }
        if(font==NULL)
            return false;
    
        return true;
    }
    
    
    //更新提醒走棋的图片
    void Show_Player(int Player_Flag)
    {
        for(int i=-50;i<=0;i++)
            {
                if(Player_Flag==1)
                    Apply_Surface(385,i,Player_1,screen,NULL);
                else
                    Apply_Surface(385,i,Player_2,screen,NULL);
                SDL_Flip(screen);
                SDL_Delay(20);
            }
    }
    
    
    //PvP游戏运行主体程序
    void Game_Control()
    {
        int done=0;
        //玩家1先手
        Player_Flag=1;
        //开启计时
        P1_Time_Start=SDL_GetTicks();
        //P2_Time_Start=SDL_GetTicks();
        //Time=SDL_GetTicks();//开启定时器
    
        while(done==0)
            {
                Timer();
                while(SDL_PollEvent(&event))
                    {
                        if(event.type==SDL_QUIT) done=1;//按×结束游戏
    
                        if(event.type==SDL_KEYDOWN)
                            if(event.key.keysym.sym==SDLK_ESCAPE)
                                done=1;
    
                        if(event.type==SDL_MOUSEBUTTONDOWN)
                            if(event.button.button==SDL_BUTTON_LEFT)
                                {
                                    Chess_x=event.motion.x;
                                    Chess_y=event.motion.y;
                                    if(Judge_Luozi_PE(Chess_x,Chess_y)==1)
                                        done=Draw_End();
                                }
                    }
                /*
                if(alpha==0)
                    alpha=SDL_ALPHA_OPAQUE;
                alpha-=5;
    
                SDL_SetAlpha(Chess_Piece_Black_,SDL_SRCALPHA,alpha);
                SDL_SetAlpha(Chess_Piece_White_,SDL_SRCALPHA,alpha);
                if(Player_Flag==1)
                    Apply_Surface(Shan_x,Shan_y,Chess_Piece_White_,screen,NULL);    
                else
                    Apply_Surface(Shan_x,Shan_y,Chess_Piece_Black_,screen,NULL);
                */
            }
        if(done==2)
            {
                int Flag;
                Flag=Start();
                if(Flag==1)
                    {
                        Draw_Chess_Board();
                        Game_Control_AI();
                    }
                else if(Flag==2)
                    {
                        Draw_Chess_Board();
                        Game_Control();
                    }
            }
    }
    
    
    //PvAI游戏运行主体程序
    void Game_Control_AI()
    {
        int done=0;
        //玩家1先手
        Player_Flag=1;
        //开启计时
        //P1_Time_Start=SDL_GetTicks();
        //P2_Time_Start=SDL_GetTicks();
        //Time=SDL_GetTicks();//开启定时器
    
        while(done==0)
            {
                //Timer();
                while(SDL_PollEvent(&event))
                    {
                        if(event.type==SDL_QUIT) done=1;//按×结束游戏
    
                        if(event.type==SDL_KEYDOWN)
                            if(event.key.keysym.sym==SDLK_ESCAPE)
                                done=1;
    
                        if(event.type==SDL_MOUSEBUTTONDOWN)
                            if(event.button.button==SDL_BUTTON_LEFT)
                                {
                                    Chess_x=event.motion.x;
                                    Chess_y=event.motion.y;
    
                                    Player_Flag=1;
                                    int IS_Luozi=Judge_Luozi_AI(Chess_x,Chess_y);
                                    if(IS_Luozi==2)
                                        {
                                            done=Draw_End();
                                        }
                                    else if(IS_Luozi==1)
                                        {
                                            Player_Flag=2;
                                            Mix_PlayChannel(-1,low,0);//音效
                                        }
                                    if(Player_Flag==2 && done==0 && AI()==1)
                                        done=Draw_End();
                                    //Mix_PlayChannel(-1,low,0);//音效
                                    
                                    //system("cls");
                                    for(int i=0;i<16;i++)
                                        {
                                            for(int j=0;j<16;j++)
                                                {
                                                    if(State[i][j]==0)
                                                        printf("  ");
                                                    else
                                                        printf("%d ",State[i][j]);
                                                }
                                            printf("
    ");
                                        }
                                    printf("
    
    
    ");
                                    
                                }
                    }
            }
        if(done==2)
            {
                int Flag;
                Flag=Start();
                if(Flag==1)
                    {
                        Draw_Chess_Board();
                        Game_Control_AI();
                    }
                else if(Flag==2)
                    {
                        Draw_Chess_Board();
                        Game_Control();
                    }
            }
    }
    
    
    
    //PvP判断当前xy坐标是否可以落子,并落子
    int Judge_Luozi_PE(int x,int y)
    {
        bool flag1=false,flag2=false;
        for(int i=0;i<16;i++)
            {
                if(x > 175+i*PIAN_YI-10 && x < 175+i*PIAN_YI+10)
                    {flag1=true;}
                if(y > 75+i*PIAN_YI-10 && y < 75+i*PIAN_YI+10)
                    {flag2=true;}
            }
        //printf("%d,%d ",flag1,flag2);
        if(flag1 && flag2)
            {
                for(int i=0;i<16;i++)
                    for(int j=0;j<16;j++)
                        if((x > 175+i*PIAN_YI-10 && x < 175+i*PIAN_YI+10) && (y > 75+j*PIAN_YI-10 && y < 75+j*PIAN_YI+10))
                            {
                                //printf("%d,%d ",i,j);
                                if(State[j][i]==0)                            
                                    {
                                        if(Player_Flag==1)
                                            {
                                                LOCK(screen);
                                                Apply_Surface(175+i*PIAN_YI-25,75+j*PIAN_YI-25,Chess_Piece_Black,screen,NULL);
                                                SDL_Flip(screen);
                                                ULOCK(screen);
                                                Mix_PlayChannel(-1,low,0);//音效
                                                if(Judge_Shengfu(j,i)==1)//判断胜负
                                                    return 1;
                                                Player_Flag=2;
                                                Show_Player(Player_Flag);
                                            }
                                        else if(Player_Flag==2)
                                            {
                                                LOCK(screen);
                                                Apply_Surface(175+i*PIAN_YI-25,75+j*PIAN_YI-25,Chess_Piece_White,screen,NULL);
                                                SDL_Flip(screen);
                                                ULOCK(screen);
                                                Mix_PlayChannel(-1,low,0);//音效
                                                State[j][i]=2;
    
                                                if(Judge_Shengfu(j,i)==1)//判断胜负
                                                    return 1;
                                                Player_Flag=1;
                                                Show_Player(Player_Flag);
                                            }
                                    }
                            }
    
                if(Player_Flag==1)
                    P1_Time_Start=SDL_GetTicks()-P1_Time;
                else
                    P2_Time_Start=SDL_GetTicks()-P2_Time;
            }
        return 0;
    }
    
    
    
    //PvAI判断当前xy坐标是否可以落子,并落子
    int Judge_Luozi_AI(int x,int y)
    {
        bool flag1=false,flag2=false;
        for(int i=0;i<16;i++)
            {
                if(x > 175+i*PIAN_YI-10 && x < 175+i*PIAN_YI+10)
                    {flag1=true;}
                if(y > 75+i*PIAN_YI-10 && y < 75+i*PIAN_YI+10)
                    {flag2=true;}
            }
        //printf("%d,%d ",flag1,flag2);
        if(flag1 && flag2)
            {
                for(int i=0;i<16;i++)
                    for(int j=0;j<16;j++)
                        if((x > 175+i*PIAN_YI-10 && x < 175+i*PIAN_YI+10) && (y > 75+j*PIAN_YI-10 && y < 75+j*PIAN_YI+10))
                            {
                                //printf("%d,%d ",i,j);
                                if(State[j][i]==0)                            
                                    {
            
                                        LOCK(screen);
                                        Apply_Surface(175+i*PIAN_YI-25,75+j*PIAN_YI-25,Chess_Piece_Black,screen,NULL);
                                        SDL_Flip(screen);
                                        ULOCK(screen);
    
                                        State[j][i]=1;
    
                                        if(Judge_Shengfu(j,i)==1)//判断胜负
                                            return 2;
                                        Show_Player(2);
                                        return 1;
                                    }
                            }
            }
        return 0;
    }
    
    
    
    //显示欢迎界面并画棋盘
    int Start()
    {
        LOCK(screen);
        Apply_Surface(0,0,Background,screen,NULL);
        Apply_Surface(175,75,Welcome,screen,NULL);
        ULOCK(screen);
        SDL_Flip(screen);
    
        bool quit = false;
        while(quit == false)
            {
                
                if(event.type==SDL_QUIT) return 0;
    
                if(event.type==SDL_KEYDOWN)
                    if(event.key.keysym.sym==SDLK_ESCAPE)
                        return 0;;
                if(SDL_PollEvent(&event))
                    {
                        Button_Flag=0;
                        PvAI_Button.Show();
                        if(PvAI_Button.Handle_Events()==1)
                            return 1;
                        //printf("%d",PvAI_Button.Handle_Events());
    
                        Button_Flag=1;
                        PvP_Button.Show();
                        if(PvP_Button.Handle_Events()==1)
                            return 2;
                        //printf("%d",PvP_Button.Handle_Events());
    
                        /*
                        if(event.type == SDL_QUIT)
                            quit=true;
                        */
                        /*
                        if(event.type==SDL_KEYDOWN)
                            if(event.key.keysym.sym==SDLK_ESCAPE)
                                quit=true;
                        */
                    }
                SDL_Flip(screen);    
            }
        return 0;
    }
    
    
    //判断胜负
    int Judge_Shengfu(int x,int y)
    {
        int j,k;
        int n1,n2 ;
        while(1)
        {
    
            /*对水平方向进行判断是否有5个同色的圆*/
            n1=0;
            n2=0;
            /*水平向左数*/
            for(j=x,k=y;j>=0;j--)
            {
                if(State[j][k]==Player_Flag)
                    n1++;
                else
                    break;
            }
            /*水平向右数*/
            for(j=x,k=y;j<=15;j++)
            {
                if(State[j][k]==Player_Flag)
                n2++;
                else
                    break;
            }
            if(n1+n2-1>=5)
            {
                return(1);
            }
    
            /*对垂直方向进行判断是否有5个同色的圆*/
            n1=0;
            n2=0;
            /*垂直向上数*/
            for(j=x,k=y;k>=0;k--)
            {
                if(State[j][k]==Player_Flag)
                    n1++;
                else
                    break ;
            }
            /*垂直向下数*/
            for(j=x,k=y;k<=15;k++)
            {
                if(State[j][k]==Player_Flag)
                    n2++;
                else
                    break ;
            }
            if(n1+n2-1>=5)
            {
                return(1);
            }
    
            /*从左上方到右下方进行判断是否有5个同色的圆*/
            n1=0;
            n2=0;
            /*向左上方数*/
            for(j=x,k=y;(j>=0)&&(k>=0);j--,k--)
            {
                if(State[j][k]==Player_Flag)
                    n1++;
                else
                    break;
            }
            /*向右下方数*/
           for(j=x,k=y;(j<=15)&&(k<=15);j++,k++)
            {
                if(State[j][k]==Player_Flag)
                    n2++;
                else
                    break;
            }
            if(n1+n2-1>=5)
            {
                return(1);
            }
    
            /*从右上方到左下方进行判断是否有5个同色的圆*/
            n1=0;
            n2=0;
            /*向右上方数*/
            for(j=x,k=y;(j<=15)&&(k>=0);j++,k--)
            {
                if(State[j][k]==Player_Flag)
                    n1++;
                else
                    break;
            }
            /*向左下方数*/
            for(j=x,k=y;(j>=0)&&(k<=15);j--,k++)
            {
                if(State[j][k]==Player_Flag)
                    n2++;
                else
                    break;
            }
            if(n1+n2-1>=5)
            {
                return(1);
    
            }
            return(0);
        }
    }
    
    
    //计时器
    void Timer()
    {
        //static Uint32 P1=0,P2=0;
        if(Player_Flag==1)
            {
                P1_Time=SDL_GetTicks()-P1_Time_Start;
                std::stringstream time;
                time << (int)P1_Time/1000;
                Seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor );
    
                SDL_Rect clip;
                clip.x=80;
                clip.y=0;
                clip.w=70;
                clip.h=30;
                Apply_Surface(80,0,Background,screen,&clip);
                Apply_Surface(80,-5,Seconds,screen,NULL);
                SDL_Flip(screen);
            }
        else if(Player_Flag==2)
            {
                P2_Time=SDL_GetTicks()-P2_Time_Start;
                std::stringstream time;
                time << (int)P2_Time/1000;
                Seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor );
    
                SDL_Rect clip;
                clip.x=80;
                clip.y=0;
                clip.w=70;
                clip.h=30;
                Apply_Surface(740,0,Background,screen,&clip);
                Apply_Surface(740,-5,Seconds,screen,NULL);
                SDL_Flip(screen);
            }
    }
    
    
    //设置按钮
    void Set_Clips()
    {
        clips[0][CLIP_MOUSEOUT].x = 0; 
        clips[0][CLIP_MOUSEOUT].y = 300;
        clips[0][CLIP_MOUSEOUT].w = 100;
        clips[0][CLIP_MOUSEOUT].h = 50;
    
        clips[0][CLIP_MOUSEOVER].x = 0;
        clips[0][CLIP_MOUSEOVER].y = 350;
        clips[0][CLIP_MOUSEOVER].w = 100;
        clips[0][CLIP_MOUSEOVER].h = 50;
    
    
        clips[1][CLIP_MOUSEOUT].x = 0;
        clips[1][CLIP_MOUSEOUT].y = 200;
        clips[1][CLIP_MOUSEOUT].w = 100;
        clips[1][CLIP_MOUSEOUT].h = 50;
    
        clips[1][CLIP_MOUSEOVER].x = 0;
        clips[1][CLIP_MOUSEOVER].y = 250;
        clips[1][CLIP_MOUSEOVER].w = 100;
        clips[1][CLIP_MOUSEOVER].h = 50;
    
    
        clips[2][CLIP_MOUSEOUT].x = 0;
        clips[2][CLIP_MOUSEOUT].y = 0;
        clips[2][CLIP_MOUSEOUT].w = 100;
        clips[2][CLIP_MOUSEOUT].h = 50;
    
        clips[2][CLIP_MOUSEOVER].x = 0;
        clips[2][CLIP_MOUSEOVER].y = 50;
        clips[2][CLIP_MOUSEOVER].w = 100;
        clips[2][CLIP_MOUSEOVER].h = 50;
    
        clips[3][CLIP_MOUSEOUT].x = 0;
        clips[3][CLIP_MOUSEOUT].y = 100;
        clips[3][CLIP_MOUSEOUT].w = 100;
        clips[3][CLIP_MOUSEOUT].h = 50;
    
        clips[3][CLIP_MOUSEOVER].x = 0;
        clips[3][CLIP_MOUSEOVER].y = 150;
        clips[3][CLIP_MOUSEOVER].w = 100;
        clips[3][CLIP_MOUSEOVER].h = 50;
    }
    
    
    //
    Button::Button(int x,int y,int w,int h,int Button_Flag)
    {
        box.x = x;
        box.y = y;
        box.w = w;
        box.h = h;
        clip = &clips[Button_Flag][CLIP_MOUSEOUT];
    }
    
    
    
    //处理事件并设置传输区域
    bool Button::Handle_Events()
    {
        int x = 0,y = 0;
    
        if(event.type == SDL_MOUSEMOTION)
            {
                x = event.motion.x;
                y = event.motion.y;
    
                if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
                    clip=&clips[Button_Flag][CLIP_MOUSEOVER];
                else
                    clip=&clips[Button_Flag][CLIP_MOUSEOUT];        
            }
    /*
        if(event.type==SDL_MOUSEBUTTONDOWN)
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                    {
                        x = event.button.x;
                        y = event.button.y;
    
                        if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
                                clip = &clips[Button_Flag][CLIP_MOUSEOVER];
                    }
            }
    */
    
        if(event.type==SDL_MOUSEBUTTONUP)
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                    {
                        x = event.button.x;
                        y = event.button.y;
    
                        if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
                            {
                                return true;
                            }
                    }
            }
        return false;
    }
    
    
    //显示按钮
    void Button::Show()
    {
        Apply_Surface(box.x,box.y,Button_Sur,screen,clip);
    }
    
    
    //画棋盘
    void Draw_Chess_Board()
    {
        SDL_Rect dest;
        for(int i=0;i<=225;i++)
            {
                dest.x=225-i;
                dest.y=225-i;
                dest.w=2*i;
                dest.h=2*i;
                Apply_Surface(400-i,300-i,Chess_Board,screen,&dest);
                SDL_Flip(screen);
                SDL_Delay(5);
            }
            
    }
    
    
    //画再次/结束
    int Draw_End()
    {
        //Button_Flag=2;
        bool quit = false;
    
        bool Again_Flag = false;
        bool End_Flag = false;
    
        while(quit == false)
            {
                if(SDL_PollEvent(&event))
                    {
                        Button_Flag=2;
                        Again_Button.Show();
                        Again_Flag = Again_Button.Handle_Events();
    
                        Button_Flag=3;
                        End_Button.Show();        
                        End_Flag = End_Button.Handle_Events();
    
                        if(event.type == SDL_QUIT)
                            quit=true;
    
                        if(event.type==SDL_KEYDOWN)
                            if(event.key.keysym.sym==SDLK_ESCAPE)
                                quit=true;
                    }
                SDL_Flip(screen);
    
                if(End_Flag==true)
                    return 1;
    
                if(Again_Flag==true)
                    {
                        
                        for(int i=0;i<16;i++)
                            for(int j=0;j<16;j++)
                                State[i][j]=0;
    
                        //先画棋盘再恢复时间,不然有两秒的差距
                        //Apply_Surface(0,0,Background,screen,NULL);
                        //Draw_Chess_Board();
    
                        //时间恢复
                        P1_Time=0;
                        P2_Time=0;
                        P1_Time_Start=SDL_GetTicks();
                        P2_Time_Start=SDL_GetTicks();
    
                        Button_Flag=1;    
                        return 2;
                    }
            }
        return 1;
    }
    
    
    
    
    
    int AI()
    {
        int AI[16][16][5]={0},PE[16][16][5]={0};
        int AI_n,PE_n;
        int p,q,m,n;
    
        for(int i=0;i<16;i++)
            for(int j=0;j<16;j++)
                if(State[i][j]==0)
                    {
                
                        //AI第一方向的得分
                        AI_n=1;
                        for(p=j-1;p>=0;p--)
                            if(State[i][p]==2)
                                AI_n++;
                            else
                                break;
    
                        for(q=j+1;q<=15;q++)
                            if(State[i][q]==2)
                                AI_n++;
                            else
                                break;
    
                        if(State[i][p]==1||p==-1||State[i][q]==1||q==16)
                            AI[i][j][1]=Return_Score(AI_n);
                        else if(State[i][p]==0 && State[i][q]==0)
                            AI[i][j][1]=Return_Score(AI_n)+1;
    
                        //PE第一方向的得分
                        PE_n=1;
                        for(p=j-1;p>=0;p--)
                            if(State[i][p]==1)
                                PE_n++;
                            else
                                break;
    
                        for(q=j+1;q<=15;q++)
                            if(State[i][q]==1)
                                PE_n++;
                            else
                                break;
    
                        if(State[i][p]==2||p==-1||State[i][q]==2||q==16)
                            PE[i][j][1]=Return_Score(PE_n);
                        else if(State[i][p]==0 && State[i][q]==0)
                            PE[i][j][1]=Return_Score(PE_n)+1;
    
    
                        //AI第二方向的得分
                        AI_n=1;
                        for(p=i-1;p>=0;p--)
                            if(State[p][j]==2)
                                AI_n++;
                            else
                                break;
    
                        for(q=i+1;q<=15;q++)
                            if(State[q][j]==2)
                                AI_n++;
                            else
                                break;
    
                        if(State[p][j]==1||p==-1||State[q][j]==1||q==16)
                            AI[i][j][2]=Return_Score(AI_n);
                        else if(State[p][j]==0 && State[q][j]==0)
                            AI[i][j][2]=Return_Score(AI_n)+1;
    
                        //PE第二方向的得分
                        PE_n=1;
                        for(p=i-1;p>=0;p--)
                            if(State[p][j]==1)
                                PE_n++;
                            else
                                break;
    
                        for(q=i+1;q<=15;q++)
                            if(State[q][j]==1)
                                PE_n++;
                            else
                                break;
    
                        if(State[p][j]==2||p==-1||State[q][j]==2||q==16)
                            PE[i][j][2]=Return_Score(PE_n);
                        else if(State[p][j]==0 && State[q][j]==0)
                            PE[i][j][2]=Return_Score(PE_n)+1;
    
    
    
                        //AI第三方向的得分
                        AI_n=1;
                        for(p=i+1,q=j-1;p<=15&&q>=0;p++,q--)
                            if(State[p][q]==2)
                                AI_n++;
                            else
                                break;
    
                        for(m=i-1,n=j+1;m>=0&&n<=15;m--,n++)
                            if(State[m][n]==2)
                                AI_n++;
                            else
                                break;
    
                        if(State[p][q]==1||p==16||q==-1||State[m][n]==1||m==-1||n==16)
                            AI[i][j][3]=Return_Score(AI_n);
                        else if(State[p][q]==0 && State[m][n]==0)
                            AI[i][j][3]=Return_Score(AI_n)+1;
    
                        //PE第三方向的得分
    
                        PE_n=1;
                        for(p=i+1,q=j-1;p<=15&&q>=0;p++,q--)
                            if(State[p][q]==1)
                                PE_n++;
                            else
                                break;
    
                        for(m=i-1,n=j+1;m>=0&&n<=15;m--,n++)
                            if(State[m][n]==1)
                                PE_n++;
                            else
                                break;
    
                        if(State[p][q]==2||p==16||q==-1||State[m][n]==2||m==-1||n==16)
                            PE[i][j][3]=Return_Score(PE_n);
                        else if(State[p][q]==0 && State[m][n]==0)
                            PE[i][j][3]=Return_Score(PE_n)+1;
    
                        
    
    
                        //AI第四方向的得分
                        AI_n=1;
                        for(p=i-1,q=j-1;p>=0&&q>=0;p--,q--)
                            if(State[p][q]==2)
                                AI_n++;
                            else
                                break;
    
                        for(m=i+1,n=j+1;m<=15&&n<=15;m++,n++)
                            if(State[m][n]==2)
                                AI_n++;
                            else
                                break;
    
                        if(State[p][q]==1||p==-1||q==-1||State[m][n]==1||m==16||n==16)
                            AI[i][j][4]=Return_Score(AI_n);
                        else if(State[p][q]==0 && State[m][n]==0)
                            AI[i][j][4]=Return_Score(AI_n)+1;
    
                        //PE第四方向的得分
                        PE_n=1;
                        for(p=i-1,q=j-1;p>=0&&q>=0;p--,q--)
                            if(State[p][q]==1)
                                PE_n++;
                            else
                                break;
    
                        for(m=i+1,n=j+1;m<=15&&n<=15;m++,n++)
                            if(State[m][n]==1)
                                PE_n++;
                            else
                                break;
    
                        if(State[p][q]==2||p==-1||q==-1||State[m][n]==2||m==16||n==16)
                            PE[i][j][4]=Return_Score(PE_n);
                        else if(State[i][p]==0 && State[i][q]==0)
                            PE[i][j][4]=Return_Score(PE_n)+1;
                        //////////////////////////////////////////////////////////////
                        
                        int temp;
                        for(p=1;p<4;p++)
                            for(q=p+1;q<5;q++)
                                if(AI[i][j][p]<AI[i][j][q])
                                    {
                                        temp=AI[i][j][p];
                                        AI[i][j][p]=AI[i][j][q];
                                        AI[i][j][q]=temp;
                                    }
                                    //AI[i][j][p] ^= AI[i][j][q] ^= AI[i][j][p] ^= AI[i][j][q];
    
                        for(p=1;p<4;p++)
                            for(q=p+1;q<5;q++)
                                if(PE[i][j][p]<PE[i][j][q])
                                {
                                    temp=PE[i][j][p];
                                    PE[i][j][p]=PE[i][j][q];
                                    PE[i][j][q]=temp;
                                }
                                    //PE[i][j][p] ^= PE[i][j][q] ^= PE[i][j][p] ^= PE[i][j][q];
    
    
                        //AI每一点的总分
    
                        if(AI[i][j][1]==1)//单子
                            AI[i][j][0]=0+1;
    
                        if(AI[i][j][1]==2)//死二
                            AI[i][j][0]=10+1;
    
                        if(AI[i][j][1]==3)//活二
                            AI[i][j][0]=20+1;
    
                        if(AI[i][j][1]==4)//死三
                            AI[i][j][0]=30+1;
                        
                        if(AI[i][j][1]==3 && AI[i][j][2]==3)//双活二
                            AI[i][j][0]=40+1;
    
                        if(AI[i][j][1]==5)//活三
                            AI[i][j][0]=50+1;
    
                        if(AI[i][j][1]==6)//死四
                            AI[i][j][0]=60+1;
    
                        if(AI[i][j][1]==5 && AI[i][j][2]==4)//活三死三
                            AI[i][j][0]=70+1;
    
                        if(AI[i][j][1]==5 && AI[i][j][2]==5)//双活三
                            AI[i][j][0]=80+1;
    
                        if(AI[i][j][1]==7)//活四
                            AI[i][j][0]=90+1;
    
                        if(AI[i][j][1]==6 && AI[i][j][2]==6)//双死四
                            AI[i][j][0]=90+1;
    
                        if(AI[i][j][1]==6 && AI[i][j][2]==5)//死四活三
                            AI[i][j][0]=90+1;
    
                        if(AI[i][j][1]==8||AI[i][j][1]==9)//活五或者死五
                            AI[i][j][0]=100+1;
    
                        /*
                        switch(AI[i][j][1])
                        {
                            case 8:AI[i][j][0]=100+1;break;
                            case 7:AI[i][j][0]=90+1;break;
                            case 6:
                            {
                                if(AI[i][j][2]==5||AI[i][j][2]==6) AI[i][j][0]=90+1;
                                else AI[i][j][0]=60+1;
                                break;
                            }
                            case 5:
                                {
                                    if(AI[i][j][2]==5) AI[i][j][0]=80+1;
                                    else if(AI[i][j][2]==4) AI[i][j][0]=70+1;
                                    else AI[i][j][0]=50+1;
                                    break;
                                }
                            case 4:AI[i][j][0]=30+1;break;
                            case 3:
                                {
                                    if(AI[i][j][2]==3) AI[i][j][0]=40+1;
                                    else AI[i][j][0]=20+1;
                                    break;
                                }
                            case 2:AI[i][j][0]=10+1;break;
                            case 1:AI[i][j][0]=0+1;break;
                            default:AI[i][j][0]=0+1;
                        }
                        */
    
                        //PE每一点的总分
    
                        if(PE[i][j][1]==1)//单子
                            PE[i][j][0]=0;
    
                        if(PE[i][j][1]==2)//死二
                            PE[i][j][0]=10;
    
                        if(PE[i][j][1]==3)//活二
                            PE[i][j][0]=20;
    
                        if(PE[i][j][1]==4)//死三
                            PE[i][j][0]=30;
                        
                        if(PE[i][j][1]==3 && PE[i][j][2]==3)//双活二
                            PE[i][j][0]=40;
    
                        if(PE[i][j][1]==5)//活三
                            PE[i][j][0]=50;
    
                        if(PE[i][j][1]==6)//死四
                            PE[i][j][0]=60;
    
                        if(PE[i][j][1]==5 && PE[i][j][2]==4)//活三死三
                            PE[i][j][0]=70;
    
                        if(PE[i][j][1]==5 && PE[i][j][2]==5)//双活三
                            PE[i][j][0]=80;
    
                        if(PE[i][j][1]==7)//活四
                            PE[i][j][0]=90;
    
                        if(PE[i][j][1]==6 && PE[i][j][2]==6)//双死四
                            PE[i][j][0]=90;
    
                        if(PE[i][j][1]==6 && PE[i][j][2]==5)//死四活三
                            PE[i][j][0]=90;
    
                        if(PE[i][j][1]==8||PE[i][j][1]==9)//活五或者死五
                            PE[i][j][0]=100;
    
                        /*
                        switch(PE[i][j][1])
                        {
                            case 8:PE[i][j][0]=100;break;
                            case 7:PE[i][j][0]=90;break;
                            case 6:
                                {
                                    if(PE[i][j][2]==5||PE[i][j][2]==6) PE[i][j][0]=90;
                                    else PE[i][j][0]=60;
                                    break;
                                }
                            case 5:
                                {
                                    if(PE[i][j][2]==4) PE[i][j][0]=70;
                                    else if(PE[i][j][2]==5) PE[i][j][0]=80;
                                    else PE[i][j][0]=50;
                                    break;
                                }
                            case 4:PE[i][j][0]=30;break;
                            case 3:
                                {
                                    if(PE[i][j][2]==3) PE[i][j][0]=40;
                                    else PE[i][j][0]=20;
                                    break;
                                }
                            case 2:PE[i][j][0]=10;break;
                            case 1:PE[i][j][0]=0;break;
                            default:PE[i][j][0]=0;
                        }
                        */
    
                    }
        //以上扫描完毕开始判断落子
        int AI_max=0,AI_x,AI_y;
        int PE_max=0,PE_x,PE_y;
    
        for(int i1=0;i1<16;i1++)
            for(int j1=0;j1<16;j1++)
                if(AI[i1][j1][0]>AI_max)
                    {
                        AI_max=AI[i1][j1][0];
                        AI_x=j1;
                        AI_y=i1;
                    }
    
        for(int i2=0;i2<16;i2++)
            for(int j2=0;j2<16;j2++)
                if(PE[i2][j2][0]>PE_max)
                    {
                        PE_max=PE[i2][j2][0];
                        PE_x=j2;
                        PE_y=i2;
                    }
        printf("%d,%d  ",AI_max,PE_max);
    
        if(AI_max > PE_max)
            {
                //LOCK(screen);
                Apply_Surface(175+AI_x*PIAN_YI-25,75+AI_y*PIAN_YI-25,Chess_Piece_White,screen,NULL);
                SDL_Flip(screen);
                //ULOCK(screen);
                State[AI_y][AI_x]=2;
                if(Judge_Shengfu(AI_y,AI_x)==1)
                    return 1;
    
            }
        else
            {
                //LOCK(screen);
                Apply_Surface(175+PE_x*PIAN_YI-25,75+PE_y*PIAN_YI-25,Chess_Piece_White,screen,NULL);
                SDL_Flip(screen);
                //ULOCK(screen);
                State[PE_y][PE_x]=2;
                if(Judge_Shengfu(PE_y,PE_x)==1)
                    return 1;
            }
        Show_Player(1);
        return 0;
    }
    
    
    //
    int Return_Score(int n)
    {
        switch(n)
            {
                case 1:return 1;
                case 2:return 2;
                case 3:return 4;
                case 4:return 6;
                case 5:return 8;
                default:
                    return 0;
            }
    }
    View Code

     

  • 相关阅读:
    QQ 表情大全
    Linux/Unix同Windows文件传输
    CentOS 5.2安装Oracle 10g
    用户名、密码测试用例
    界面测试用例
    PLSQL导入导出数据库
    JavaScript代码格式规范和注意事项
    Node.js 之发送文件数据编码问题
    Node.js 之IP地址和端口号
    Node.js 之ContentType内容类型
  • 原文地址:https://www.cnblogs.com/pingge/p/3354743.html
Copyright © 2011-2022 走看看