五子棋游戏程序设计制作(C语言)

五子棋游戏程序设计制作

大家都玩过手机的小游戏吧。我是编程爱好者,喜欢动脑筋研究一些算法,编程做出小游戏程序,很有趣很有成就感。在学习VB6编程时就编制过钻石棋和伤脑筋十二块那样的游戏,都是标准Windows图形界面的。现在有空闲时就玩手机上编译手机游戏。

 

五子棋游戏程序设计制作(C语言)

 

以下是源码:
//  最简单的 C 语言编程
//  myspringc  v2.7  可编译安卓本机 app 应用
//  此样例可复制黏贴到 myspringc  编译
//  此文档可用 VB,VC  ,  java 改写
//**********************************
//******  五子棋 Gobang   *********
//**********************************
Canvas cs;    //画布
string sBarDes[10];
int nBarId[10];
string sMenu[50];
int nMenu[50];
float src[4];  //ClearDraw (cls) clear screen
int size[2];    //load picture size
string s1,s2,s3,s4,s5,s6;
string s,ss,ss1,ss2,ss3;
 int dx,dy;        //draw X, Y
FileInput filebox;   //file Input box
string fname;  //filename
int i,j,t,k;           //t = times
int pn[225];     // pieces 0=” ” , 1=black , 2=white
int n;                // 225 棋子  编码
int px,py;        // piece x,y
int context;    //canvasProc 触控
int obj;
int id;
int event;
int dn;        //下子计数
int isDo;      //游戏操作控制 1=可下子,0=不可下
int kn;
string report;
int B,W;   //detect win Black White
//**********

main(){
setDisplay(1);
  cs.SetBackground(200,200,200);
  cs.Active();
   sBarDes[0]=”开始游戏”;
   nBarId[0]=100;
   sBarDes[1]=” 悔  棋 “;
   nBarId[1]=101;
   sBarDes[2]=”查看走步”;
   nBarId[2]=102;
   sBarDes[3]=”显示棋板”;
   nBarId[3]=103;
   sBarDes[4]=”退出程序”;
   nBarId[4]=104;
   sBarDes[5]=”  “;
   nBarId[5]=105;
   setToolBarHeight(6);
   setButtonTextSize(13);
   setToolBarBackgroundColor(255,220,220,220);
   setButtonColor(255,0,0,240);
   setButtonTextColor(255,255,255,0);
   setToolBar(100,myToolBarProc,sBarDes,nBarId,6);

   setTitle(“五子棋 (双人对奕) V.1.0.5   “);
//**********
  cs.SetProc (context, mycanvasProc);
     for (i=1;i       isDo=1;         //游戏操作控制
      s6=”黑棋选子”;     //init
      px=320 ;  py=320 ;      //标记
     board ();      //绘出棋板
//draw title****
        ss3=”五子棋 (双人对奕)”;
   cs.SetTextSize (60);
   cs.SetTextStyle (1);
     cs.SetFillMode (1);//0不填色,1填色
    cs.SetColor(255,180,90,40);
         cs.DrawText (ss3,104,504);
      cs.SetColor(255,0,250,0);
         cs.DrawText (ss3,100,500);
    cs.SetFillMode (0);//0不填色,1填色
    cs.SetColor(255,250,250,0);
         cs.DrawText (ss3,100,500);
     cs.Update();
while (){}
  }//main ()

mycanvasProc (int context,int obj,int id,int event,float x,float y){             
     kn=kn+1;     //延时
     if ((kn-kn/2*2)==0){    }else{  return;  }  
       board ();   //redraw board
  //** get chess piece position X Y 
   if (x>40&&x40&&y       px=(int)(x/40)*40;    //取整数
      py=(int)(y/40)*40;
      s=intToString (px);
      s1= “px = “+s;
      s=intToString (py);
      s2= “py = “+s;
   dx=px/40;   dy=py/40;
      s=intToString (dx);
      s3= “dx = “+s;
      s=intToString (dy);
      s4= “dy = “+s;
           n=(dy-1)*15+dx;    //pn number
      s5=”选定落子位 >>   “+intToString (n);
    cs.SetFillMode (0);//0不填色,1填色
  cs.SetColor (255,250,0,0);  //chess flag选子标记
    cs.DrawRect (px-20,py-20,px+20,py+20);  
        }    
       //选子后落子缓冲按钮
    if (isDo==1){  //可下子
    if (x>100&&x900&&y           pn[n]=1;    
          s6=”白棋选子”;           }    //黑已下
    if (x>400&&x900&&y           pn[n]=2;
          s6=”黑棋选子”;           }   //白棋下
            } //isDo==1
   cs.SetFillMode (1);//0不填色,1填色
   cs.SetColor (255,230,230,230);  //clear n
    cs.DrawRect (530,770,600,810);
     // cs.SetColor(255,255,0,0);
    // cs.SetTextSize (24);
     //cs.DrawText (s1,50,700);  //测试
     //cs.DrawText (s2,50,740);
     //cs.DrawText (s3,200,700);
     //cs.DrawText (s4,200,740);
     cs.SetColor(255,0,0,180);
     cs.SetTextSize (30);
     cs.DrawText (s5,330,800);

cs.Update ();
}//mycanvasProc 

 board (){
   // draw board grids 棋盘方格
    cs.SetFillMode (1);//0不填色,1填色
  cs.SetColor (255,250,250,250);  //backcolor
    cs.DrawRect (0,0,720,850);
  cs.SetColor (255,110,110,110);  //pad
    cs.DrawRect (10,10,637,638);
  cs.SetColor (255,220,150,50);  //pad
    cs.DrawRect (8,8,632,632);
  cs.SetColor (255,250,180,100);  //pad
    cs.DrawRect (20,20,620,620);
    cs.SetFillMode (0);//0不填色,1填色
     for (i=1;i         for (j=1;j     cs.SetColor (255,150,150,150);  //grid color
        cs.DrawRect(i*40-1,j*40-1,i*40+39,j*40+39);  
    cs.SetColor (255,250,250,250);  //grid color
        cs.DrawRect(i*40,j*40,i*40+40,j*40+40);  
         }  }
    cs.SetColor (255,0,0,0);  // rectangle line
       cs.DrawRect (21,21,620,620);
       cs.DrawRect (20,20,620,620);

    //draw flag points
    cs.SetFillMode (1);//0不填色,1填色
    cs.SetColor (255,240,240,240);  //pieces
       cs.DrawCircle(160,160,5);
       cs.DrawCircle(480,160,5);
       cs.DrawCircle(160,480,5);
       cs.DrawCircle(480,480,5);
       cs.DrawCircle(320,320,5);

  //draw button 绘制按钮
  cs.SetColor (255,20,20,20);  
    cs.DrawRect (100,900,301,960);
    cs.DrawRect (400,900,601,960);
  cs.SetColor (255,250,0,0);  
      for (i=1;i   cs.SetColor (255,250-i*4,200-i*3,250-i*4);  
      cs.DrawLine (101,900+i,300,900+i);  
      cs.DrawLine (401,900+i,600,900+i);  
          }    
   cs.SetColor (255,250,250,250);  
       cs.DrawCircle (140,930,16);
       cs.DrawCircle (440,930,16);
  cs.SetColor (255,0,0,0);  
       cs.DrawCircle (140,930,15);
  cs.SetColor(255,255,250,0);
      cs.SetTextStyle (1);
     cs.SetTextSize (32);
       cs.DrawText (“黑子下”,175,940);
       cs.DrawText (“白子下”,475,940);
   //print title
    cs.SetTextSize (80);
      cs.SetTextStyle (1);
  cs.SetColor (255,100,100,100);  
        cs.DrawText (“五子棋”,50,770);
        cs.DrawText (“Gobang”,50,870);
   cs.SetColor (255,220,220,220);  
        cs.DrawText (“五子棋”,52,772);
        cs.DrawText (“Gobang”,52,872);

   for (i=1;i   //draw chess pieces 画出已下黑白子
     dx=(i-(i/15*15))*40;
     dy=(i/15)*40+40;
       if (dx==0){ dx=15*40;    dy=dy-40;   }
 if (pn[i]==2){  //white
    cs.SetColor (255,180,80,40);  //piecesW
       cs.DrawCircle(dx+2,dy+2,15);
    cs.SetColor (255,220,220,220);  //pieceW
       cs.DrawCircle(dx,dy,15);
    cs.SetColor (255,250,250,250);  //pieceW
       cs.DrawCircle(dx-5,dy-5,2);   }
 if (pn[i]==1){  //black
    cs.SetColor (255,220,100,40);  //piecesB
       cs.DrawCircle(dx+1,dy+1,15);
    cs.SetColor (255,0,0,0);  //pieceB
       cs.DrawCircle(dx,dy,15);
    cs.SetColor (255,200,200,200);  //pieceB
       cs.DrawCircle(dx-5,dy-5,2);       }
          //   s=intToString(i);  //test…pnumber
          // cs.SetTextSize (16);
           //cs.SetColor (255,0,0,0);  
           //cs.DrawText (s,dx,dy);
              } //draw chess pieces
     cs.SetColor (255,240,0,0);  //刚落子标记
       cs.DrawCircle(px,py,5);
   //提示选子
     cs.SetTextSize (42);
     cs.SetColor(255,255,120,250);
     cs.DrawRect (390,670,600,740);
      cs.SetColor(255,255,250,0);
      cs.DrawText (s6,410,720);
   cs.Update ();
    detect ();
}//board ()

detect (){
   //detect Black chess  判胜 ******
   for (j=1;j        for (i=1;i           B=(i-1)*15+j ;    //pn number
  //********
       if (pn[B]==1){
             for (k=1;k             k=B;
      if ( pn[k+1]==1&&pn[k+2]==1&&pn[k+3]==1&&pn[k+4]==1){  goto showwin ;  }   //右
       if ( pn[k+15]==1&&pn[k+30]==1&&pn[k+45]==1&&pn[k+60]==1){  goto showwin ;  }  //下
      if ( pn[k+16]==1&&pn[k+32]==1&&pn[k+48]==1&&pn[k+64]==1){  goto showwin ;  }  //右斜
       if ( pn[k+14]==1&&pn[k+28]==1&&pn[k+42]==1&&pn[k+56]==1){  goto showwin ;  }  //斜左
                 }  //next k
            }
//*******
      }     }      //next i, j
//detect White chess 判胜 ******
    for (j=1;j        for (i=1;i           W=(i-1)*15+j ;    //pn number
  //********
       if (pn[W]==2){
             for (k=1;k             k=W;
      if ( pn[k+1]==2&&pn[k+2]==2&&pn[k+3]==2&&pn[k+4]==2){  goto showwin ;  }   //右
       if ( pn[k+15]==2&&pn[k+30]==2&&pn[k+45]==2&&pn[k+60]==2){  goto showwin ;  }  //下
      if ( pn[k+16]==2&&pn[k+32]==2&&pn[k+48]==2&&pn[k+64]==2){  goto showwin ;  }  //右斜
       if ( pn[k+14]==2&&pn[k+28]==2&&pn[k+42]==2&&pn[k+56]==2){  goto showwin ;  }  //斜左
                 }  //next k
            }
//*******
      }     }      //next i, j
//*****************************
return;
 //show win flag
showwin:    // show win and put win flag 
     cs.SetTextSize (60);
      if (pn[k]==1) ss=”黑棋胜!!”;   
      if (pn[k]==2) ss=”白棋胜!!”;   
   cs.SetTextStyle (1);
     cs.SetFillMode (1);//0不填色,1填色
    cs.SetColor(255,180,90,40);
         cs.DrawText (ss,204,504);
      cs.SetColor(255,255,0,0);
         cs.DrawText (ss,200,500);
    cs.SetFillMode (0);//0不填色,1填色
    cs.SetColor(255,250,250,0);
         cs.DrawText (ss,200,500);
     isDo=0;      
}//detect ()
 
test (){    //测试,算法检测
    for (i=1;i      n=i;
     //dx=(n-(int)(n/15*15))*40;
     //dy=((int)(n/15))*40+40;
     dx=(n-(n/15*15))*40;
     dy=(n/15)*40+40;
     if (dx==0){ dx=15*40;    dy=dy-40;   }
   print “chess   num = “,n;
   print ”   dx = “,dx;
   print ”   dy = “,dy;       }
}//test ()

myToolBarProc(int nBtn,int nContext){
      if(nBtn==100){//开始游戏
         for (i=1;i              setDisplay (1);
             dn=0;      //计数清零
             isDo=1;
             px=320 ;  py=320 ;
             board ();            } 
      if(nBtn==101){//悔棋,可选子连续清除
             if (isDo==1) pn[n]=0;
             board ();         }
      if(nBtn==102){//查看走步
              setDisplay (0);
               print  report;
               }
      if(nBtn==103){//显示图板
           setDisplay (1);
               }
      if(nBtn==104){//退出程序
         clearOutput();
         cs.ClearDraw (0,src);
         setDisplay (0);
          exit (0);        }
     if(nBtn==105){//test 测试
          //test ();
          setDisplay (0);     //关闭图板,查看测试
          }
}//myToolBar ()

loadpic (){  //可用于装载预设棋盘底图
//use filebox open a file and load a picture
         filebox.SetTitle(“装载图片(*.bmp;*.jpg” />          filebox.SetFilter(“.bmp;.jpg” />          filebox.Show();
         if(filebox.GetCount()==1){
            fname=filebox.GetFileName(0);
            if(getPicSize(fname,size)                return;
            }
             cs.ClearDraw (0,src);
            cs.DrawBitmap(fname,0,2);
             cs.Update ();
           //setDisplay (1);
           // print “Load a picture”;
          //  print fname;
         }
}//loadpic ()

 

//****  End  ********

 

文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览113257 人正在系统学习中

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月8日
下一篇 2022年1月8日

相关推荐