跳到正文

个人记录

Maze.cpp

CodingLoverRao29约 14 分钟
原文
文章信息与版本

文章信息

发布
2025年8月23日
收录
2026年1月1日
更新
2026年11月18日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1
C++
//A Simple Maze Game
//Welcome to here!
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<utility>
#include<vector>

#define pf    place.first
#define ps    place.second
#define Kai   true
#define Guan  false
using namespace std;

// Setting
const int           N = 20;                // 地图大小,默认值:20 
const int         SEE = 2;                 // 视野范围,默认值:2 
const string       Ti = "Maze";            // 标题,默认值:"Maze" 
const string        V = "Release v1.0.10"; // 版本 
const char       WALL = '#';               // 墙的字符,默认值:'#' 
const char     GROUND = ' ';               // 地面的字符,默认值:' ' 
const char       WHAT = '.';               // all里的未知格子的字符,默认值:'.' 
const char     Player = 'T';               // 代表玩家的字母,默认值:'T'
const bool   isShowMe = Kai;               // 是否显示自己,默认值:Kai
const bool isShowList = Kai;               // 是否显示帮助列表,默认值:Kai
const bool isMakeCode = Guan;              // 是否生成地图编码(实验性功能),默认值:Guan 

/*
By CodingLoverRao29
Update: 2026-8-19
Now:Release v1.0.10
更新日志:
1.0.0:初版
1.0.1:更新 kill 
1.0.2:更新视野 
1.0.3:更新 all 函数,可以在游戏结束后显示自己所看到的大地图 
       更新设置,可以便捷地调整地图格式了 
       改变了默认的地图字符
	   增加了一个开关,可以在地图中显示自己,默认开启 
1.0.4:新增帮助列表及开关,默认开启 
       新增宏定义 T, F, CLOSE, OPEN
1.0.5:将 op 的读入从字符改为字符串,使输入多个字符时避免出现问题
	   (例如:更新前,输入 www 可以触发向上走(迷之操作),现在不可以了) 
1.0.6:更新 bfs 函数,判断可行性 
1.0.7:更新地图编码,保存在 MapCode.txt 中 
       更新 isMakeCode 开关,默认关闭 
1.0.8:更新统计数据,包括正确移动与无效移动
       将地图可达性的表示方法换成True和False
	   增加两个宏定义 Kai 和 Guan,分别表示 true 与 false 
1.0.9:增加了边界判定,也就是说修复了会跑到 x 地区的情况 
1.0.10:更改了一些默认设定,增加了Player常量
       优化了代码结构,现在不会出现可达性为false的情况,并且删除了\kill 
       增加了设置效验,不要打SEE=-1啊! 
       [@fxh1020] e-太难打了,于是跳跃操作更改为连续输入两个同个字符 
*/ 


char ditu[N][N];
char seeditu[N][N];
bool bfsditu[N][N];
vector<pair<int,int> > bfsqueue;
int dx[8]={1,-1,0,0,2,-2,0,0};
int dy[8]={0,0,1,-1,0,0,2,-2};
string code=""; 
pair<int,int> place=make_pair(0,0);
int suc_mov=0,fai_mov=0,all_mov=0;

char mmm(int nnn){
	if(nnn){
		code+='1';
		return WALL;
	}
	else{
		code+='0';
		return GROUND;
	}
}

void makemap(){
	for(int i=0; i<N; ++i){
		for(int j=0; j<N; ++j){
			if(i==0&&j==0){
				ditu[i][j]='s';
			}
			else if(i==N-1&&j==N-1){
				ditu[i][j]='e';
			}
			else{
				ditu[i][j]=mmm(rand()%2);
			}
			seeditu[i][j]=WHAT;
		}
	}
}

void see(){
	for(int i=pf-SEE; i<=pf+SEE; ++i){
		for(int j=ps-SEE; j<=ps+SEE; ++j){
			if(i<0||i>N-1||j<0||j>N-1){
				cout<<"x"<<" ";
			}
			else if(isShowMe&&i==pf&&j==ps){
				cout<<Player<<" ";
				seeditu[i][j]=ditu[i][j];
			}
			else{
				cout<<ditu[i][j]<<" ";
				seeditu[i][j]=ditu[i][j];
			}
		}
		cout<<endl;
	}
}

void all(){
	for(int i=0; i<N; ++i){
		for(int j=0; j<N; ++j){
			cout<<seeditu[i][j]<<" ";
		}
		cout<<endl;
	}
}

void ShowList(){
	if(!isShowList){
		return;
	}
	cout<<"w: Move up 1 space;"<<endl;
	cout<<"s: Move down 1 space;"<<endl;
	cout<<"a: Move left 1 space;"<<endl;
	cout<<"d: Move right 1 space;"<<endl;
	cout<<"ww: Move up 2 spaces;"<<endl;
	cout<<"ss: Move down 2 spaces;"<<endl;
	cout<<"aa: Move left 2 spaces;"<<endl;
	cout<<"dd: Move right 2 spaces;"<<endl<<endl;
}

bool bfs(){
	bfsqueue.clear(); 
	bfsqueue.push_back(make_pair(0,0));
	memset(bfsditu,false,sizeof(bfsditu));
	bfsditu[0][0]=true;
	int tail=1,head=0;
	while(tail>head){
		if(bfsqueue[head].first==N-1&&bfsqueue[head].second==N-1){
			return true;
		}
		for(int i=0; i<8; ++i){
			int x=bfsqueue[head].first+dx[i],y=bfsqueue[head].second+dy[i];
			if(x>=0&&x<N&&y>=0&&y<N&&ditu[x][y]!=WALL){
				if(!bfsditu[x][y]){
					bfsqueue.push_back(make_pair(x,y));
					tail++;
					bfsditu[x][y]=true;
				}
			}
		}
		head++;
	}
	return false;
}

int main(){
	if(N<=1||SEE<0||SEE>N/2||N>150){
		cout<<"Please check & change your Settings."<<endl;
		return 1; 
	}
	srand(time(0));
	cout<<"Wait for a minute..."<<endl;
	bool cango = false;
	do{
		makemap();
		cango=bfs();
	}while(!cango);
	system("cls");
	cout<<Ti<<" "<<V<<endl;
	cout<<"New output: ---------------"<<endl<<endl;
	cout<<"Now: 0, 0"<<endl;
	cout<<"Limit: "<<N-1<<", "<<N-1<<endl;
	string op;
	while(1){
		cout<<endl;
		cout<<"You can enter w, a, s, d, ww, aa, ss or dd to move."<<endl;
		ShowList();
		see();
		getline(cin,op);
		all_mov++;
		system("cls");
		cout<<Ti<<" "<<V<<endl;
		cout<<"New output: ";
		if(op=="w"){
			if(pf-1>=0&&(ditu[pf-1][ps]==GROUND||ditu[pf-1][ps]=='s'||ditu[pf-1][ps]=='e')){
				pf--;
				cout<<"Successfully moved up."<<endl;
				suc_mov++;
			}
			else{
				cout<<"You cannot move to "<<pf-1<<", "<<ps<<endl;
				fai_mov++;
			}
		}
		else if(op=="s"){
			if(pf+1<N&&(ditu[pf+1][ps]==GROUND||ditu[pf+1][ps]=='s'||ditu[pf+1][ps]=='e')){
				pf++;
				cout<<"Successfully moved down."<<endl;
				suc_mov++;
			}
			else{
				cout<<"You cannot move to "<<pf+1<<", "<<ps<<endl;
				fai_mov++;
			}
		}
		else if(op=="a"){
			if(ps-1>=0&&(ditu[pf][ps-1]==GROUND||ditu[pf][ps-1]=='s'||ditu[pf][ps-1]=='e')){
				ps--;
				cout<<"Successfully moved left."<<endl;
				suc_mov++;
			}
			else{
				cout<<"You cannot move to "<<pf<<", "<<ps-1<<endl;
				fai_mov++;
			}
		}
		else if(op=="d"){
			if(ps+1<N&&(ditu[pf][ps+1]==GROUND||ditu[pf][ps+1]=='s'||ditu[pf][ps+1]=='e')){
				ps++;
				cout<<"Successfully moved right."<<endl;
				suc_mov++;
			}
			else{
				cout<<"You cannot move to "<<pf<<", "<<ps+1<<endl;
				fai_mov++;
			}
		}
		else if(op=="ww"){
			if(pf-2>=0&&(ditu[pf-2][ps]==GROUND||ditu[pf-2][ps]=='s'||ditu[pf-2][ps]=='e')){
				pf-=2;
				cout<<"Successfully jumped up."<<endl;
				suc_mov++;
			}	
			else{
				cout<<"You cannot move to "<<pf-2<<", "<<ps<<endl;
				fai_mov++;
			}
		}
		else if(op=="ss"){
			if(pf+2<N&&(ditu[pf+2][ps]==GROUND||ditu[pf+2][ps]=='s'||ditu[pf+2][ps]=='e')){
				pf+=2;
				cout<<"Successfully jumped down."<<endl;
				suc_mov++;
			}	
			else{
				cout<<"You cannot move to "<<pf+2<<", "<<ps<<endl;
				fai_mov++;
			}
		}
		else if(op=="aa"){
			if(ps-2>=0&&(ditu[pf][ps-2]==GROUND||ditu[pf][ps-2]=='s'||ditu[pf][ps-2]=='e')){
				ps-=2;
				cout<<"Successfully jumped left."<<endl;
				suc_mov++;
			}	
			else{
				cout<<"You cannot move to "<<pf<<", "<<ps-2<<endl;
				fai_mov++;
			}
		}
		else if(op=="dd"){
			if(ps+2<N&&(ditu[pf][ps+2]==GROUND||ditu[pf][ps+2]=='s'||ditu[pf][ps+2]=='e')){
				ps+=2;
				cout<<"Successfully jumped right."<<endl;
				suc_mov++;
			}	
			else{
				cout<<"You cannot move to "<<pf<<", "<<ps+2<<endl;
				fai_mov++;
			}
		}
		else{
			cout<<"Invalid move."<<endl;
			fai_mov++;
		}
		cout<<endl;
		cout<<"Now: "<<pf<<", "<<ps<<endl;
		cout<<"Limit: "<<N-1<<", "<<N-1<<endl;
		if(ditu[pf][ps]=='e'){
			system("cls");
			cout<<Ti<<" "<<V<<endl;
			cout<<"New output: You win!"<<endl<<endl;
			cout<<"Now: "<<N-1<<", "<<N-1<<endl;
			cout<<"Limit: "<<N-1<<", "<<N-1<<endl<<endl;
			cout<<"You do not need to type now."<<endl;
			all();
			cout<<"You moved "<<all_mov<<" steps,"<<endl;
			cout<<suc_mov<<" successful steps and "<<fai_mov<<" invaild steps.";
			cout<<endl;
			if(isMakeCode){
				freopen("MapCode.txt","w",stdout);
                cout<<"MapCode: "<<code<<endl;
			}
			system("pause >nul");
			return 0;
		}
	}
}
原文