三子棋源代码 可运行 c语言

#include<stdio.h>

#include<graphics.h>

#include<bios.h>         /*用到的库函数*/

#define chessnull    '0'

#define chess1      '1'

#define chess2      '2'

int z=0;

char panduan[100][100]={chessnull};    /*定义数组都为“0”*/

main()                /*主函数*/

{int driver=VGA,gmode=VGAHI;

 int i,j,x=20,y=20;

 int m=50,n=50;

initgraph(&driver,&gmode," ");     /*变成图形界面*/

 

qipan(m,n);      /*调用棋盘函数*/

 qizi(x,y);        /*调用棋子函数*/

 yidong(x,y);     /*走棋函数*/

getch();

}

int qipan(int m,int n)      /*制作棋盘函数*/

{int i;

setbkcolor(1);

for(i=0;i<14;i++)

{ setcolor(4);

  line(m,n+30*i,530,n+30*i);

  bioskey(1);

  }

for(i=0;i<17;i++)

{ setcolor(4);

  line(m+30*i,n,m+30*i,440);

  bioskey(1);

  }

}

 

int qizi(int x,int y)       /*制作棋子函数*/

{    setcolor(5);

      circle(x,y,13);

      bioskey(1);

}

 

 

int yidong(int x,int y)     /*走棋的函数*/

{int x0,y0;

int press;

int q;

int f=0;

while (!(x==570 && y==420))     /*使用上下左右键控制*/

{ x0=x;y0=y;

while (bioskey(1)==0);

press=bioskey(0);

q=(press==18432)*1+(press==19200)*2+(press==20480)*3+(press==19712)*4;

switch(q)

{case 1:if(y>50)

do{y=y-30;}while(panduan[(x-50)/30][(y-50)/30]==chess1||panduan[(x-50)/30][(y-50)/30]==chess2);break;

case 2:if(x>50)

do{x=x-30;}while(panduan[(x-50)/30][(y-50)/30]==chess1||panduan[(x-50)/30][(y-50)/30]==chess2);break;

case 3:if(y<420)

do{y=y+30;}while(panduan[(x-50)/30][(y-50)/30]==chess1||panduan[(x-50)/30][(y-50)/30]==chess2);break;

case 4:if(x<510)

do{x=x+30;}while(panduan[(x-50)/30][(y-50)/30]==chess1||panduan[(x-50)/30][(y-50)/30]==chess2);break;

}

setcolor(1);

 

 

circle(x0,y0,13);

setcolor(4);

if(y0>=50&&x0>=80&&x0<=510)

line(x0-30,y0,x0+30,y0);

if(x0>=50&&y0>=80&&y0<420)

line(x0,y0-30,x0,y0+30);

if(f%2==0)

{setcolor(5);

circle(x,y,13);

}

else

{setcolor(8);

 circle(x,y,13);

}

if(press==0x3920)

{

 if(f%2==0)

{setcolor(5);

circle(x,y,13);

panduan[(x-50)/30][(y-50)/30]=chess1;

win(x,y);

x=20;y=20;

f=f+1;

}

else

{setcolor(8);

 circle(x,y,13);

 panduan[(x-50)/30][(y-50)/30]=chess2;

 win(x,y);

 x=20;y=20;

f=f+1;

}

}

if(press==283)     /*按ESC键推出*/

     break;

bioskey(1);

}}

win(int x,int y)    /*赢棋处理函数*/

{       if(panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30][(y-50)/30+1]==chess1&&

   panduan[(x-50)/30][(y-50)/30+2]==chess1)

         {  printf("hong win ");}

if(panduan[(x-50)/30][(y-50)/30-1]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1&&

panduan[(x-50)/30][(y-50)/30+1]==chess1)

{       printf("hong win ");}

if(panduan[(x-50)/30][(y-50)/30-2]==chess1&&

   panduan[(x-50)/30][(y-50)/30-1]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1)

{       printf("hong win ");}     /*判断竖排的是否连成一条线*/

 

if(panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30+1][(y-50)/30]==chess1&&

   panduan[(x-50)/30+2][(y-50)/30]==chess1)

{       printf("hong win ");}

if(panduan[(x-50)/30-1][(y-50)/30]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30+1][(y-50)/30]==chess1)

{       printf("hong win ");}

if(panduan[(x-50)/30-1][(y-50)/30]==chess1&&

   panduan[(x-50)/30-2][(y-50)/30]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1)

{       printf("hong win ");}     /*判断横排的是否连成一条线*/

 

 

if(panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30+1][(y-50)/30+1]==chess1&&

   panduan[(x-50)/30+2][(y-50)/30+2]==chess1)

{printf("hong win ");}

 

if(panduan[(x-50)/30-1][(y-50)/30-1]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30+1][(y-50)/30+1]==chess1)

{       printf("hong win ");}

 

if(panduan[(x-50)/30-2][(y-50)/30-2]==chess1&&

   panduan[(x-50)/30-1][(y-50)/30-1]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1)

{printf("hong win ");}      /*判断左斜排的是否连成一条线*/

 

 

if(panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30-1][(y-50)/30+1]==chess1&&

   panduan[(x-50)/30-2][(y-50)/30+2]==chess1)

{printf("hong win ");}

 

if(panduan[(x-50)/30+1][(y-50)/30-1]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1&&

   panduan[(x-50)/30-1][(y-50)/30+1]==chess1)

{  printf("hong win ");}

 

if(panduan[(x-50)/30+1][(y-50)/30-1]==chess1&&

   panduan[(x-50)/30+2][(y-50)/30-2]==chess1&&

   panduan[(x-50)/30][(y-50)/30]==chess1)

{printf("hong win ");}   /*判断右斜排的是否连成一条线*/

 

 

if(panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30][(y-50)/30+1]==chess2&&

   panduan[(x-50)/30][(y-50)/30+2]==chess2)

{printf("hui win ");}

 

if(panduan[(x-50)/30][(y-50)/30-1]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30][(y-50)/30+1]==chess2)

{printf("hui win ");}

 

if(panduan[(x-50)/30][(y-50)/30-2]==chess2&&

   panduan[(x-50)/30][(y-50)/30-1]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2)

{printf("hui win ");}   /*判断竖排的是否连成一条线*/

 

 

if(panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30+1][(y-50)/30]==chess2&&

   panduan[(x-50)/30+2][(y-50)/30]==chess2)

{printf("hui win ");}

 

if(panduan[(x-50)/30-1][(y-50)/30]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30+1][(y-50)/30]==chess2)

{printf("hui win ");}

 

if(panduan[(x-50)/30-1][(y-50)/30]==chess2&&

   panduan[(x-50)/30-2][(y-50)/30]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2)

{printf("hui win ");}    /*判断横排的是否连成一条线*/

 

 

if(panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30+1][(y-50)/30+1]==chess2&&

   panduan[(x-50)/30+2][(y-50)/30+2]==chess2)

{printf("hui win ");}

 

if(panduan[(x-50)/30-1][(y-50)/30-1]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30+1][(y-50)/30+1]==chess2)

{printf("hui win ");}

 

if(panduan[(x-50)/30-2][(y-50)/30-2]==chess2&&

   panduan[(x-50)/30-1][(y-50)/30-1]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2)

{printf("hui win ");}     /*判断左斜排的是否连成一条线*/

 

 

if(panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30-1][(y-50)/30+1]==chess2&&

   panduan[(x-50)/30-2][(y-50)/30+2]==chess2)

{printf("hui winy");}

if(panduan[(x-50)/30+1][(y-50)/30-1]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2&&

   panduan[(x-50)/30-1][(y-50)/30+1]==chess2)

{printf("hui win ");}     

if(panduan[(x-50)/30+1][(y-50)/30-1]==chess2&&

   panduan[(x-50)/30+2][(y-50)/30-2]==chess2&&

   panduan[(x-50)/30][(y-50)/30]==chess2) {        printf("hui win ");}

/*判断右斜排的是否连成一条线*/

 
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值