java 贪吃蛇ai_贪吃蛇(部分AI功能)

1 #include

2 #include"iostream.h"

3 #include"graphics.h"

4 #include"conio.h"

5 #include"string.h"

6 #include"math.h"

7 #include"time.h"

8 #include"stdlib.h"

9 #include"stdio.h"

10 #include"conio.h"

11 #define M_x 1300

12 #define M_y 700

13 #define M 10//kuai size

14 #define N 10

15 #define G_m 20//snack max numbers

16 #define G_n 20

17 int snack_hx,snack_tx; //snack heard and tail (x,y)

18 intsnack_hy,snack_ty;19 intkuai_x,kuai_y;20 int F,flge,score,ku,norun,N1=3;21 intnu,kong_h,kong_t,can_to;22 intT[G_m][G_n],snack_temp[G_m][G_n],snack_tempf[G_m][G_n];23 intt[G_m][G_n];24 /*typedef struct users25 {26 char name[10];27 char mm[10];28 int score;29 int rank;30 }user;31 user re(user i)32 {return i;}33 */

34 typedef structkuai35 {36 intx;37 inty;38 intv;39 intf;40 intn;41 floatcolor;42 struct kuai *next;43 }*kuai_link,kuai_data;44 structkuai snack[G_m][G_n];45

46 intjiemian()47 {48 char mm[20]="排名:";49 //int Y=700,X=600;

50 initgraph(M_x,M_y,1 ); //第三个对窗口格式有影响(- +)51 //setlinecolor(255);

52 HWND hwnd=GetHWnd();53 SetWindowText(hwnd,"贪吃蛇1.0版@www.baidu.com/p/y123456789xx98");54 for(int i=0;i

65 settextcolor(RED);66 outtextxy(M_x-180,40,mm);67 rectangle(M_x-200,10,M_x-10,M_y-10);68 outtextxy(M_x-180,200,"得分");69 outtextxy(M_x-180,230,"关卡");70 for(i=0;i

77 return 0;78 }79

80 int build()//creact one kuai

81 { inti,j;82 for(i=0;i

87 {88 i=rand()%G_m;j=rand()%G_n;89 }90 while(snack[i][j].f!=0);91 kuai_x=i;kuai_y=j;92 T[i][j]=G_m*G_n;flge=4;score++;93 if(1)94 {//setlinecolor(BLUE);

95 setfillcolor(GREEN );96 fillrectangle(i*M,N*j,M+i*M,N+N*j); }//Sleep(1000);

97 returnflge;98 }99

100 intinitsnack()101 {102 inti,j;103 //N1=3;104 //memset(snack,0,sizeof(snack[G_m][G_n]));//105 for(i=0;i

132 }133 }134 return 0;135 }136

137 int run1()//heard jin

138 {139 inti,j,x,y;140 x=snack[snack_hx][snack_hy].f%2;y=snack[snack_hx][snack_hy].f/2;141 i=snack_hx+x;j=snack_hy+y;142 F=snack[snack_hx][snack_hy].f;143 if(snack[i][j].f==0)//can jin

144 {145 snack[snack_hx][snack_hy].n=1;146 snack[i][j].f=F; snack[i][j].n=0;147 snack_hx=i;snack_hy=j;//heard

148 }149 else

150 {151 flge=11;152 }153 returnflge;154 }155 int run2()//tail jin

156 {157 int i=0,j=0,m=0,n=0,k=0,h=0;158 m=snack[snack_tx][snack_ty].f%2;n=snack[snack_tx][snack_ty].f/2;159 k=snack_tx+m;h=snack_ty+n;i=snack_tx;j=snack_ty;160 snack_tx=k;snack_ty=h;161 snack[i][j].n=0;162 snack[i][j].f=0;//tail

163 returnflge;164 }165 int run3()//tail tui

166 {167 int i=0,j=0,m=0,n=0,k=0,h=0;168 m=snack[snack_tx][snack_ty].f%2;n=snack[snack_tx][snack_ty].f/2;169 i=snack_tx;j=snack_ty;170 k=snack_tx-m;h=snack_ty-n;171 if(snack[k][h].f==0&&k>=0&&h>=0&k

176 }177 else

178 {179 flge=33;score--;180 }181 returnflge;182 }183

184 intbianjie()185 { inti,j;186 if(snack_hx<0||snack_hy<0||snack_hx>=G_m||snack_hy>=G_n)187 flge=55;188 else

189 flge=5;190 returnflge;191 }192 intsnack_to()193 {194 inti,j;195 for(i=0;i

205 inteat_own()206 {207 int i,j,m,n,k,h,number=0;208 k=snack_tx;h=snack_ty;209 do

210 { //从未到头倒推

211 m=snack[k][h].f%2;n=snack[k][h].f/2;212 i=k+m;j=h+n;213 k=i;h=j;number++;214 }215 while(snack[i][j].n==1);216 if(snack[i][j].n==0&&i==snack_hx&&j==snack_hy&&number!=score+N1-3 )///当倒推数和本身长度不等时 flge=0

217 flge=66;218 else

219 flge=6;220 returnflge;221 }222

223 int eat_kuai()//success return flge=3

224 {225 if(T[snack_hx][snack_hy]>10)226 flge=7;227 else

228 flge=77;229 returnflge;230 }231

232

233 intview()234 {235 inti,j,m,n;236 BeginBatchDraw();237 for(i=0;i

247 {setfillcolor(RED);248 fillrectangle(i*M,N*j,M+i*M,N+N*j);}249 }250 #if 1

251 IMAGE img; //加载的图片

252 loadimage(&img,"D:\\link.jpg",M,N);253 m=snack_hx;n=snack_hy;254 putimage(m*M,n*N,&img);255 #else

256 setfillcolor(0X255555);//heard

257 m=snack_hx;n=snack_hy;258 fillrectangle(m*M,N*n,M+m*M,N+N*n);259 #endif

260 setfillcolor(GREEN);261 i=snack_tx;j=snack_ty;//-snack[snack_tx][snack_ty].f%2;j=snack_ty-snack[snack_tx][snack_ty].f/2;

262 fillrectangle(i*M,N*j,M+i*M,N+N*j);263 EndBatchDraw();264 return 0;265 }266 intscor()267 {268 clearrectangle(M_x-140,200,M_x-11,230);269 char g_score31[10]={0}; //调试查看

270 sprintf(g_score31,"%d",score-2);settextstyle(0,0,g_score31);271 outtextxy(M_x-130,200,g_score31);272 clearrectangle(M_x-140,229,M_x-11,300);273 char g_score32[10]={0}; //调试查看

274 sprintf(g_score32,"%d",ku);settextstyle(0,0,g_score32);275 outtextxy(M_x-130,230,g_score32);276 char g_score33[10]={0}; //调试查看

277 sprintf(g_score33,"%d",flge);settextstyle(0,0,g_score33);278 outtextxy(M_x-130,260,g_score33);279 return 0;280 }281

282 int law(int x1,int y1,int x2,int y2) //heard x,y return kuai.n

283 {284 inti,j,k,h,m,n,x,y;285 nu=1;286 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}300 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}301 if(snack_tempf[k2][h]==0&&k2

304 }305 srand(time(0));306 int model=rand()%4;307 if(model==0)308 {309 do

310 {311 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}318 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}319 if(snack_tempf[k2][h]==0&&k2

331 {332 for(j=0;j=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}339 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}340 if(snack_tempf[k2][h]==0&&k2

352 {353 for(j=G_m-1;j>=0;j--)354 for(i=G_n-1;i>=0;i--)355 {356 if(t[i][j]==nu)357 {358 k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;359 if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}360 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}361 if(snack_tempf[k2][h]==0&&k2

373 {374 for(i=G_m-1;i>=0;i--)375 for(j=G_n-1;j>=0;j--)376 {377 if(t[i][j]==nu)378 {379 k=i;h=j;nu++;int k1=k-1,k2=k+1,h1=h-1,h2=h+1;380 if(snack_tempf[k1][h]==0&&k1>=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}381 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}382 if(snack_tempf[k2][h]==0&&k2

393 clearrectangle(M_x-290,200,M_x-250,240);//11调试用

394 char g_score53[10]={0};395 sprintf(g_score53,"%d",kong_h);settextstyle(0,0,g_score53);396 outtextxy(M_x-275,210,g_score53);397 clearrectangle(M_x-290,300,M_x-250,360);//11调试用

398 char g_score51[10]={0};399 sprintf(g_score51,"%d",kong_t);settextstyle(0,0,g_score51);400 outtextxy(M_x-275,320,g_score51);401 char g_score52[10]={0};402 sprintf(g_score52,"%d",model);settextstyle(0,0,g_score52);403 outtextxy(M_x-275,340,g_score52);404

405 {406 while(m!=x1||n!=y1)407 //do

408 {409 k=m;h=n; //从未到头倒推

410 x=snack_tempf[m][n]%2;y=snack_tempf[m][n]/2;411 i=m-x;j=n-y;412 m=i;n=j;413

414 }415 if(nu!=G_m*G_n)416 snack[x1][y1].f=snack_tempf[k][h];417 //f();

418 }419 returnsnack[x1][y1].f;420 }421

422 int heard(int x4,int y4) //heard x,y return kuai.n

423 {424 inti,j,k,h,m,n,x,y;425 nu=1;426 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}440 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}441 if(snack_tempf[k2][h]==0&&k2

444 }445 do

446 {447 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}454 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}455 if(snack_tempf[k2][h]==0&&k2=G_m*G_n)463 for(i=0;i

474 int tail(int x3,int y3) //heard x,y return kuai.n

475 {476 inti,j,k,h,m,n,x,y;477 nu=1;478 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}492 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}493 if(snack_tempf[k2][h]==0&&k2

496 }497 do

498 {499 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}506 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}507 if(snack_tempf[k2][h]==0&&k2

514 kong_t=0;515 if(nu>=G_m*G_n)516 for(i=0;i

525 returnkong_t;526 }527

528 int can(int x7,int y7,int x8,int y8) //heard x,y return kuai.n

529 {530 inti,j,k,h,m,n,x,y;531 nu=1;532 snack[x8][y8].f=0;533 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}547 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}548 if(snack_tempf[k2][h]==0&&k2

551 }552 do

553 {554 for(i=0;i=0){snack_tempf[k1][h]=-1;t[k1][h]=nu;}561 if(snack_tempf[k][h1]==0&&h1>=0){snack_tempf[k][h1]=-2;t[k][h1]=nu;}562 if(snack_tempf[k2][h]==0&&k2=G_m*G_n)572 can_to=0;573 returncan_to;574 }575 int main(int argc,char *argv[])576 {577 int next=1;578 int a=0;579 while(next)580 {581 score=1;582 jiemian();583 initsnack();build();584 int ai=1;585 //scanf("%d",&ai);

586 Sleep(1000);587 while(flge!=0)588 {589 while(ai)590 {591 if(flge==100)592 {build();norun=0;}593 view();594 scor();595 bianjie();596 //Sleep(1000);

597 if(flge==55)break;598 if(flge==5)599 flge=10;600 eat_own();601 if(flge==66)break;602 if(flge==6)603 flge=10;604 eat_kuai();605 if(flge==7)606 {607 run3();608 flge=100;609 }610 if(flge!=100)611 {612 intkuaif1,kuaif2;613 kuaif1=can(snack_hx,snack_hy,kuai_x,kuai_y);614 kuaif2=can(snack_tx,snack_ty,kuai_x,kuai_y);615 heard(snack_hx,snack_hy);616 tail(snack_tx,snack_ty);617 if(kuaif1==1&&kuaif2==1)618 {619 law(snack_hx,snack_hy,kuai_x,kuai_y);620 getin();621 run2();run1();622 }623

624 if(kuaif2!=kuaif1||(kuaif1==0&&kuaif2==0))625 {626 intaa,bb;627 aa=abs((snack_hx+snack_hy));628 bb=abs((snack_tx+snack_ty));629 if(abs((aa-bb))==1);630 int t=snack[snack_tx][snack_ty].f;631 snack[snack_tx][snack_ty].f=0;632 law(snack_hx,snack_hy,snack_tx,snack_ty);633 snack[snack_tx][snack_ty].f=t;634 getin();635 run2();run1();636 }637

638

639 }640 }641

642

643 if(flge==100)644 {build();norun=0;}645 getin();646 run1();647 if(flge==11)break;648 if(flge==1)649 flge=10;650 run2();651 if(flge==2)652 flge=10;653 view();654 scor();655 bianjie();656 if(flge==55)break;657 if(flge==5)658 flge=10;659 eat_own();660 if(flge==66)break;661 if(flge==6)662 flge=10;663 eat_kuai();664 if(flge==7)665 {666 run3();667 flge=100;668 }669 //{law(snack_hx,snack_hy,snack_tx,snack_ty);

670 Sleep(500);671 //Sleep(500-10*ku);

672 }673 if(a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值