0
已解决
当前功能:wasd移动 撞墙自动挖墙(需要5下挖掉)
代码基本手搓,输出颜色控制借助了AI
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<stack>
#include<map>
#include<windows.h>
using namespace std;
struct node_start{
int tx,ty,way;
};
struct node_dir{
int dx,dy;
};
char a[5200][5200];
int cnt[5200][5200];//挖掘状态
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}},x,y;
int lx,ly,rx,ry;
char wmp;
bool vis[5200][5200],vistart[5200][5200];
stack<node_start> s;
void setColor(int color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}//颜色函数
void start(int len,int cx,int cy){
int x,y,w,k,nx,ny;
srand((unsigned int)time(NULL));
// 初始化
s.push({cx,cy,-1});
vis[cx][cy]=true;
a[cx][cy]=' ';
int step=0;
int max_step=len; // 控制线条总长度
while(!s.empty() && step<max_step){
node_start u=s.top();
x=u.tx;
y=u.ty;
w=u.way;
//寻找一个未访问的方向
bool found=false;
int tmp=0;
// 随机尝试序列
int dirs[4]={0,1,2,3};
for(int i=0;i<4;i++){
int r=rand()%4;
swap(dirs[i],dirs[r]);
}
for(int i=0;i<4;i++){
k=dirs[i];
// 避免直接回头
if(w!=-1 && k==(w+2)%4) continue;
nx=x+dir[k][0];
ny=y+dir[k][1];
if(nx>1 && nx<5000 && ny>1 && ny<5000 && !vis[nx][ny]){
vis[nx][ny]=true;
a[nx][ny]=' ';
s.push({nx,ny,k}); //下一步将继续从新节点延申
found=true;
step++;
break;
}
}
if(!found){
s.pop();
}
}
}
int main(){
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);//获取修改输出颜色权限
for(int i=0;i<5200;i++){
for(int j=0;j<5200;j++) {
a[i][j]='H';
vis[i][j]=false;
}
}
start(5000,2500,2500);
start(5000,2450,2550);
start(5000,2550,2450);
for(int i=1;i<=5000;i++){
for(int j=1;j<=5000;j++){
if(a[i][j]==' ' && rand()%150==0 || rand()%1000==0){
vistart[i][j]=true;
start(rand()%200+1,i,j);
}
}
}
for(int i=1;i<=5000;i++){
for(int j=1;j<=5000;j++){
if(vistart[i][j]){
start(rand()%200+1,i,j);
}
cnt[i][j]=5;
}
}
/*
for(int i=2470;i<=2570;i++){
for(int j=2470;j<=2570;j++){
cout<<a[i][j];
}
cout<<endl;
}*/
//地图查看器
map<char,node_dir> m;
m['w']={-1,0};
m['s']={1,0};
m['a']={0,-1};
m['d']={0,1};
x=2500,y=2500;
while(true){
system("cls");
lx=x-8;
rx=x+8;
ly=y-16;
ry=y+16;
for(int i=lx;i<=rx;i++){
for(int j=ly;j<=ry;j++){
if(i==x && j==y){
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout<<'I';
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
continue;
}
if(cnt[i][j]==5){
cout<<a[i][j];
}else if(cnt[i][j]>=3){
setColor(FOREGROUND_INTENSITY);
cout<<a[i][j];
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}else{
setColor(FOREGROUND_RED | FOREGROUND_BLUE);
cout<<a[i][j];
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
}
cout<<endl;
}//输出地图
cin>>wmp;
if(wmp=='w' || wmp=='a' || wmp=='s' || wmp=='d'){
if(a[x+m[wmp].dx][y+m[wmp].dy]==' '){
x+=m[wmp].dx;
y+=m[wmp].dy;
}else if(a[x+m[wmp].dx][y+m[wmp].dy]=='H'){
cnt[x+m[wmp].dx][y+m[wmp].dy]--;
if(cnt[x+m[wmp].dx][y+m[wmp].dy]==0){
a[x+m[wmp].dx][y+m[wmp].dy]=' ';
}
}
}
}
return 0;
}
