我的五子棋源程式,還不夠厲害,誰能繼續完善 (轉)

a400發表於2007-08-13
我的五子棋源程式,還不夠厲害,誰能繼續完善 (轉)[@more@]

  /* By ZhouDahua.  1999,3,30 */
  /*TC2.0*/
  #include 
  #include 
  #include 
  #include 
  #include 
  #include 
  #define DOWN 80
  #define UP 72
  #define F1 59
  #define LEFT 75
  #define RIGHT 77
  #define Q 16
  #define E 18
  #define X 45
  #define R 19
  #define ESC 1
  #define ENTER 28


  int judge(int);
  int getkey();
  void dec();
  void count(int,int,int);
  void sd();
  int tx,ty;
  int a[16][16]={0}, b[16][16]={0};  /* 全域性變數 */

  void main()
  {
  int x,y,m,n,h=0,s=0,done;  int hx=150,sy=80;
  int ch;
  void *buf_curse;
  unsigned size;


  int graph=DETECT;
  int graphmode;
  registerbgidriver(EGAVGA_driver);
  initgraph(&graphdriver,&graphmode," ");
 begin:sd();
  for(x=0;x<16;x++)
  for(y=0;y<16;y++) a[x][y]=0;
  setfillstyle(1,13);
  bar(0,0,12,12);
  size=imagesize(0,0,12,12);
  buf_curse=malloc(size);
  getimage(0,0,12,12,buf_curse); /* save imag 儲存圖象 */
  putimage(0,0,buf_curse,XOR_PUT);

  tx=1000,ty=1000;
  cleardevice();
  setcolor(WHITE);
  h=0;s=0;hx=154;sy=84;
  setfillstyle(1,CYAN);
  bar(0,0,639,510);
  setfillstyle(1,BLACK);
  bar(157,87,483,413);
  /*  setfillstyle(1,DARKGRAY);
  bar(157,87,483,413);
  */  setfillstyle(1,4);/*BROWN); */
  bar(147,77,473,403);
  setfillstyle(1,LIGHTGRAY);
  bar(151,81,469,399);

  for(x=160;x<=460;x+=20)
  line(x,90,x,390);
  for(y=90;y<=390;y+=20)
  line(160,y,460,y);  /* SETUP FACE */
  putimage(154,84,buf_curse,XOR_PUT); /* xor option*/
  setfillstyle(1,13);
  bar(150,420,470,460);  /* */
  setfillstyle(1,BLACK);
  bar(153,460,475,467);
  bar(470,424,475,467);
  setfillstyle(1,13);
  setcolor(YELLOW);
  outtextxy(160,430,"WZQ program  By ZhouDahua 1999-5-1.");
  outtextxy(160,448,"V1.3A");
  setcolor(10);
  outtextxy(500,100,"R to res");
  outtextxy(500,120,"Esc to quit");
  outtextxy(500,140,"Q or Enter to put");
  outtextxy(500,160,"Enter to put down");

  /*  outtextxy(50,110,"Please:"); */
wait: do{ done=1;
  ch=getkey();
  switch(ch)
  {  case 76 :
  case DOWN:
  {putimage(hx,sy,buf_curse,XOR_PUT);
  if(s==15) s=0;
  else s+=1;
  sy=84+s*20;
  putimage(hx,sy,buf_curse,XOR_PUT);
  break;
  }

  case UP:
  {putimage(hx,sy,buf_curse,XOR_PUT);
  if(s==0) s=15;
  else s-=1;
  sy=84+s*20;
  putimage(hx,sy,buf_curse,XOR_PUT);
  break;
  }

  case LEFT:
  {putimage(hx,sy,buf_curse,XOR_PUT);
  if(h==0) h=15;
  else h-=1;
  hx=154+h*20;
  putimage(hx,sy,buf_curse,XOR_PUT);
  break;
  }

  case RIGHT:
  {putimage(hx,sy,buf_curse,XOR_PUT);
  if(h==15) h=0;
  else h+=1;
  hx=154+h*20;
  putimage(hx,sy,buf_curse,XOR_PUT);
  break;}
  case R:
  {putimage(hx,sy,buf_curse,XOR_PUT);
  setcolor(WHITE);  goto begin;  }
  case X:
  case E:
  case ESC: goto end;

  case Q:
  case ENTER: done=0;break;
  }

 } while(done);
 if(a[h][s]!=0) {  bar(150,420,470,460);setcolor(10);
 outtextxy(160,430,"You do not put it here!");sd(); goto wait;}

  a[h][s]=1;
  for(x=6;x>0;x--)
  { setcolor(13);
  circle(160+h*20,90+s*20,x);}

  if(judge(1)) {  bar(150,420,470,460);setcolor(YELLOW);
  outtextxy(160,430,"Wokao  You Win ! ");
  outtextxy(160,445,"Press R to again"); sd();
  aa: if(getch()!='r') {sd();goto aa;}
  putimage(hx,sy,buf_curse,XOR_PUT);  setcolor(WHITE);
  goto begin;  }

  decide(a);
  if(judge(2)) {  bar(150,420,470,460);setcolor(YELLOW);
  outtextxy(160,430,"Sorry, You Fail ");
  outtextxy(160,445,"Press R to again "); sd();
  bb: if(getch()!='r') {sd();goto bb;}
  putimage(hx,sy,buf_curse,XOR_PUT);  setcolor(WHITE);
  goto begin;  }
  goto wait;


end:
  closegraph(); free(buf_curse);
  for(h=0;h<1;h++) {sd();delay(800);}

}

/***********************************/
void  decide(arr)
  int arr[16][16];
  {
  int i,j,x,y,l,l1,l2,l3,l4;
  int max_1,max_2,n=1,m=2;
  int  x_1=0,y_1=0; int x_2=0,y_2=0;
  for(i=6;i>0;i--)
  {setcolor(15);
  circle(tx,ty,i);}

 see:  for(i=0;i<16;i++)
  for(j=0;j<16;j++) b[i][j]=0;

  for(i=0;i<16;i++)
  for(j=0;j<16;j++)
  if(arr[i][j]==0)
  {  for(x=i,y=j,l1=0;arr[x][y+1]==n&&y<15;y++)l1+=2;
  if(arr[x][y+1]==m) l1--;
  for(x=i,y=j;  arr[x][y-1]==n&&y>0;y--) l1+=2;
  if(arr[x][y-1]==m) l1--;
  count(l1,i,j);
  for(x=i,y=j,l2=0;arr[x+1][y-1]==n&&x<15&&y>0;x++,y--) l2+=2;
  if(arr[x+1][y-1]==m)l2--;
  for(x=i,y=j;  arr[x-1][y+1]==n&&x>0&&y<15;x--,y++) l2+=2;
  if(arr[x-1][y+1]==m)l2--;
  count(l2,i,j);
  for(x=i,y=j,l3=0;arr[x+1][y+1]==n&&x<15&&y<15;x++,y++) l3+=2;
  if(arr[x+1][y+1]==m)l3--;
  for(x=i,y=j;  arr[x-1][y-1]==n&&x>0&&y>0;x--,y--) l3+=2;
  if(arr[x-1][y-1]==m)l3--;
  count(l3,i,j);
  for(x=i,y=j,l4=0;arr[x+1][y]==n&&x<15;x++) l4+=2;
  if(arr[x+1][y]==m)l4--;
  for(x=i,y=j;  arr[x-1][y]==n&&x>0;x--) l4+=2;
  if(arr[x-1][y]==m)l4--;
  count(l4,i,j);

  }

  if(n==1)
  { max_1=b[0][0];
  for(i=0;i<16;i++)
  for(j=0;j<16;j++)
  if(b[i][j]>=max_1)
  {max_1=b[i][j];
  x_1=i;
  y_1=j;
  }
 n++; m--;
 goto see;
  }

  if(n==2)
  { max_2=b[0][0];
 for(i=0;i<16;i++)
 for(j=0;j<16;j++)
 if(b[i][j]>=max_2) {max_2=b[i][j]; x_2=i;y_2=j;} n++;m--;  }

  if(max_2>=90||(max_1<90&&max_2>=50)||max_2>=max_1)
 {x_1=x_2; y_1=y_2;}

  tx=160+x_1*20;ty=90+y_1*20;
  for(i=6;i>0;i--)
  {setcolor(15);if(i<3)setcolor(BLACK);
  circle(tx,ty,i);}

  arr[x_1][y_1]=2;

  }

void count(int l,int i,int j)
  {
  switch(l)
  {
  case -1:
  case 0:  break;
  case 1:  b[i][j]+=2;  break;  /*2*/
  case 2:  b[i][j]+=4;  break;  /*4*/
  case 3:  b[i][j]+=7;  break;
  case 4:  b[i][j]+=10;  break;
  case 5:  b[i][j]+=13;  break;
  case 6:  b[i][j]+=50;  break;
  case 7:
  case 8:  b[i][j]+=95;  break;
  case 9:
  case 10:
  case 11:
  case 13:
  case 14:
  case 15:
  case 16:
  case 17:
  case 18: b[i][j]+=150;
  default: b[i][j]+=0; break;
  }
  }

int judge(int n)
  {
  int i,j,x,y,ju=0,l1,l2,l3,l4;
  for(i=0;i<16;i++)
  for(j=0;j<16;j++)
  if(a[i][j]==n)
  {for(x=i,y=j,l1=1;a[x][y+1]==n&&y<15;y++) l1++;
 if(l1>=5) {ju=1;goto re;}
  for(x=i,y=j,l2=1;a[x+1][y-1]==n&&x<15&&y>0;x++,y--) l2++;
 if(l2>=5) {ju=1;goto re;}
  for(x=i,y=j,l3=1;a[x+1][y+1]==n&&x<15&&y<15;x++,y++) l3++;
 if(l3>=5) {ju=1;goto re;}
  for(x=i,y=j,l4=1;a[x+1][y]==n&&x<15;x++) l4++;
 if(l4>=5) {ju=1;goto re;}
  }
re: if(i!=16&&j!=16) {setcolor(WHITE);circle(160+i*20,90+j*20,10);}
  return(ju);
 }

int getkey()
{union REGS rg;
 rg.h.ah=0;
 int86(0x16,&rg,&rg);
 return rg.h.ah;
}
void sd()
{sound(720);delay(3500);nosound();}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10790690/viewspace-953201/,如需轉載,請註明出處,否則將追究法律責任。

相關文章