签到

06月21日
尚未签到

共有回帖数 0

    告别旧巷

    等级:
    前些天发过这个黑白棋,不过原来不能和电脑下,只能两个人下
     现在可以选择是否加电脑,并且还加了一点注释

     #include"graphics.h"
    #include"stdio.h"
    #define LEN sizeof(struct dian)
    #define NULL 0
    int dian[8][8],result[10][2];   /*dian[][]中存格子的状态,无子,黑子或白子*/
    int N,M,s=0,K=0;
    int dx,dy,key;
    char COMPUTER=0;
    int computer=2;
    struct dian    /*记录所有已经下的子的位置*/
    {
     int x;
     int y;
     struct dian *next;
     };
    struct dian a,*p1,*p,*head;
    void init(void)  /*初始化*/
    {
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"c:\tc");
    cleardevice();
    }
    void jiemian1(void)  /*第一个界面*/
    {
    setlinestyle(0,0,3);
    setcolor(3);
    rectangle(1,1,640,480);
    setcolor(4);
    rectangle(3,3,638,478);
    setcolor(6);
    rectangle(5,5,636,476);
    settextstyle(0,0,6);
    setcolor(7);
    outtextxy(160,160,"HEI BAI");
    settextstyle(0,0,1);
    setcolor(9);
    outtextxy(240,280,"press 1 to play with computer");
    outtextxy(240,320,"press 2 to play with another");
    do{                             /*选择是和人玩还是和电脑下去*/
    COMPUTER=bioskey(0);
     }while(COMPUTER!='1'&&COMPUTER!='2');
    cleardevice();
    }
    void qipan(void)    /*画棋盘*/
    {
    int i;
    setlinestyle(0,0,3);
    setcolor(9);
    for(i=0;i=160;i+=20)
    {
     line(240,i+160,400,i+160);
     line(i+240,160,i+240,320);
    }
    }
    void insert(int x,int y)    /*将下子的位置插入链表*/
    {
    struct dian *p2;
    p2=(struct dian*)malloc(LEN);
    p2-x=x; p2-y=y;
    p1-next=p2;
    p2-next=NULL;
    p1=p2;
    }
    void paint(int x,int y,int t)    /*画棋子*/
    {
    setcolor(t);
    setlinestyle(0,0,1);
    circle(20*x+250,20*y+170,8);
    setfillstyle(SOLID_FILL,t);
    floodfill(20*x+250,20*y+170,t);
    }
    void jiemian2(void)    /*下棋的界面*/
    {
    int i,j;
    for(i=0;i8;i++)
    for(j=0;j8;j++)
    dian[j]=1;
     setcolor(8);
     rectangle(236,156,404,324);
     setfillstyle(SOLID_FILL,6);
     bar(238,158,402,322);
     setcolor(3);
     rectangle(8,8,160,378);
     bar(10,10,158,376);
     settextstyle(0,0,1);
     outtextxy(20,20,"LEFT : change");
     outtextxy(20,50,"RIGHT : change");
     outtextxy(20,80,"ENTER: play");
     outtextxy(20,110,"ESC : leave");
     setcolor(9);
     outtextxy(20,200,"anything wrong:");
     outtextxy(10,230,"zhenlin220@126.com");
     qipan();
     a.x=3;a.y=3;a.next=NULL;dian[3][3]=0;   /*刚开局时候棋盘上的四个棋子*/
     p=head=&a;p1=&a;
     paint(3,3,0);
     insert(3,4);paint(3,4,7); dian[3][4]=7;
     insert(4,3);paint(4,3,7); dian[4][3]=7;
     insert(4,4);paint(4,4,0); dian[4][4]=0;
    }
    void shine(int x,int y,int t)   /*棋子闪烁*/
    {
    static int i=0,j=0;
    if(i==x&&j==y)  return 0;
    do{
    paint(x,y,t);
    delay(50000);
    paint(x,y,6);
    delay(50000);
      }while(!kbhit());
      key=bioskey(0);
      i=x;j=y;
    }
    void scan(int x,int y,int t)   /*查找可以下子的位置或是要变颜色的棋子*/
    {
    int b,c,d,e,i,j,m,n,r;
    K=0;
    if(dian[x][y]==1) r=1;
    else r=t;
    b=x-1; c=x+1; d=y-1; e=y+1;
    if(b0) b=0;      /*查一个子如黑子四周格子的情况,有可能出边界,把边界限制一下*/
    if(c7) c=7;
    if(d0) d=0;
    if(e7) e=7;
    for(i=b;i=c;i++)
     for(j=d;j=e;j++)
     {
      if(dian[j]==t||dian[j]==1) continue;


      dx=i-x;dy=j-y;m=i;n=j;
      while(dian[m][n]==s){m+=dx;n+=dy;}
      if(dian[m][n]==r) continue;
      if(m0||m7||n0||n7) continue;
      result[K][0]=m;result[K][1]=n;
      K++;
     }
    }
    void vary(int x,int y,int t) /*下子,插入链表,查找并改变需要改变颜色的棋子*/
    {
    int i,m,n;
    if(t==0) N+=1;
     else M+=1;
    paint(x,y,t);
    insert(x,y);
    scan(x,y,t);
    dian[x][y]=t;
    for(i=0;iK;i++)
     {
      dx=result[0]-x;
      dy=result[1]-y;
      if(dx==0) dx=0;
       else dx=dx/abs(dx);
      if(dy==0) dy=0;
       else dy=dy/abs(dy);
      m=x; n=y; m+=dx; n+=dy;
      do{
         if(t==0) { N++; M--;}
          else { M++; N--;}
         paint(m,n,t);
         dian[m][n]=t;
         m+=dx; n+=dy;
        }while(dian[m][n]!=t);
     }
    }
    void presskey(int t)   /*按键*/
    {
    int i;
    for(i=0;iK;i++)
     {
      if(computer)   /*如果和电脑下并且这一步不是电脑下*/
      do{
        shine(result[0],result[1],t);  /*闪烁棋子*/
        }while(key!=0x4b00&&key!=0x4d00&&key!=0x1c0d&&key!=0x011b);/*如果没有按键继续闪烁*/
     if(computer==0)
     {
     srand(time(NULL));  /*电脑下棋随机选择下哪个位置*/
     i=rand()%K;
     }
     if(computer==0||key==0x1c0d)
        {
        vary(result[0],result[1],t);break;
        }
     if(key==0x011b) exit(0);   /*如果按ESC离开游戏*/
    }
    }
    void run(int t)
    {
    int i;
    if(t==0) s=7;
    if(t==7) s=0;
    a: p=head;
    do{
      if(dian[p-x][p-y]==s) continue;    
      scan(p-x,p-y,t);    /*查找可以下子的地方并存在result数组中*/
      if(K==0) continue;
      presskey(t);
      if(computer==0||key==0x1c0d) break;
      }while((p=p-next)!=NULL);
      if(key==0x4b00||key==0x4d00) goto a;
    }
    void score(void)   /*显示分数*/
    {
    char str1[4],str2[4];
    setlinestyle(SOLID_LINE,0,THICK_WIDTH);
    setcolor(3);
    rectangle(4,4,636,476);
    setcolor(4);
    rectangle(6,6,634,474);
    setfillstyle(SOLID_FILL,YELLOW);
    rectangle(8,380,632,472);
    setcolor(9);
    rectangle(10,382,630,470);
    bar(12,384,628,468);
    settextstyle(0,0,2);
    setcolor(6);
    outtextxy(280,400,"score");
    sprintf(str1,"%d",N);
    sprintf(str2,"%d",M);
    outtextxy(120,430,"PLAYER1");
    outtextxy(260,430,str1);
    outtextxy(300,430,":");
    outtextxy(330,430,str2);
    if(COMPUTER=='1') outtextxy(390,430,"COMPUTER");
    else outtextxy(390,430,"PLAYER2");
    }
    void winer(void)    /*胜利显示*/
    {
    settextstyle(0,0,4);
    setcolor(9);
    if(NM) outtextxy(200,50,"player1 win!");
    else if(NM) outtextxy(200,50,"player2 win!");
    else outtextxy(200,50,"no winer!");
    }
    main()
    {int i;
    init();   /*初始化*/
    jiemian1();   /*第一个界面*/
    b:
    cleardevice();
    N=2; M=2;   /*初始分数*/
    jiemian2();
    do{
       if(COMPUTER=='1')
       {
       if(s==0) computer=1;
       else  computer=0;
       }
       score();
       run(s);
      }while(N+M!=64||N==0||M==0);
    score();
    winer();
    settextstyle(0,0,1);
    outtextxy(260,120,"press any key to restart");
    getch();
    goto b;
    }
    

    楼主 2016-03-03 17:15 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知