跪求神犇改代码
  • 板块灌水区
  • 楼主_c_m_
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/7/11 08:15
  • 上次更新2024/7/11 09:55:26
查看原帖
跪求神犇改代码
993248
_c_m_楼主2024/7/11 08:15

自己做的贪吃蛇,可是加入了尾巴(函数名叫wb)就炸了,思路是头(0)吃了苹果(.)会克隆自己(o)显示,然后删掉所有克隆再克隆一遍(吃了两个苹果,头克隆的克隆体,克隆自己),将头的移动步骤记录下来,克隆体1重复头上一个移动,克隆体2重复头上上个移动,bug是他不删除,卡局无限克隆,有时(克隆体)突然消失,吃了苹果克隆体错位。

跪求神犇救救蒟蒻吧


#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
char a[10][20],b[201],c;
int x=9,y=4,n,j,f=0,js=0,d=0;
int gz(){
	int gx=rand() %20;
	int gy=rand() %10;
	a[gy][gx]='.';
	js++;
	return 0;
}
int wb(int i,int d){
	int sx=x;
	int sy=y;
	if(b[i]=='w'&&y!=0)sx--;
	if(b[i]=='s'&&y!=9)sx++;
	if(b[i]=='a'&&x!=0)sy--;
	if(b[i]=='d'&&x!=19)sy--;
	a[sy][sx]='o';
	if(d>1)wb(i++,d--);
	if(d==0)a[sx][sy]=';
	else return 0;
}
int main(){
	
	while(n==0){
		a[y][x]='0';
		system("cls");
		cout<<"┌──────────┐"<<endl;
		for(int i=0;i<10;i++){
			cout<<"│";
			for(int j=0;j<20;j++){
				cout<<a[i][j];
			}
			cout<<"│"<<endl;
		}
		cout<<"└──────────┘"<<endl;
		cout<<"分数 :"<<f<<endl; 
		a[y][x]=c;
		b[0]= getch();
		for(int i=1;i<=d;i++){
			b[i]=b[i-1];
			
		}
		wb(1,d);
		if(b[0]=='w'&&y!=0){
			if(a[y-1][x]=='.'){
				f++;
				js--;
				d++;
				
			}
			y--;
		}
		if(b[0]=='s'&&y!=9){
			if(a[y+1][x]=='.'){
				f++;
				js--;
				d++	;
			}
			y++;
		}
		if(b[0]=='a'&&x!=0){
			if(a[y][x-1]=='.'){
				f++;
				js--;
				d++	;
			}
			x--;
		}
		if(b[0]=='d'&&x!=19){
			if(a[y][x+1]=='.'){
				f++;
				js--;
				d++;
			}
			x++;
		}
		j++;
		if(j>19&&js<=4){
			gz();
			j=0;
		}
	}
	return 0;
}

2024/7/11 08:15
加载中...