NO 2 (最后一章时有彩蛋务必观看)
void Ending(){
printf("E键退出,R键重来\n");
char ch=getch();
while(ch!='E'&&ch!='e'&&ch!='R'&&ch!='r') ch=getch();
if(ch=='E'||ch=='e') return;
else{main();exit(0);}
}
void result(int t){
setcolor(7+ground);cls();
if(ms==2){
if(t==0) printf("Win!!!");else
printf("Lose...\n");
printf("机器人:%d\n你:%d\n",tot[2],tot[0]);
}else
if(ms==3) if(t==1) printf("White Lose\n");else printf("White win\n");else
if(ms==5) if(t==1) printf("Player 2 win\n");else printf("Player 1 win\n");
Ending();
}
bool check(int x,int y){
if(x<1||x>n||y<2||y>m) return 0;
bool t=1;
if(ms!=3)
for(int i=1;i<=tot[0];i++)
if(x==ty[0][i].x&&y==ty[0][i].y){t=0;break;}
if(ms==1||ms==4||ms==5)
for(int i=1;i<=tot[1];i++)
if(x==ty[1][i].x&&y==ty[1][i].y){t=0;break;}
if(ms==2||ms==3)
for(int i=1;i<=tot[2];i++)
if(x==ty[2][i].x&&y==ty[2][i].y){t=0;break;}
if(ms==3)
for(int i=1;i<=tot[3];i++)
if(x==ty[3][i].x&&y==ty[3][i].y){t=0;break;}
return t;
}
void fnd(int t,int x,int y){
if(check(x,y)) return;
setcolor(7+ground);cls();
if(ms==3) if(t==3) printf("White lose\n");else printf("White win\n");else
if(ms==4||ms==5) if(t==1) printf("Player 1 win\n");else printf("Player 2 win\n");
else{
if(t==2) printf("Win!!!\n");else printf("Lose...\n");
printf("分数:%d\n",score);score=0;
}
Ending();
}
void rand_food(){
if(!vis[foodd[0].x][foodd[0].y]){
setcolor(12+ground);
int x=brand()%(n+1),y=(brand()%m/2)*2;
while(!check(x,y)) x=brand()%(n+1),y=(brand()%m/2)*2;
GOTO(x,y);printf("▇");
if(!vis[x][y]) food++;vis[x][y]=1;foodd[0]=(xcw){x,y};
}
if(!vis[foodd[1].x][foodd[1].y]){
setcolor(12+ground);
int x=brand()%(n+1),y=(brand()%m/2)*2;
while(!check(x,y)) x=brand()%(n+1),y=(brand()%m/2)*2;
GOTO(x,y);printf("▇");
food++;vis[x][y]=1;foodd[1]=(xcw){x,y};
}
}
void draw(){
setcolor(14+ground);
for(int i=0;i<=n+1;i++) GOTO(i,m+2),printf("▇"),GOTO(i,0),printf("▇");
for(int i=1;i<=m/2+1;i++) GOTO(n+1,i*2),printf("▇"),GOTO(0,i*2),printf("▇");
}
int choose(){
printf("0.退出\n");
printf("1.单人模式\n");
printf("2.双人模式\n");
printf("3.人机对抗(谁先吃20个)\n");
printf("4.机机对抗(谁先吃20个)\n");
printf("5.对抗模式\n");
printf("6.对抗模式(谁先吃20个)\n");
printf("快捷键:\n");
printf("P:暂停\n");
printf("E:退出\n");
char ch=getch();
while(ch<'0'||ch>'6') ch=getch();
if(ch=='0') return 48;else
if(ch=='1') ms=0;else
if(ch=='2') ms=1;else
if(ch=='3') ms=2;else
if(ch=='4') ms=3;else
if(ch=='5') ms=4;else
if(ch=='6') ms=5;
}
void stop(){
char ch=getch();
while(ch!='P'&&ch!='p') ch=getch();
}
int mods(){
setcolor(10+ground);
if(ms!=3){
ty[0][1]=(xcw){1,6};ty[0][2]=(xcw){1,4};ty[0][tot[0]=3]=(xcw){1,2};f[0]=1;
for(int i=1;i<=tot[0];i++) GOTO(ty[0][i].x,ty[0][i].y),printf("▇");
}
if(ms==1||ms==4||ms==5){
setcolor(7+ground);
ty[1][1]=(xcw){n,m-6};ty[1][2]=(xcw){n,m-4};ty[1][tot[1]=3]=(xcw){n,m-2};f[1]=2;
for(int i=1;i<=tot[1];i++) GOTO(ty[1][i].x,ty[1][i].y),printf("▇");
}
if(ms==2||ms==3){
setcolor(15+ground);
ty[2][1]=(xcw){n,m-6};ty[2][2]=(xcw){n,m-4};ty[2][tot[2]=3]=(xcw){n,m-2};f[2]=2;
for(int i=1;i<=tot[2];i++) GOTO(ty[2][i].x,ty[2][i].y),printf("▇");
}
if(ms==3){
setcolor(13+ground);
ty[3][1]=(xcw){1,6};ty[3][2]=(xcw){1,4};ty[3][tot[3]=3]=(xcw){1,2};f[3]=1;
for(int i=1;i<=tot[3];i++) GOTO(ty[3][i].x,ty[3][i].y),printf("▇");
}
}
int ads(int x){return x<0?-x:x;}
int get_food(int x,int y,xcw z){return ads(x-z.x)+ads(y-z.y);}
int other(int x){
if(x==0) return 3;else
if(x==1) return 2;else
if(x==2) return 1;else
return 0;
}
void machine(int t,int x,int y){
bool tt=0;int foodid,minn=1e9,newf=f[t];
if(get_food(x,y,foodd[0])<=get_food(x,y,foodd[1])&&vis[foodd[0].x][foodd[0].y]||!vis[foodd[1].x][foodd[1].y]) foodid=0;else foodid=1;
for(int i=0;i<4;i++)
if(f[t]^other(i)){
if(check(x+flg[i][0],y+flg[i][1])){
int now=get_food(x+flg[i][0],y+flg[i][1],foodd[foodid]);
if(now<minn) newf=i,tt=1,minn=now;
if(!tt) newf=i;
}
}
f[t]=newf;
}
int change(int t){
if(t==0) setcolor(10+ground);else
if(t==1) setcolor(7+ground);else
if(t==2) setcolor(15+ground);else
if(t==3) setcolor(13+ground);
int x=ty[t][1].x,y=ty[t][1].y;
if(t==2||t==3) machine(t,x,y);
x+=flg[f[t]][0],y+=flg[f[t]][1];
for(int i=tot[t];i;i--) ty[t][i+1]=ty[t][i];
GOTO(ty[t][tot[t]+1].x,ty[t][tot[t]+1].y);
printf(" ");
if(vis[x][y]){
vis[x][y]=0,score+=(t==0||t==1),food--;
if(++tot[t]>=23&&(ms==3||ms==5)) result(t);
GOTO(ty[t][tot[t]].x,ty[t][tot[t]].y),printf("▇");
}
fnd(t,x,y);
ty[t][1]=(xcw){x,y};
GOTO(ty[t][1].x,ty[t][1].y);
printf("▇");
}
int tanchi(){
bsrand();score=0;
system("title ");
system("mode con cols=80 lines=31");
if(choose()==48)
{
system("cls");
return 0;
}
cls();
printf("设置速度(毫秒/格)(建议100):");
cin>>tim;
MEM(vis,0);food=0;MEM(ty,0);
cls();draw();mods();
while(1){
while(!kbhit()){
Sleep(tim);
rand_food();
if(ms!=3) change(0);
rand_food();
if(ms==1||ms==4||ms==5) change(1);else
if(ms==2) change(2);else
if(ms==3) change(2),change(3);
}
char ch=getch();
bool t=0;
if(ch=='E'||ch=='e') return 0;else
if(ch=='P'||ch=='p') stop();else
if(ms==0||ms==2){
if(ch==-32) ch=getch(),t=1;
if((ch==75&&t||ch=='A'||ch=='a')&&f[0]^1) f[0]=2;else
if((ch==77&&t||ch=='D'||ch=='d')&&f[0]^2) f[0]=1;else
if((ch==80&&t||ch=='S'||ch=='s')&&f[0]^3) f[0]=0;else
if((ch==72&&t||ch=='W'||ch=='w')&&f[0]^0) f[0]=3;
}else
if(ms==1||ms==4||ms==5){
if((ch=='W'||ch=='w')&&f[0]^0) f[0]=3;else
if((ch=='A'||ch=='a')&&f[0]^1) f[0]=2;else
if((ch=='D'||ch=='d')&&f[0]^2) f[0]=1;else
if((ch=='S'||ch=='s')&&f[0]^3) f[0]=0;
if(ch==-32) ch=getch(),t=1;
if(ch==72&&t&&f[1]^0) f[1]=3;else
if(ch==75&&t&&f[1]^1) f[1]=2;else
if(ch==77&&t&&f[1]^2) f[1]=1;else
if(ch==80&&t&&f[1]^3) f[1]=0;
}
}
system("cls");
return 0;
}
自行阅读,老规矩,64赞出注释,所有文章均180赞出教程。