| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 771 人关注过本帖
标题:五子棋的编程,谁能给改一下啊
只看楼主 加入收藏
老人与颂扬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-7-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
五子棋的编程,谁能给改一下啊
#include<graphics.h>

#define N 20
const int LINE_NUM=19;


/*define keyboard*/
#define up 0x4800
#define down 0x5000
#define left 0x4b00
#define right 0x4d00
#define esc 0x011b
#define enter 0x1c0d
#define A 0x1e61
#define S 0x1f73
#define D 0x2064
#define W 0x1177
#define space 0x3920
#define Y 0x1579
#define n 0x316e


int gamespeed=5000;
int i,j,key;


struct Solution
{
     int x;
     int y;/*x,y define the place of the curSolution*/
     int yes[N][N];/* 0:no chess,1: white chess,2: Red chess*/
     int will;/* 1: white chess is setting,2: Red chess is setting*/
     int ok;/*1:white chess is winning,2: white chess is winning*/
}Solution;

void init(void)       /*graph drive*/
{
     int driver=DETECT,mode=0;
    /* registerbgidriver(EGAVGA_driver);    */
     initgraph(&driver,&mode,"");
}







void drawqp(void)/*draw qi pan*/
{
     setcolor(5);
     for(i=50;i<=(N-1)*20+30;i+=20)
     {
         
          line(50,i,50+(19-1)*20,i);
          line(i,50,i,50+(19-1)*20);
     }
     setcolor(11);
     for(i=1;i<3;i++)
     {
         
          circle(50+(10-1)*20,50+(10-1)*20,i);
          circle(50+(5-1)*20,50+(5-1)*20,i);
          circle(50+(5-1)*20,50+(15-1)*20,i);
          circle(50+(15-1)*20,50+(5-1)*20,i);
          circle(50+(15-1)*20,50+(15-1)*20,i);
         
     }
}

void  titleMessage (void)  /*input message*/
{
     setcolor(15);
     settextstyle(0,0,1);
     outtextxy(50,20,"White Chess: Up Down Left Right Enter    Black Chess: W S A D Space");
}

void place(void)/*画出当前光标的位置*/
{
     line(Solution.x-10,Solution.y-10,Solution.x-7,Solution.y-10);
     line(Solution.x-10,Solution.y-10,Solution.x-10,Solution.y-7);

     line(Solution.x-10,Solution.y+10,Solution.x-10,Solution.y+7);
     line(Solution.x-10,Solution.y+10,Solution.x-7,Solution.y+10);

     line(Solution.x+10,Solution.y-10,Solution.x+10,Solution.y-7);
     line(Solution.x+10,Solution.y-10,Solution.x+7,Solution.y-10);

     line(Solution.x+10,Solution.y+10,Solution.x+7,Solution.y+10);
     line(Solution.x+10,Solution.y+10,Solution.x+10,Solution.y+7);

 }

void judge(void)/*judge the result*/
{
     for(i=1;i<N;i++)
     {
          for(j=1;j<N;j++)
          {
               if(
                   ((j+4)<N&&Solution.yes[i][j]!=0&&
                    Solution.yes[i][j]==Solution.yes[i][j+1]&&
                    Solution.yes[i][j+1]==Solution.yes[i][j+2]&&
                    Solution.yes[i][j+2]==Solution.yes[i][j+3]&&
                    Solution.yes[i][j+3]==Solution.yes[i][j+4])||

                    ((i+4)<N&&Solution.yes[i][j]!=0&&
                    Solution.yes[i][j]==Solution.yes[i+1][j]&&
                    Solution.yes[i+1][j]==Solution.yes[i+2][j]&&
                    Solution.yes[i+2][j]==Solution.yes[i+3][j]&&
                    Solution.yes[i+3][j]==Solution.yes[i+4][j])||

                    ((i+4)<N&&(j+4)<N&&Solution.yes[i][j]!=0&&
                    Solution.yes[i][j]==Solution.yes[i+1][j+1]&&
                    Solution.yes[i+1][j+1]==Solution.yes[i+2][j+2]&&
                    Solution.yes[i+2][j+2]==Solution.yes[i+3][j+3]&&
                    Solution.yes[i+3][j+3]==Solution.yes[i+4][j+4])||

                    ((i+4)<N&&j>4&&Solution.yes[i][j]!=0&&
                    Solution.yes[i][j]==Solution.yes[i+1][j-1]&&
                    Solution.yes[i+1][j-1]==Solution.yes[i+2][j-2]&&
                    Solution.yes[i+2][j-2]==Solution.yes[i+3][j-3]&&
                    Solution.yes[i+3][j-3]==Solution.yes[i+4][j-4]))
               {
                    if(Solution.yes[i][j]==1)
                         Solution.ok=1;
                    else
                         Solution.ok=2;
                         break;
               }
          }
          if(Solution.ok!=0)
               break;
     }
}

void gameover(void)
{
     setcolor(0);
     settextstyle(0,0,1);
     outtextxy(50,20,"White Chess: Up Down Left Right Enter    Black Chess: W S A D Space");
     if(Solution.ok==1)
     {
         outtextxy(488,119,"White");
         
     }
     else
     {
         outtextxy(488,119,"Red");
         
     }
     
     outtextxy(488,189," Chess");
     
     outtextxy(488,259," Win");
     outtextxy(488,329," !");

     
}

/*show the x and y coordinate*/
void ShowXY(void)
{
     char str1[10],str2[10];
     setfillstyle(SOLID_FILL,7);
     bar(50,440,205,470);
     setcolor(12);
     settextstyle(0,0,2);
     sprintf(str1,"X:%d",(Solution.x-30)/20);
     sprintf(str2,"Y:%d",(Solution.y-30)/20);
     outtextxy(60,450,str1);
     outtextxy(135,450,str2);
}

void gameplay(void)
{
     for(i=1;i<N;i++)/*initialize chessboard*/
          for(j=1;j<N;j++)
               Solution.yes[i][j]=0;

     Solution.will=1; /* white chess first */

     Solution.ok=0;/* result is not decided*/

     Solution.x=50+(10-1)*20;/*230*/

     Solution.y=50+(10-1)*20;/*initialize cursor ,the position is in the center of the chessboard*/

     setcolor(15);
     place();

     while(1)
     {
          ShowXY();
          key=bioskey(0);
          if(key==esc)
               break;

          /*white chess key is moving*/
      else if(key==up&&Solution.y>50&&Solution.will==1)/*50 the top*/
          {
               setcolor(0);
               place();
               Solution.y-=20;
          }
      else if(key==down&&Solution.y<(N-1)*20+30&&Solution.will==1)/*230 the bottow  */
          {
               setcolor(0);
               place();
               Solution.y+=20;
          }
          else if(key==left&&Solution.x>50&&Solution.will==1)
          {
               setcolor(0);
               place();
               Solution.x-=20;
          }
          else if(key==right&&Solution.x<(N-1)*20+30&&Solution.will==1)
          {
               setcolor(0);
               place();
               Solution.x+=20;
          }

          /*red chess is moving*/
          else if(key==W&&Solution.y>50&&Solution.will==2)
          {
               setcolor(0);
               place();
               Solution.y-=20;
          }
          else if(key==S&&Solution.y<(N-1)*20+30&&Solution.will==2)
          {
               setcolor(0);
               place();
               Solution.y+=20;
          }
          else if(key==A&&Solution.x>50&&Solution.will==2)
          {
               setcolor(0);
               place();
               Solution.x-=20;
          }
          else if(key==D&&Solution.x<(N-1)*20+30&&Solution.will==2)
          {
               setcolor(0);
               place();
               Solution.x+=20;
          }
          else if(key==space&&Solution.yes[(Solution.x-30)/20][(Solution.y-30)/20]==0&&Solution.will==2)
          {
               setcolor(13);
               for(i=1;i<=9;i++)
               {
            circle(Solution.x,Solution.y,i);/* i is the radius*/
            delay(10000);/* delay time 1 s  */
               }
               Solution.yes[(Solution.x-30)/20][(Solution.y-30)/20]=2;
               Solution.will=1;
               judge();/* judge result*/
               if(Solution.ok!=0)
               {
                    gameover();
                    break;
               }
          }
          else if(key==enter&&Solution.yes[(Solution.x-30)/20][(Solution.y-30)/20]==0&&Solution.will==1)
          {
               setcolor(15);
               for(i=1;i<=9;i++)
               {
            circle(Solution.x,Solution.y,i);/* i is the radius, draw the concentric circles */
                    delay(10000);
               }
               Solution.yes[(Solution.x-30)/20][(Solution.y-30)/20]=1;
               Solution.will=2;
               judge();
               if(Solution.ok!=0)
               {
                    gameover();
                    break;
               }
          }
          else
               continue;
          if(Solution.ok!=0)
               break;
          if(Solution.will==1)
               setcolor(15);
          else
               setcolor(13);
          place();
     }/*endwhile(1)*/
}



main()
{

     while(1)
     {
          init();
          cleardevice();
          titleMessage();
          drawqp();
          setfillstyle(SOLID_FILL,7);


          gameplay();
          setcolor(15);
          settextstyle(0,0,2);
          outtextxy(230,450,"Continue? (Y/N)");
          while(1)
          {
               key=bioskey(0);
               if(key==Y||key==n||key==esc)
                    break;
          }
          if(key==n||key==esc)
               break;
     }
     closegraph();
}

搜索更多相关主题的帖子: 五子棋 
2010-07-19 16:20
老人与颂扬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-7-19
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

这是错误:

d:\fivechess.c(1) : fatal error C1083: Cannot open include file: 'graphics.h': No such file or directory
哪位高手能给改一下啊,小弟万分感谢!
2010-07-19 16:29
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:7 
这个是'graphics.h'这个文件没有,你可以到网上下载一个

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-07-19 16:38
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
哦,这个五子棋好像是我发的,那么也顺便发个下载网址吧:http://down.

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-07-19 16:40
梦会圆
Rank: 1
来 自:河南
等 级:新手上路
帖 子:5
专家分:8
注 册:2010-7-11
收藏
得分:7 
/*turboc2.0下编译通过*/
#include
#include
#include
#include
#define N 15
#define B 7
#define STOP -10000
#define OK 1
#define NO 0
#define UP 328
#define DOWN 336
#define LEFT 331
#define RIGHT 333

/*定义了两个数,n为棋盘的大小。b为背景颜色的数值*/
int a[N+1][N+1];
int zx,zy;
int write=1,biaoji=0;

struct zn{
long sum;
int y;
int x;
}w[N+1][N+1],max,max1;


void cbar(int i,int x,int y,int r);
void map(int a[][]);
int getkey();
int key();
void zuobiao(int x,int y,int i);
int tu(int a[][],int write);
int wtu(int a[][],int write);
int zhineng(int a[][]);
int zh5(int y,int x,int a[][]);
long zzh5(int b[][],int i);

main()
{
int i,j;
int gdriver=DETECT;
int gmode;
initgraph(&gdriver,&gmode,);
zx=(N+1)/2;
zy=(N+1)/2;
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
a[i][j]=0;
map(a);
i=1;
while(i)
{
int k,n;

k=wtu(a,write);
if(k==STOP) goto end;
map(a);

n=zhineng(a);
if(n==STOP) goto end;
map(a);

}
end:
;

}

/* 实现对局的程序 ,计算全部N*N个格中,最应该填的格子 */
int zhineng(int a[N+1][N+1])
{
int i,j;
int k;
max.sum=-1;


for(i=0;i<=N;i++)
for(j=0;j<+N;j++)
{
w[i][j].sum=0;
w[i][j].x=i;
w[i][j].y=j;
}

for(i=1;i<=N-4;i++)
for(j=1;j<=N-4;j++)
{
k=zh5(i,j,a);
if(k==STOP) return (STOP);

}

for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
{
if(max.sum {
max.sum=w[i][j].sum;
max.y=i;
max.x=j;
}
else if(max.sum==w[i][j].sum)
{
if(((max.y-zy)*(max.y-zy)+(max.x-zx)*(max.x-zx))>((i-zy)*(i-zy)+(j-zx)*(j-zx)))
max.sum=w[i][j].sum;
max.y=i;
max.x=j;
}
}
if(a[max.y][max.x]==0)
{
a[max.y][max.x]=-1;
zy=max.y;
zx=max.x;
}

}


/* 转换成5*5的数组,计算出在二十五个格子中,最应该填的格 */
int zh5(int y,int x,int a[N+1][N+1])
{
int i,j;
int b[6][6];
long c[13];
long d[6][6];
long temp;
for(i=y;i<=y+4;i++)
for(j=x;j<=x+4;j++)
b[i+1-y][j+1-x]=a[i][j];

c[1]=b[1][1]+b[1][2]+b[1][3]+b[1][4]+b[1][5];
c[2]=b[2][1]+b[2][2]+b[2][3]+b[2][4]+b[2][5];
c[3]=b[3][1]+b[3][2]+b[3][3]+b[3][4]+b[3][5];
c[4]=b[4][1]+b[4][2]+b[4][3]+b[4][4]+b[4][5];
c[5]=b[5][1]+b[5][2]+b[5][3]+b[5][4]+b[5][5];

c[6]=b[1][1]+b[2][1]+b[3][1]+b[4][1]+b[5][1];
c[7]=b[1][2]+b[2][2]+b[3][2]+b[4][2]+b[5][2];
c[8]=b[1][3]+b[2][3]+b[3][3]+b[4][3]+b[5][3];
c[9]=b[1][4]+b[2][4]+b[3][4]+b[4][4]+b[5][4];
c[10]=b[1][5]+b[2][5]+b[3][5]+b[4][5]+b[5][5];

c[11]=b[1][1]+b[2][2]+b[3][3]+b[4][4]+b[5][5];
c[12]=b[1][5]+b[2][4]+b[3][3]+b[4][2]+b[5][1];


for(i=1;i<=12;i++)
{
switch(c[i])
{
case 5:biaoji=1;return(STOP);
case -5:biaoji=-1;return(STOP);

case -4:c[i]=100000;break;
case 4:c[i]=100000;break;
case -3:c[i]=150;break;
case 3:c[i]=150;break;
case -2:c[i]=120;break;
case 2:c[i]=100;break;
case -1:c[i]=1;break;
case 1:c[i]=1;break;

default: c[i]=0;
}
}

for(i=1;i<=12;i++)
{
if(c[i]==150)
c[i]+=zzh5(b,i);
}
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
d[i][j]=0;

for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
{
if(i==j) d[i][j]+=c[11];
if((i+j)==6) d[i][j]+=c[12];
d[i][j]+=c[i]+c[j+5];
}

for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
{
if(b[i][j]!=0)
d[i][j]=-2;
}
max1.sum=-1;
max1.y=0;
max1.x=0;
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
{
if(max1.sum {
max1.sum=d[i][j];
max1.y=i;
max1.x=j;
w[i+y-1][j+x-1].sum+=max1.sum;
}
else if(max1.sum==d[i][j])
{
if(((i+y-1-zy)*(i+y-1-zy)+(j+x-1-zx)*(j+x-1-zx))>((max1.y+y-1-zy)*(max1.y+y-1-zy)+(max1.x+x-1-zx)*(max1.x+x-1-zx)))
{
max1.sum=d[i][j];
max1.y=i;
max1.x=j;
}
}
}

}

long zzh5(int b[6][6],int n)
{
int i,j,k,l,m;
switch(n)
{
case 1:i=b[1][1];j=b[1][2];k=b[1][3];l=b[1][4];m=b[1][5];break;
case 2:i=b[2][1];j=b[2][2];k=b[2][3];l=b[2][4];m=b[2][5];break;
case 3:i=b[3][1];j=b[3][2];k=b[3][3];l=b[3][4];m=b[3][5];break;
case 4:i=b[4][1];j=b[4][2];k=b[4][3];l=b[4][4];m=b[4][5];break;
case 5:i=b[5][1];j=b[5][2];k=b[5][3];l=b[5][4];m=b[5][5];break;
case 6:i=b[1][1];j=b[2][1];k=b[3][1];l=b[4][1];m=b[5][1];break;
case 7:i=b[1][2];j=b[2][2];k=b[3][2];l=b[4][2];m=b[5][2];break;
case 8:i=b[1][3];j=b[2][3];k=b[3][3];l=b[4][3];m=b[5][3];break;
case 9:i=b[1][4];j=b[2][4];k=b[3][4];l=b[4][4];m=b[5][4];break;
case 10:i=b[1][5];j=b[2][5];k=b[3][5];l=b[4][5];m=b[5][5];break;
case 11:i=b[1][1];j=b[2][2];k=b[3][3];l=b[4][4];m=b[5][5];break;
case 12:i=b[1][5];j=b[2][4];k=b[3][3];l=b[4][2];m=b[5][1];break;

}
if((i==0&&j==1&&k==1&&l==1&&m==0))
return (900);
if((i==0&&j==-1&&k==-1&&l==-1&&m==0))
return(1000);
if((i==0&&j==0&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==0&&m==0))
return(20);
if((i==0&&j==0&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==0&&m==0))
return(20);

if((i==-1&&j==1&&k==1&&l==1&&m==1)||(i==1&&j==-1&&k==1&&l==1&&m==1)||(i==1&&j==1&&k==-1&&l==1&&m==1)||(i==1&&j==1&&k==1&&l==-1&&m==1)||(i==1&&j==1&&k==1&&l==1&&m==-1))
return(-60);

if((i==1&&j==-1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==1&&k==-1&&l==-1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==1&&m==-1)||(i==-1&&j==-1&&k==-1&&l==-1&&m==1))
return(-60);

}
/* 循环执行坐标的选择,直到按回车,空格或ESC键 */
int wtu(int a[N+1][N+1],int write)
{
int i=1;
map(a);
zuobiao(zx,zy,1);
while(i)
{
int k;
k=tu(a,write);
if(k==OK) i=0;
if(k==STOP) return (STOP);
}
}


/*从键盘获得输入的值*/
int getkey()
{
int key,lo,hi;
key=bioskey(0);
lo=key&0x00ff;
hi=(key&0xff00)>>8;
return((lo==0) ? hi+256:lo);
}

/*对获得的值进行判断*/
/*对应的码值分别如下*/
/* 上:328 下:336 左: 331 右: 333 */
/* 回车:13 ESC键: 27 */

int key()
{
int k;
k=getkey();
switch(k)
{
case 27: return (STOP);
case 13:
case ' ': return (OK);
case 328: return (UP);
case 336: return (DOWN);
case 331: return (LEFT);
case 333: return (RIGHT);
default: return (NO);
}

}

/*用来显示坐标的位置*/
void zuobiao(int x,int y,int i)
{
int r;
if(i!=0)
{
setcolor(GREEN);

for(r=1;r<=5;r++)
circle(75+25*x,25+25*y,r);


}
else
{
if(a[zy][zx]==1)
{
setcolor(8);
for(r=1;r<=5;r++)
circle(75+25*x,25+25*y,r);
}
else if(a[zy][zx]==-1)
{
setcolor(WHITE);
for(r=1;r<=5;r++)
circle(75+25*x,25+25*y,r);
}
else
{
setcolor(B);
for(r=1;r<=5;r++)
circle(75+25*x,25+25*y,r);
setcolor(RED); line(75+25*zx-5,25+25*zy,75+25*x+5,25+25*zy);
line(75+25*zx,25+25*zy-5,75+25*zx,25+25*zy+5);
}
}
}

/*从键盘获得的值进行判断,反映在显示的图上*/

int tu(int a[N+1][N+1],int write)
{
int k;
re:
k=key();
if(k==OK)
{
if(a[zy][zx]==0)
{
a[zy][zx]=write;
}
else
goto re;
}

if(k==STOP) return(STOP);

if(k==NO) goto re;

if(k==UP)
{
int i,j;
if(zy==1) j=zy;
else j=zy-1;
zuobiao(zx,zy,0);
zuobiao(zx,j,1);
zy=j;
goto re;
}
if(k==DOWN)
{
int i,j;
if(zy==N) j=zy;
else j=zy+1;
zuobiao(zx,zy,0);
zuobiao(zx,j,1);
zy=j;
goto re;
}
if(k==LEFT)
{
int i,j;
if(zx==1) i=zx;
else i=zx-1;
zuobiao(zx,zy,0);
zuobiao(i,zy,1);
zx=i;
goto re;
}
if(k==RIGHT)
{
int i,j;
if(zx==N) i=zx;
else i=zx+1;
zuobiao(zx,zy,0);
zuobiao(i,zy,1);
zx=i;
goto re;
}

}







/* 根据数组中(存储棋子位置)各位置的数,画实心圆(画出棋子)*/
void cbar(int i,int x,int y,int r)
{
if(i!=0)
{
if(i==1)
setcolor(8);
else if(i==-1)
setcolor(WHITE);

for(i=1;i<=r;i++)
{
circle(x,y,i);
}
}
}

/*画出棋盘,和各个棋子*/
void map(int a[N+1][N+1])
{
int i,j;
cleardevice();
setbkcolor(B);
setcolor(RED);
for(i=0;i {

line(100,50+25*i,75+N*25,50+25*i);
line(100+25*i,50,100+25*i,25+N*25);
}
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
cbar(a[i][j],75+25*j,25+25*i,10);
}

 
 
你看看行不
2010-07-19 18:04
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
你的前四个include是什么,什么也没包含啊

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-07-19 18:05
老人与颂扬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-7-19
收藏
得分:0 
四个include里什么都没包含,怎么可能运行的了啊········
2010-07-20 16:05
vikingguo
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-21
收藏
得分:0 
四个include里什么都没包含,怎么可能运行的了啊········
2010-07-21 14:44
老人与颂扬
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-7-19
收藏
得分:0 
vikingguo
你是深山里的黄瓜,欠拍·········哈哈
2010-07-21 15:17
vikingguo
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-21
收藏
得分:0 
lz  是公公    太贱
2010-07-21 15:19
快速回复:五子棋的编程,谁能给改一下啊
数据加载中...
 
   



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

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