贪吃蛇(部分AI功能)

#include <dos.h>
#include"iostream.h"
#include"graphics.h"
#include"conio.h"
#include"string.h"
#include"math.h" 
#include"time.h"
#include"stdlib.h"
#include"stdio.h" 
#include"conio.h"
#define M_x 1300
#define M_y 700
#define M 10//kuai size
#define N 10
#define G_m 20//snack max numbers
#define G_n 20 
int snack_hx,snack_tx; //snack heard and tail (x,y)
int snack_hy,snack_ty;
int kuai_x,kuai_y;
int F,flge,score,ku,norun,N1=3;
int nu,kong_h,kong_t,can_to;
int   T[G_m][G_n],snack_temp[G_m][G_n],snack_tempf[G_m][G_n];
int t[G_m][G_n];
/*typedef struct users
{
    char name[10];
    char mm[10];
    int score;
    int rank;
}user;
user re(user i)
{return i;}
*/
typedef struct  kuai
{
    int x;
    int y;
    int v; 
    int f;
    int n;
    float color;
    struct kuai *next;
}*kuai_link,kuai_data;
struct kuai snack[G_m][G_n];

int jiemian()
{
 char mm[20]="排名:";
//int Y=700,X=600;   
initgraph(M_x,M_y,1 ); //第三个对窗口格式有影响(- +)
//setlinecolor(255);
HWND hwnd=GetHWnd();
SetWindowText(hwnd,"贪吃蛇1.0版@www.baidu.com/p/y123456789xx98 ");
for(int i=0;i<M_x;i++)
{
    setlinecolor(BLACK);
    line(i,0,M_x,M_y);
}
 setlinecolor(BLUE);
line(1,1,M_x-1,0);
line(1,1,1,M_y-1);
line(M_x-1,M_y-1,M_x-1,1);
line(M_x-1,M_y-1,1,M_y-1);
//setbkmode(TRANSPARENT);
settextcolor(RED);
outtextxy(M_x-180,40,mm);
rectangle(M_x-200,10,M_x-10,M_y-10);
outtextxy(M_x-180,200,"得分");
outtextxy(M_x-180,230,"关卡"); 
for(i=0;i<G_m;i++)
for(int j=0;j<G_n;j++)
{
    setlinecolor(BLUE);
    rectangle(i*M,j*N,M+i*M,N+N*j);
}  
      
return 0;
}

int build()//creact one kuai
 {     int i,j;
 for(i=0;i<G_m;i++)
     for(j=0;j<G_n;j++)
         T[i][j]=0;
     srand(time(0)); 
     do
     {
     i=rand()%G_m;j=rand()%G_n;
     }
    while(snack[i][j].f!=0);
    kuai_x=i;kuai_y=j;
    T[i][j]=G_m*G_n;flge=4;score++;
    if(1) 
    {//setlinecolor(BLUE);
        setfillcolor(GREEN );
    fillrectangle(i*M,N*j,M+i*M,N+N*j);    }//Sleep(1000);
     return flge;
 }

 int initsnack()
  {
      int i,j;
      //N1=3;
      //memset(snack,0,sizeof(snack[G_m][G_n]));//
for(i=0;i<G_m;i++)
for(j=0;j<G_n;j++)
{snack[i][j].f=0;snack[i][j].n=0;}
      snack_hx=G_m/2-3;snack_hy=G_n/2;
      for(i=0;i<N1;i++)
      {
          snack[snack_hx+i][snack_hy].n=1;
          snack[snack_hx+i][snack_hy].f=-1;      
      }
      snack_tx=snack_hx+N1-1;snack_ty=snack_hy;
      return 0;
  }
 int getin()
 {
     int c;
             while(kbhit())
            {    
            fflush(stdin);
            c=getch();   
                switch(c)                                     
                {
                    case 32:{c=getch();c=0;break;}
                    case 72:{if(snack[snack_hx][snack_hy].f==2){;}else snack[snack_hx][snack_hy].f=-2;break;}
                    case 77:{if(snack[snack_hx][snack_hy].f==-1){;}else snack[snack_hx][snack_hy].f=1;break;}
                    case 75:{if(snack[snack_hx][snack_hy].f==1){;}else snack[snack_hx][snack_hy].f=-1;break;}
                    case 80:{if(snack[snack_hx][snack_hy].f==-2){;}else snack[snack_hx][snack_hy].f=2;break;}
                    //default:{snack[snack_hx][snack_hy].f=0;c=0;break;}
                }
            }
     return 0;
 }

 int run1()//heard jin
 {
     int i,j,x,y;
     x=snack[snack_hx][snack_hy].f%2;y=snack[snack_hx][snack_hy].f/2;
     i=snack_hx+x;j=snack_hy+y;
     F=snack[snack_hx][snack_hy].f;
     if(snack[i][j].f==0)//can jin
     {
         snack[snack_hx][snack_hy].n=1;
         snack[i][j].f=F; snack[i][j].n=0;
         snack_hx=i;snack_hy=j;//heard
     }
     else
     {
         flge=11;
     }
     return flge;
 }
 int run2()//tail jin
 {
     int i=0,j=0,m=0,n=0,k=0,h=0;
     m=snack[snack_tx][snack_ty].f%2;n=snack[snack_tx][snack_ty].f/2;
     k=snack_tx+m;h=snack_ty+n;i=snack_tx;j=snack_ty;
     snack_tx=k;snack_ty=h;
     snack[i][j].n=0;
     snack[i][j].f=0;//tail 
     return flge;
 }
 int run3()//tail tui
 {
     int i=0,j=0,m=0,n=0,k=0,h=0;
     m=snack[snack_tx][snack_ty].f%2;n=snack[snack_tx][snack_ty].f/2;
     i=snack_tx;j=snack_ty;
     k=snack_tx-m;h=snack_ty-n;
     if(snack[k][h].f==0&&k>=0&&h>=0&k<G_m&&h<G_n)
     {
         snack_tx=k;snack_ty=h;
          snack[k][h].n=1;
         snack[k][h].f=snack[i][j].f;//tail 
     }
     else
     {
          flge=33;score--;
     }
     return flge;
 }
  
int bianjie()
 {    int i,j;
    if(snack_hx<0||snack_hy<0||snack_hx>=G_m||snack_hy>=G_n)
        flge=55;
    else
        flge=5;
    return flge;
 }
int snack_to()
{
    int i,j;
    for(i=0;i<G_m;i++)
        for(j=0;j<G_n;j++)
        {snack_temp[i][j]=0;
        snack_temp[i][j]=snack[i][j].n;
        snack_tempf[i][j]=0;
        snack_tempf[i][j]=snack[i][j].f;
        }
    return 0;
}

int eat_own()
{
    int i,j,m,n,k,h,number=0;
    k=snack_tx;h=snack_ty;
    do
    {                                                    //从未到头倒推
     m=snack[k][h].f%2;n=snack[k][h].f/2;
     i=k+m;j=h+n;
     k=i;h=j;number++;
    }
     while(snack[i][j].n==1);
     if(snack[i][j].n==0&&i==snack_hx&&j==snack_hy&&number!=score+N1-3 )///当倒推数和本身长度不等时 flge=0
          flge=66;
     else
         flge=6;
     return flge;
}

int eat_kuai()//success return flge=3
{
    if(T[snack_hx][snack_hy]>10)
        flge=7;
    else
        flge=77;
        return flge;
}


int view()
 {
     int i,j,m,n;
      BeginBatchDraw();
     for(i=0;i<G_m;i++)
         for(j=0;j<G_n;j++)
         {
             if(T[i][j]!=0)
             {setfillcolor(YELLOW);
             fillrectangle(i*M,N*j,M+i*M,N+N*j);continue;}
             if(snack[i][j].f==0)
             {setfillcolor(BLACK);
             fillrectangle(i*M,N*j,M+i*M,N+N*j);}
             else
             {setfillcolor(RED);
             fillrectangle(i*M,N*j,M+i*M,N+N*j);}
         }
#if 1
          IMAGE img;     //加载的图片
    loadimage(&img,"D:\\link.jpg",M,N);
    m=snack_hx;n=snack_hy;
    putimage(m*M,n*N,&img);
#else
    setfillcolor(0X255555);//heard
     m=snack_hx;n=snack_hy;
     fillrectangle(m*M,N*n,M+m*M,N+N*n);
#endif
     setfillcolor(GREEN);
     i=snack_tx;j=snack_ty;//-snack[snack_tx][snack_ty].f%2;j=snack_ty-snack[snack_tx][snack_ty].f/2;
     fillrectangle(i*M,N*j,M+i*M,N+N*j);
         EndBatchDraw(); 
         return 0;
 }
int scor()
{
     clearrectangle(M_x-140,200,M_x-11,230);
     char g_score31[10]={0};                                //调试查看
                sprintf(g_score31,"%d",score-2);settextstyle(0,0,g_score31);
                outtextxy(M_x-130,200,g_score31);
     clearrectangle(M_x-140,229,M_x-11,300);
     char g_score32[10]={0};                                //调试查看
                sprintf(g_score32,"%d",ku);settextstyle(0,0,g_score32);
                outtextxy(M_x-130,230,g_score32);
    char g_score33[10]={0};                                //调试查看
                sprintf(g_score33,"%d",flge);settextstyle(0,0,g_score33);
                outtextxy(M_x-130,260,g_score33);
    return 0;
}

 int law(int x1,int y1,int x2,int y2)         //heard x,y return kuai.n
  {
      int i,j,k,h,m,n,x,y;
      nu=1;
      for(i=0;i<G_m;i++)
        for(j=0;j<G_n;j++)
        {
        snack_tempf[i][j]=0;
        snack_tempf[i][j]=snack[i][j].f;
        }
       for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
              t[i][j]=0;
      k=x1;h=y1;
      if(snack[k][h].f!=0)
      { 
          int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
         
        }
 srand(time(0));
     int model=rand()%4;
     if(model==0)
     {
         do
      {
          for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;
      }
     while(nu<=G_m*G_n);
     }
     if(model==1)
         {
         do
      {
          for(j=0;j<G_m;j++)
          for(i=0;i<G_n;i++)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;
      }
     while(nu<=G_m*G_n);
     }
     if(model==2)
         {
         do
      {
          for(j=G_m-1;j>=0;j--)
          for(i=G_n-1;i>=0;i--)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;
      }
     while(nu<=G_m*G_n);
     }
     if(model==3)
         {
         do
      {
          for(i=G_m-1;i>=0;i--)
          for(j=G_n-1;j>=0;j--)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;
      }
     while(nu<=G_m*G_n);
     }
      m=x2;n=y2;
     // f();
      clearrectangle(M_x-290,200,M_x-250,240);//11调试用
     char g_score53[10]={0};
                sprintf(g_score53,"%d",kong_h);settextstyle(0,0,g_score53);
                outtextxy(M_x-275,210,g_score53); 
      clearrectangle(M_x-290,300,M_x-250,360);//11调试用
     char g_score51[10]={0};
                sprintf(g_score51,"%d",kong_t);settextstyle(0,0,g_score51);
                outtextxy(M_x-275,320,g_score51);
     char g_score52[10]={0};
                sprintf(g_score52,"%d",model);settextstyle(0,0,g_score52);
                outtextxy(M_x-275,340,g_score52); 
            
        {
            while(m!=x1||n!=y1)        
      //do
            {
                k=m;h=n;                                                //从未到头倒推
                x=snack_tempf[m][n]%2;y=snack_tempf[m][n]/2;
                i=m-x;j=n-y;
                m=i;n=j;
                
            }
            if(nu!=G_m*G_n)
            snack[x1][y1].f=snack_tempf[k][h];
    //    f();
        }
      return snack[x1][y1].f;
  }

  int heard(int x4,int y4)         //heard x,y return kuai.n
  {
      int i,j,k,h,m,n,x,y;
      nu=1;
      for(i=0;i<G_m;i++)
        for(j=0;j<G_n;j++)
        {
        snack_tempf[i][j]=0;
        snack_tempf[i][j]=snack[i][j].f;
        }
       for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
              t[i][j]=0;
      k=x4;h=y4;
      if(snack[k][h].f!=0)
      { 
          int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
         
        }
      do
      {
          for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;
          kong_h=0;
          if(nu>=G_m*G_n)
          for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
          {
              if(t[i][j]!=0)
                  kong_h++;
          }
      }
     while(nu<=G_m*G_n);
return kong_h;
  }

       int tail(int x3,int y3)         //heard x,y return kuai.n
  {
      int i,j,k,h,m,n,x,y;
      nu=1;
      for(i=0;i<G_m;i++)
        for(j=0;j<G_n;j++)
        {
        snack_tempf[i][j]=0;
        snack_tempf[i][j]=snack[i][j].f;
        }
       for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
              t[i][j]=0;
      k=x3;h=y3;
      if(snack[k][h].f!=0 )
      { 
          int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
         
        }
      do
      {
          for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;

          kong_t=0;
          if(nu>=G_m*G_n)
          for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
          {
              if(t[i][j]!=0)
                  kong_t++;
          }
      }
     while(nu<=G_m*G_n);
    // f();
return kong_t;
  }

int can(int x7,int y7,int x8,int y8)         //heard x,y return kuai.n
  {
      int i,j,k,h,m,n,x,y;
      nu=1;
      snack[x8][y8].f=0;
      for(i=0;i<G_m;i++)
        for(j=0;j<G_n;j++)
        {
        snack_tempf[i][j]=0;
        snack_tempf[i][j]=snack[i][j].f;
        }
       for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
              t[i][j]=0;
      k=x7;h=y7;
      if(snack[k][h].f!=0)
      { 
          int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
         
        }
      do
      {
          for(i=0;i<G_m;i++)
          for(j=0;j<G_n;j++)
          {
               if(t[i][j]==nu)
              {
                  k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;
                  if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}
                  if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}
                  if(snack_tempf[k2][h]==0&&k2<G_m){snack_tempf[k2][h]=1;t[k2][h]=nu;}
                  if(snack_tempf[k][h2]==0&&h2<G_n){snack_tempf[k][h2]=2;t[k][h2]=nu;} 
                  nu--;
              }
          }
          nu++;
      }
     while(t[x8][y8]==0&&nu<=G_m*G_n);
     can_to=1;
     if(nu>=G_m*G_n)
        can_to=0;
    return can_to;
  }
 int main(int argc,char *argv[])  
 {
     int next=1;
     int a=0;
     while(next)
     { 
     score=1;
     jiemian();
     initsnack();build();
     int ai=1;
     //scanf("%d",&ai);
     Sleep(1000);
     while(flge!=0)
     {    
        while(ai)
        {
            if(flge==100)
        {build();norun=0;}
         view();
         scor();
        bianjie();
        //Sleep(1000); 
        if(flge==55)break;
        if(flge==5)
            flge=10;
        eat_own();
        if(flge==66)break;
        if(flge==6)
            flge=10;
        eat_kuai();
        if(flge==7)
        {
            run3();
            flge=100;
        }
        if(flge!=100)
        {
            int kuaif1,kuaif2;
                kuaif1=can(snack_hx,snack_hy,kuai_x,kuai_y);
                kuaif2=can(snack_tx,snack_ty,kuai_x,kuai_y);
                heard(snack_hx,snack_hy);
                tail(snack_tx,snack_ty);
                if(kuaif1==1&&kuaif2==1)
                {
                    law(snack_hx,snack_hy,kuai_x,kuai_y);
                    getin();
                    run2();run1();
                }

                if(kuaif2!=kuaif1||(kuaif1==0&&kuaif2==0))
                {
                    int aa,bb;
                    aa=abs((snack_hx+snack_hy));
                    bb=abs((snack_tx+snack_ty));
                    if(abs((aa-bb))==1);
                    int t=snack[snack_tx][snack_ty].f;
                        snack[snack_tx][snack_ty].f=0;
                        law(snack_hx,snack_hy,snack_tx,snack_ty);
                        snack[snack_tx][snack_ty].f=t;
                        getin();
                        run2();run1();
                }
                

        }
        }


        if(flge==100)
        {build();norun=0;}
        getin();
        run1();
        if(flge==11)break;
        if(flge==1)
            flge=10;
        run2();
        if(flge==2)
            flge=10;
         view();
         scor();
        bianjie();
        if(flge==55)break;
        if(flge==5)
            flge=10;
        eat_own();
        if(flge==66)break;
        if(flge==6)
            flge=10;
        eat_kuai();
        if(flge==7)
        {
            run3();
            flge=100;
        }
        //{law(snack_hx,snack_hy,snack_tx,snack_ty);
        Sleep(500);
        //Sleep(500-10*ku);
     }
    if(a<score)
        a=score;
kbhit();
next=getch();
if(next==27)
next=0;
     }
     score=a;
     score=score-2;
 return score;
 } 

 

posted @ 2017-10-26 15:51  my——master  阅读(137)  评论(0编辑  收藏  举报