用C语言实现图形界面五子棋人机对战

五子棋软件


#1 用C语言实现图形界面五子棋人机对战 作者:有志青年 发表时间:2007-4-18 18:50:37

本文作者照片:

原作者注:
部分关于图形界面的代码是本人改写自网上,在此感谢公开代码的人,也号召大家公开代码,这样更有利于软件的发展。
此五子棋没有判胜算法,读者自已加。
#include
#include
#include
#include
#include "loadbmp.h"
union REGS regs;
int X__max,Y__max,x_max,y_max;
int chessman[15][15]={0};/*数组元素对应棋盘交叉点,用‘0’表示空位、‘1’代表己方的子、‘2’
代表对方的子*/
int playerkind[15][15][4]={0},complayerkind[15][15][4]={0};/*双方各点连子性质0活1冲2死*/
int playervalue[15][15][4]={0},complayervalue[15][15][4]={0};/*双方各点优先值各方向*/
int value[15][15][3]={0};/*最终双方各方向优先值的和第三个下标1为人2为电脑*/
int complayer[15][15][4]={0},player[15][15][4]={0};/*各点所有方向上连子数 0横向1纵向2左斜3右斜*/
int role=0;/*角色 1为人,2为电脑*/
int mode=0;/*对战模式 1为人机,2为双人*/
void Initgr() /*屏幕初始化成图形模式*/
{
    int w,h,grdriver=DETECT,grmode;
    registerbgidriver(EGAVGA_driver);
    initgraph(&grdriver,&grmode,"");/*在双引号中可加你tc放的路径,但要在tc里有EGAVGA.BGI这个来初始图形*/
    if (graphresult())/*若调用不成功,退出*/
    {
        printf("\n 调用失败!\n");
        exit(1);
    }
    X__max=getmaxx();/*求横向象点坐标数*/
    Y__max=getmaxy();/*求纵向象点坐标数*/
    getaspectratio(&w,&h);/*求纵横比*/
    x_max=1000; /*设置屏幕坐标的宽度*/
    y_max=x_max*(float)Y__max*h/((float)X__max*w);
}
int Msinit(int Xlo,int Xhi,int Ylo,int Yhi)/*鼠标初始化*/
{
    int retcode;
    regs.x.ax=0;/*初始化鼠标*/
    int86(0x33,&regs,&regs);
    retcode=regs.x.ax;
    if (retcode==0) return 0;
    regs.x.ax=7;/*设置鼠标X方向的移动范围*/
    regs.x.cx=Xlo;
    regs.x.dx=Xhi;
    int86(0x33,&regs,&regs);
    regs.x.ax=8;/*设置鼠标Y方向的移动范围*/
    regs.x.cx=Ylo;
    regs.x.dx=Yhi;
    int86(0x33,&regs,&regs);
    regs.x.ax=15;/*设置mickey与象素的比,这各会影响鼠标移动速度*/
    regs.x.cx=(int)(x_max/X__max);
    regs.x.dx=(int)(y_max/Y__max);
    int86(0x33,&regs,&regs);
    return retcode;
}
int Msread(int *px,int *py,int *pbuttons)/*读鼠标位置及状态*/
{
    static int x0=320,y0=240,but0=0;
    int xnew,ynew,ch;
    do
    {
        if (kbhit())
        {
            ch=getch();
            if (ch==13)
            {
                *pbuttons=1;
                return -1;
            }
            else return ch; /*返回键盘输入*/
        }
        regs.x.ax=3; /*调用功能3,读鼠标位置及状态*/
        int86(0x33,&regs,&regs);
        xnew=regs.x.cx;/*返回


#2 用C语言实现图形界面五子棋人机对战2 作者:有志青年 发表时间:2007-4-18 18:51:27

void computer()/*根据最终优先值电脑下子*/
{
    int x1,y1,x2,y2,x3,y3,m,n;/*循环变量*/
    long value1=0,value2=0,value3=0;
    for (value1=value[0][0][1],value2=value[0][0][2],value3=value1+value2,m=0;m=9999||value2>=9999)&&value1=value2)
        {
             chessman_2(x1,y1);
             chessman[x1][y1]=2;
             value[x1][y1][1]=value[x1][y1][2]=0;
        }
        else
        {
             chessman_2(x2,y2);
             chessman[x2][y2]=2;
             value[x2][y2][1]=value[x2][y2][2]=0;
        }
    }
    if(value1>=99999&&value2=99999)/*取判胜点*/
    {
        chessman_2(x2,y2);
        chessman[x2][y2]=2;
        value[x2][y2][1]=value[x2][y2][2]=0;
    }
    if(value1>=99999&&value2>=99999)/*双方都能判胜时,电脑攻击*/
    {
        chessman_2(x2,y2);
        chessman[x2][y2]=2;
        value[x2][y2][1]=value[x2][y2][2]=0;
    }
    if(value1

#3 用C语言实现图形界面五子棋人机对战3 作者:有志青年 发表时间:2007-4-18 18:51:52

void information()/*根据棋盘下子情况得出最终优先值*/
{
    int m=0,n=0,z=0,LX=0,RX=0,UY=0,DY=0;/*循环变量*/
    int kind1=0,kind2=0;/*0活2死*/
    int SUM1=0,SUM2=0;/*连子数*/
    /**************把非空白点的相应信息清零**************/
    for(m=0;m<15;m++)
    for(n=0;n<15;n++)
    for(z=0;z<4;z++)
    {
      if(chessman[m][n]!=0)
      {
         player[m][n][z]=complayer[m][n][z]=0;
         playerkind[m][n][z]=complayerkind[m][n][z]=0;
         playervalue[m][n][z]=complayervalue[m][n][z]=0;
      }
    }
    /*******实现各棋子点各方向连子数以及连子的性质信息的统计*******/

#4 用C语言实现图形界面五子棋人机对战4 作者:有志青年 发表时间:2007-4-18 18:54:45

for (n=0;n<15;n++)/*横向*/
    {
        for (m=0;m<15;m++)
        {
            if (chessman[m][n]==0)/*空白点*/
            {
                if (m!=0&&m!=14)/*空白点不在边界*/
                {
                    for (LX=m-1;LX>-1;LX--)/*左侧人方子数*/
                    {
                        if (chessman[LX][n]==0)/*左边终点是空白点,以后叫左活*/
                        {
                            kind1=0;
                            SUM1=m-LX;
                            break;
                        }
                        if (chessman[LX][n]==2)/*左边终点是对方子,以后叫左死*/
                        {
                            kind1=2;
                            SUM1=m-LX;
                            break;
                        }
                        if(chessman[LX][n]==1&&LX==0)/*左边终点是边界*/
                        {
                            kind1=2;
                            SUM1=m-LX+1;
                            break;
                        }
                &nbs


#5 用C语言实现图形界面五子棋人机对战5 作者:有志青年 发表时间:2007-4-18 18:56:34

/******************以下电脑以及其余三个方向的统计算法与以上算法完全类似***********************/
    for (n=0;n<15;n++)/*横向*/
    {
        for (m=0;m<15;m++)
        {
            if (chessman[m][n]==0)/*空白点*/
            {
                if (m!=0&&m!=14)
                {
                    for (LX=m-1;LX>-1;LX--)/*左侧机器子数*/
                    {
                        if (chessman[LX][n]==0)/*左活*/
                        {
                            kind1=0;
                            SUM1=m-LX;
                            break;
                        }
                        if (chessman[LX][n]==1)/*左死*/
                        {
                            kind1=2;
                            SUM1=m-LX;
                            break;
                        }
                        if(chessman[LX][n]==2&&LX==0)
                        {
                            kind1=2;
                            SUM1=m-LX+1;
                            break;
                        }
           &nb

#6 用C语言实现图形界面五子棋人机对战6 作者:有志青年 发表时间:2007-4-18 18:57:09

if (m==14)
                {
                    kind2=2;
                    SUM2=1;
                    for (LX=m-1;LX>-1;LX--)/*左侧机器子数*/
                    {
                        if (chessman[LX][n]==0)/*左活*/
                        {
                            kind1=0;
                            SUM1=m-LX;
                            break;
                        }
                        if (chessman[LX][n]==1)/*左死*/
                        {
                            kind1=2;
                            SUM1=m-LX;
                            break;
                        }
                        if(chessman[LX][n]==2&&LX==0)
                        {
                            kind1=2;
                            SUM1=m-LX+1;
                            break;
                        }
                    }
                }
                complayerkind[m][n][0]=judge(kind1,kind2);
     &nbs

#7 用C语言实现图形界面五子棋人机对战7 作者:有志青年 发表时间:2007-4-18 18:57:36

for (m=0;m<15;m++)/*纵向*/
    {
        for (n=0;n<15;n++)
        {
            if (chessman[m][n]==0)/*空白点*/
            {
                if (n!=0&&n!=14)
                {
                    for (UY=n-1;UY>-1;UY--)/*上方机器子数*/
                    {
                        if (chessman[m][UY]==0)/*上活*/
                        {
                            kind1=0;
                            SUM1=n-UY;
                            break;
                        }
                        if (chessman[m][UY]==1)/*上死*/
                        {
                            kind1=2;
                            SUM1=n-UY;
                            break;
                        }
                        if(chessman[m][UY]==2&&UY==0)
                        {
                            kind1=2;
                            SUM1=n-UY+1;
                            break;
                        }
                    }
           &

#8 用C语言实现图形界面五子棋人机对战8 作者:有志青年 发表时间:2007-4-18 18:58:04

for (m=0;m<15;m++)/*左斜*/
    {
        for (n=0;n<15;n++)
        {
            if (chessman[m][n]==0)
            {
                if (m!=0&&m!=14&&n!=0&&n!=14)
                {
                    for (LX=m-1,UY=n-1;LX>-1&&UY>-1;LX--,UY--)/*左斜上人方子数*/
                    {
                        if (chessman[LX][UY]==0)/*左斜上活*/
                        {
                            kind1=0;
                            SUM1=m-LX;
                            break;
                        }
                        if (chessman[LX][UY]==2)/*左斜上死*/
                        {
                            kind1=2;
                            SUM1=m-LX;
                            break;
                        }
                        if(chessman[LX][UY]==1&&(LX==0||UY==0))
                        {
                            kind1=2;
                            SUM1=m-LX+1;
                            break;
                        }
                    }

#9 用C语言实现图形界面五子棋人机对战9 作者:有志青年 发表时间:2007-4-18 18:58:29

for (m=0;m<15;m++)
    for (n=0;n<15;n++)
    {
        if (chessman[m][n]==0)
        {
            if (m!=0&&m!=14&&n!=0&&n!=14)
            {
                for (LX=m-1,UY=n-1;LX>-1&&UY>-1;LX--,UY--)/*左斜上机器子数*/
                {
                    if (chessman[LX][UY]==0)/**/
                    {
                        kind1=0;
                        SUM1=m-LX;
                        break;
                    }
                    if (chessman[LX][UY]==1)
                    {
                        kind1=2;
                        SUM1=m-LX;
                        break;
                    }
                    if(chessman[LX][UY]==2&&(LX==0||UY==0))
                    {
                        kind1=2;
                        SUM1=m-LX+1;
                        break;
                    }
                }
                for (RX=m+1,DY=n+1;RX<15&&DY<15;RX++,DY++)
                {
                    if (chessman[RX][DY]==0)
                    {
                        kind2=0;
     &nbsp

#10 用C语言实现图形界面五子棋人机对战10 作者:有志青年 发表时间:2007-4-18 19:01:17

for (m=0;m<15;m++)/*右斜*/
    {
        for (n=0;n<15;n++)
        {
            if (chessman[m][n]==0)
            {
                if (m!=0&&m!=14&&n!=0&&n!=14)
                {
                    for (RX=m+1,UY=n-1;RX<15&&UY>-1;RX++,UY--)/*右斜上人方*/
                    {
                        if (chessman[RX][UY]==0)
                        {
                            kind1=0;
                            SUM1=RX-m;
                            break;
                        }
                        if (chessman[RX][UY]==2)
                        {
                            kind1=2;
                            SUM1=RX-m;
                            break;
                        }
                        if(chessman[RX][UY]==1&&(RX==14||UY==0))
                        {
                            kind1=2;
                            SUM1=RX-m+1;
                            break;
                        }
                    }
   &n


#11 用C语言实现图形界面五子棋人机对战11 作者:有志青年 发表时间:2007-4-18 19:02:25

for (m=0;m<15;m++)
    for (n=0;n<15;n++)
    {
        if (chessman[m][n]==0)
        {
            if (n!=0&&n!=14&&m!=0&&m!=14)
            {
                for (RX=m+1,UY=n-1;RX<15&&UY>-1;RX++,UY--)/*右斜上机器*/
                {
                    if (chessman[RX][UY]==0)
                    {
                        kind1=0;
                        SUM1=RX-m;
                        break;
                    }
                    if (chessman[RX][UY]==1)
                    {
                        kind1=2;
                        SUM1=RX-m;
                        break;
                    }
                    if(chessman[RX][UY]==2&&(RX==14||UY==0))
                    {
                        kind1=2;
                        SUM1=RX-m+1;
                        break;
                    }
                }
                for (LX=m-1,DY=n+1;LX>-1&&DY<15;LX--,DY++)
                {
                    if (chessman[LX][DY]==0)
                    {
                        kind2=0;

#12 用C语言实现图形界面五子棋人机对战12 作者:有志青年 发表时间:2007-4-18 19:03:13

/*******整理上文统计信息并录入优先值表******/
    for (m=0;m<15;m++)/*人方*/
    for (n=0;n<15;n++)
    for (z=0;z<4;z++)
    {
        if (chessman[m][n]==0)
        {
            if (playerkind[m][n][z]==0)/*活*/
            {
                switch(player[m][n][z])
                {
                case 1:playervalue[m][n][z]=2;break;/*活1优先值*/
                case 2:playervalue[m][n][z]=30;break;/*活2*/
                case 3:playervalue[m][n][z]=1000;break;/*活3*/
                case 4:playervalue[m][n][z]=9999;break;/*活4*/
                case 5:playervalue[m][n][z]=99999;break;/*活5判胜*/
                }
            }
            if (playerkind[m][n][z]==1)/*冲*/
            {
                switch(player[m][n][z])
                {
                case 1:playervalue[m][n][z]=1;break;/*冲1*/
                case 2:playervalue[m][n][z]=5;break;/*冲2*/
                case 3:playervalue[m][n][z]=50;break;/*冲3*/
                case 4:playervalue[m][n][z]=1500;break;/*冲4*/
                case 5:playervalue[m][n][z]=99999;break;/*冲5判胜*/
                }
            }
            if (playerkind[m][n][z]==2)/*死*/
            {
                switch(player[m][n][z])
                {
                case 1:playervalue[m][n][z]=0;break;/*死1*/
                case 2:playervalue[m][n][z]=0;break;/*死2*/
                case 3:playervalue[m][n][z]=0;break;/*死3*/
                case 4:playervalue[m][n][z]=0;break;/*死4*/
                case 5:playervalue[m][n][z]=99999;break;/*死5判胜*/

#13 用C语言实现图形界面五子棋人机对战13 作者:有志青年 发表时间:2007-4-18 19:04:06

/**********分别求出各个空白点对于人和电脑的四个方向优先值的总和,即最终的优先值***********/
    for (m=0;m<15;m++)
    for (n=0;n<15;n++)
    {
        if (chessman[m][n]==0)
        {
            value[m][n][1]=playervalue[m][n][0]+playervalue[m][n][1]+playervalue[m][n][2]+playervalue[m][n][3];
            value[m][n][2]=complayervalue[m][n][0]+complayervalue[m][n][1]+complayervalue[m][n][2]+complayervalue[m][n][3];
        }
    }
}
main()
{
    int X,Y,x,y,a,b;
    int m,n;
    Initgr();/*初始化图形屏幕*/
    choose();
    interface();
    Msinit(0,(int)x_max,0,(int)y_max);/*初始化鼠标*/
    while (x!=2&&mode==1)
    {
        Msread(&X,&Y,&x);
        Cursor(a,b); /*删除之前的鼠标,因为屏幕输出模式的关系*/
        if (role==1&&x==1)
        {
            a=(int)(X*1.0/x_max*X__max);
            b=(int)((y_max-Y)*1.0/y_max*Y__max);
            m=(a-50)/25;
            n=(b-50)/25;
            if (m<15&&m>-1&&n<15&&n>-1)
            {
                if (chessman[m][n]==0)
                {
                    chessman_1(m,n);
                    chessman[m][n]=1;
                    information();
                    value[m][n][1]=value[m][n][2]=0;
                    role=roleturn(role);
                }
            }
        }
        if (role==2)
        {
            computer();
            information();
            role=roleturn(role);
        }
        Cursor(X,Y);
        a=X;b=Y;
    }
    Cursor(X,Y);/*再调用一次把原来的指针削掉*/
    getch();
}

#14 Re:用C语言实现图形界面五子棋人机对战 作者:百花谷堂主 发表时间:2009-7-9 23:13:25

晕哦······