[我的早期C程序源代码]黑白棋

类别:编程语言 点击:0 评论:0 推荐:

/*程序还不够完善*/

#include<io.h>
#include<stdio.h>
#include<dos.h>
#include<string.h>
#include<math.h>
#include<bios.h>
#include<mem.h>
#include<fcntl.h>
#include<stdlib.h>
#include<conio.h>
#include <graphics.h>

#define HH '0'
#define N 6
int Q=0;
char key;
int fine=1;
int A=0,B=0;
char s[N][N],m[N][N];
void *ball;
int maxx;
unsigned int size;
loading()
{ int z=1;

  while(z)
     { if(z>77)break;
       printf("<");
       delay(5000);
       z++;
     }
  delay(2000);
  cleardevice();

}
unsigned char Get_Ascii_Key(void)
{
     if(bioskey(1))
        return(bioskey(0));
     else return(0);
}
int Test_Ascii_Key(int ascii)
{
if(Get_Ascii_Key()==ascii)
return(1);
else
return(0);
}
void tu(){
maxx=getmaxx();
size=imagesize(210,390,450,420);
ball=malloc(size);
setfillstyle(1,10);
bar(160,170,473,253);
setfillstyle(1,9);
bar(163,173,470,250);
setcolor(13);
outtextxy(200,200,"Welcome to BLACK & WHITE chess");
setcolor(14);
outtextxy(250,220,"Developer :Wu siyuan ");
outtextxy(290,240,"2002.8");
outtextxy(210,390,"Press any key to contunue...");
getimage(210,390,450,420,ball);

while(!kbhit()){
   putimage(210,390,ball,XOR_PUT);
   delay(30000);
}

cleardevice();
}
void sou(void){

}
void print()
{ int i=0,j=30;
   setcolor(11);
   outtextxy(35,20,"  0     1     2      3      4     5");
   outtextxy(14,50,"0");
   outtextxy(14,100,"1");
   outtextxy(14,150,"2");
   outtextxy(14,200,"3");
   outtextxy(14,250,"4");
   outtextxy(14,300,"5");
   setbkcolor(0);
   setlinestyle(SOLID_LINE,0,2);
   for(i=0;i<7;i++)
      { setcolor(GREEN);
 line (j,30,j,330);
 j+=50;
      }

      j=30;
   for(i=0;i<7;i++)
       { setcolor(GREEN);
  line(30,j,330,j);
  j+=50;

       }
for(i=0;i<6;i++)
   for(j=0;j<6;j++)
      { if(s[i][j]=='o')

  {
  setcolor(WHITE);
  circle(55+j*50,55+i*50,15);
  }
 else if(s[i][j]=='x')
       { setcolor(RED);
        circle(55+j*50,55+i*50,15); }
      }

}

int cal(char z)
{ int *p=0;
  int num=0,i,j;
  for(i=0;i<N;i++)
    for(j=0;j<N;j++)
      if(s[i][j]==z)num++;
  p=&num;
  return(*p);
}
void save()
{int i,j;
   for(i=0;i<N;i++)
       for(j=0;j<N;j++)
  m[i][j]=s[i][j];
}

void load()
{ int i,j;
   for(i=0;i<N;i++)
     for(j=0;j<N;j++)
       s[i][j]=m[i][j];
}


 snew(int x,int y,char z1,char z2)
{int i,j,a;int can[]={0,0,0,0,0,0,0,0,0},k[N*N];
 if(s[x][y-1]==z1&&(y-1)>=0)
    {
      i=0;
      while(i<y)
       {
   if(s[x][i]==z2){  can[1]=1;break; }
   i++;
       }
    }
 if(s[x][y+1]==z1)
    {
      i=y+2;
      while(i<N)
       {
   if(s[x][i]==z2){ can[2]=1;break;}
   i++;
       }
    }
 if(s[x-1][y]==z1&&(x-1)>=0)
    {
      i=0;
      while(i<x)
       {
    if(s[i][y]==z2){can[3]=1;break;}
    i++;
       }
    }
 if(s[x+1][y]==z1)
    {i=x+2;
     while(i<N)
      {
        if(s[i][y]==z2){can[4]=1;break;}
        i++;
      }
    }
 if(s[x-1][y+1]==z1&&(x-1)>=0)
    {i=2;
     while(y+i<N&&(x-i>=0))
      {if(s[x-i][y+i]==z2){can[5]=1;break;}
       i++;
      }
    }
 if(s[x+1][y-1]==z1&&(y-1)>=0)
   {i=2;
     while(i<N&&(y-i>=0))
     {if(s[x+i][y-i]==z2)
     {can[6]=1;break;}
      i++;
     }
   }
 if(s[x-1][y-1]==z1&&(x-1)*(y-1)>=0)
  {i=2;
   while(x-i>=0&&y-i>=0)
    {if(s[x-i][y-i]==z2)
    {can[7]=1;
      break;}
     i++;
    }
  }
 if(s[x+1][y+1]==z1)
  {i=2;
   while(i+x<N)
    {if(s[x+i][y+i]==z2)
    {can[8]=1;
      break;}
     i++;
    }
  }
  for(i=1,j=0;i<=8;i++)
 if(can[i]==0)j++;

     if(j==8&&Q==1)
 {printf("error!");
   exit(1);  }
     else if(j!=8)s[x][y]=z2;

  if(can[1]==1)
    { i=1;
      while(s[x][y-i]==z1)
       {s[x][y-i]=z2;
 i++;
       }
      can[1]=0;
    }
  if(can[2]==1)
    {i=1;
     while(s[x][y+i]==z1)
       {s[x][y+i]=z2;
 i++;
       }
     can[2]=0;
    }
  if(can[3]==1)
    {i=1;
    while(s[x-i][y]==z1)
      {  s[x-i][y]=z2;
  i++;
      }
     can[3]=0;
    }
  if(can[4]==1)
    {i=1;
     while(s[x+i][y]==z1)
      {s[x+i][y]=z2;
       i++;
      }
     can[4]=0;
    }
  if(can[5]==1)
    {i=1;
    while(s[x-i][y+i]==z1)
      {s[x-i][y+i]=z2;
       i++;
      }
    can[5]=0;
    }
  if(can[6]==1)
    {i=1;
     while(s[x+i][y-i]==z1)
     {s[x+i][y-i]=z2;
      i++;
     }
    can[6]=0;
    }
  if(can[7]==1)
    {i=1;
    while(s[x-i][y-i]==z1)
       {s[x-i][y-i]=z2;
 i++;
       }
      can[7]=0;
    }
  if(can[8]==1)
     {i=1;
      while(s[x+i][y+i]==z1)
       {s[x+i][y+i]=z2;
 i++;
       }
 can[8]=0;
     }

} /* snew */


void one()
{ int a,b;char g='o';

 load();
 setcolor(15);
 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
 printf("\n To exit the Game press the 'q' key.\n");
 print();

printf("Please input x (0 to 5):");
while(1){

           a=Get_Ascii_Key();

           if(a==113||a==81){
             printf("\nQuit?(y or n)");
      while(1)
                {
      if(Test_Ascii_Key(89)||Test_Ascii_Key(121))
                         exit(0);
      else if(Test_Ascii_Key(110)||Test_Ascii_Key(78))
    {printf("\n\tWelcome to return the Game !\n");break; }

                }}


    if(a>=48&&a<=53){printf("\tGood! x has get!\n");
    break;}
        }
 a=a-48;
 printf("Please input y (0 to 5):");

while(1){

    b=Get_Ascii_Key();
    if(b==113||b==81)
          {
             printf("\nQuit?(y or Y)");
      while(1)
                {
      if(Test_Ascii_Key(89)||Test_Ascii_Key(121))
                         exit(0);
      else if(Test_Ascii_Key(110)||Test_Ascii_Key(78))
                         break;

                }}
    if(b>=48&&a<=53){printf("\tGood! y has get!");break;}
         }
       b=b-48;


 Q=1;
 snew(a,b,'x','o');
 Q=0;
 save();


 A=cal(g);
 B=cal('x');
/* printf("\t\t\t\t\t\t\rA=%d,B=%d",A,B);*/

}/* one */
void search()
{
    int i,j,r=0;
    for(i=0;i<N;i++)
      for(j=0;j<N;j++)
 if(s[i][j]==HH)
     r++;
  if(r==0)fine=0;

}
void two()
{ char h='x';int row[N*N],col[N*N];
  int a=1,ii,j,t,t1,k[N*N];
  load();
  for(ii=0;ii<N;ii++)
    for(j=0;j<N;j++)
      if(s[ii][j]==HH)
 {
  row[a]=ii;
  col[a]=j;
  a++;
 }
     /*  space right */

  for(ii=1;ii<a;ii++)
  {
    load();                               /*two*/
    snew(row[ii],col[ii],'o','x');
    k[ii]=cal(h);

  }
    t=k[1];
    ii--;
    t1=ii;
    while(ii)
    {
       if(t<=k[ii])
     {t=k[ii];t1=ii;}
       ii--;
    }

    a=t1;
    A=cal('o');
    B=t;
    load();


    snew(row[a],col[a],'o','x');
    save();
    cleardevice();

    printf("\n\n\t\t\t\t\t    Computer located :%d,%d",row[a],col[a]);

}

main()
{ int i,j;
  int gd=DETECT,gm;
  clrscr();
  initgraph(&gd,&gm,"d:\\tc\\bgi");


    setbkcolor(BLACK);

    tu();

    sou();
    setbkcolor(0);

  for(i=0;i<N;i++)
     for(j=0;j<N;j++)
       s[i][j]='0';
   s[2][2]=s[3][3]='x';
   s[2][3]=s[3][2]='o';
  save();
  loading();
  cleardevice();

while(fine)

  {
    print();
    one();
    two();
    search();
    if(key==113||key==81)
      {
 printf("\nQuit?(y or Y)");
 while(!Test_Ascii_Key(89)&&!Test_Ascii_Key(121));
 break;
      }
  }
  if(A>B)outtextxy(400,200, " You win!");
  else if(A<B)outtextxy(400,200," I win!");
       else outtextxy(400,200," It's a draw!");
   printf("\n       You:%d  I:%d",A,B);
   getch();
}

本文地址:http://com.8s8s.com/it/it22948.htm