修练者
走迷宫0.1完整版的代码
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <cstdio>
#include <windows.h>
using namespace std;
string map[205][205]; //20*20的地图
int cho,g_cnt,go_pl=10; //金币初始为10(买一个炸药)
int lg1=20,lg2=20;
int cnt,jb,minc=0x3f3f3f3f,maxg;
struct yy{
int x,y,h; //map[x][y]
}goa[400],pl; //金币;玩家
int win=0,lose=0;
void end_TJ(int cnt,int jb){//更新最大金币,最小步数
if(jb >maxg)maxg=jb ;
if(cnt<minc)minc=cnt;
}
bool pd(string map[205][205]){//判断地图是否可通行
bool vis[205][205]={{}};
vis[1][1]=1;
for(int i=1;i<=lg1;i++){
for(int j=1;j<=lg2;j++){
if(j==1&&i==1)continue;
if(map[i][j]!="##")vis[i][j]=(vis[i-1][j]||vis[i][j-1]);
}
}
return vis[lg1][lg2];
}
void Color(int a){//颜色控制函数(部分代码为复制)
if(a==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//标准
if(a==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE); //浅蓝
if(a==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN); //绿
if(a==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE); //紫红
if(a==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED); //浅橙
if(a==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); //浅黄
if(a==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE); //蓝
if(a==7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); //浅灰
if(a==8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED); //红
if(a==9) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_BLUE); //浅蓝 外
if(a==10) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_BLUE); //紫红 外
if(a==11) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_BLUE); //深紫
if(a==12) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN); //棕黄
if(a==13) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY); //深灰
if(a==14) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE); //较浅蓝
}
void game(){ //走迷宫函数
int sea,dif;
cout<<"请输入一个随机的地图种子(100~999)\n";
char n1=getch();//用getch函数模拟输入(防止一群丧彪乱按导致游戏崩溃)
cout<<n1;
char n2=getch();
cout<<n2;
char n3=getch();
cout<<n3<<endl;
sea=(n1-'0')*100+(n2-'0')*10+(n3-'0');
cout<<"请输入难度(1~3)\n";
char c1=getch()-'0';
dif=c1;
cout<<dif<<endl;
int sc=5-dif,sc1=dif*400,tl=(5-dif)*50;
pl.x=1;
pl.y=1;
cout<<"生成地图中,请稍后...\n";
while(sea--){ //生成地图
for(int i=1;i<=lg1;i++){
for(int j=1;j<=lg2;j++){
if(rand()%sc==1)map[i][j]="##";
else map[i][j]=" ";
}
}
}
jb=10;
cnt=0;
go_pl=10;
g_cnt=0;
map[1][1]=" ";
map[lg1][lg2]=" M";
cout<<"按动w/s/a/d开始游戏\n";
while(1){
cnt++;
if(tl==0){
system("cls");
lose++;
end_TJ(cnt,jb);
Color(8);
cout<<"you die...\n\n";
Color(0);
cout<<"按任意键退出";
// Sleep(800);
char ed=getch();
return;
}
if(pl.x==lg1&&pl.y==lg2){
system("cls");
win++;
end_TJ(cnt,jb);
Color(5);
cout<<"you win!\n\n";
Color(0);
cout<<"按任意键退出";
// Sleep(800);
char ed=getch();
return;
}
cho=getch();
map[pl.x][pl.y]=" ";
for(int i=1;i<=lg1;i++){
for(int j=1;j<=lg2;j++){
if(rand()%sc1==1&&map[i][j]==" "){//生成新的金币
g_cnt++;
goa[g_cnt].x=i;
goa[g_cnt].y=j;
}
if(map[i][j]=="go")map[i][j]=" "; //取消老的金币
}
}
if(cho=='w'){
if(pl.x>1&&map[pl.x-1][pl.y]!="##")pl.x--;system("cls");
}else if(cho=='s'){
if(pl.x<lg1&&map[pl.x+1][pl.y]!="##")pl.x++;system("cls");
}else if(cho=='a'){
if(pl.y>1&&map[pl.x][pl.y-1]!="##")pl.y--;system("cls");
}else if(cho=='d'){
if(pl.y<lg2&&map[pl.x][pl.y+1]!="##")pl.y++;system("cls");
}
if(cho=='e'){//商店
system("cls");
cout<<"1.面包:2金币 (增加10体力)\n2.烤肉:4金币 (增加25体力)\n3.炸药:10金币 (破环以你为中心5*5的障碍)\n4.退出";
char cho1;
cho1=getch();
if(cho1=='1'&&go_pl>=2){
go_pl-=2;
tl+=10;
}else if(cho1=='2'&&go_pl>=4){
go_pl-=4;
tl+=25;
}else if(cho1=='3'&&go_pl>=10){
go_pl-=10;
for(int n=pl.x-2;n<=pl.x+2;n++){
for(int m=pl.y-2;m<=pl.y+2;m++){
if(n>=1&&n<=lg1&&m>=1&&m<=lg2&&map[n][m]=="##")map[n][m]=" ";
}
}
}
system("cls");
}
for(int i=1;i<=g_cnt;i++){
if(pl.x==goa[i].x&&pl.y==goa[i].y&&goa[i].h==0){
jb++; //游戏金币+1
go_pl++; //捡金币
goa[i].h=1; //这个金币已经捡过了
}
if(goa[i].h==0)map[goa[i].x][goa[i].y]="go";
}
map[pl.x][pl.y]="Yo";
tl--;
cout<<"金币:"<<go_pl;
if(tl>100)Color(2);
else if(tl>50)Color(5);
else Color(8);
cout<<" 体力:"<<tl<<endl;
for(int i=1;i<=lg1;i++){
for(int j=1;j<=lg2;j++){
if(map[i][j]=="##")Color(13);
if(map[i][j]=="go")Color(12);
if(map[i][j]=="Yo")Color(2);
if(map[i][j]==" M"&&cnt%2==1)Color(9);
if(map[i][j]==" M"&&cnt%2==0)Color(10);
cout<<map[i][j];
Color(0);
}
cout<<endl;
}
}
}
string name="";
bool in(){
freopen("走迷宫.txt","r",stdin);
while(cin>>name){
cin>>win>>lose>>minc>>maxg;
}
fclose(stdin);
if(name==""&&win==0&&lose==0)return 1;
return 0;
}
void gooa(){//玩家信息
system("cls");
Color(7); cout<<"玩家 ";
Color(14);cout<<name<<endl;
Color(7); cout<<"赢得";
Color(5); cout<<win;
Color(7); cout<<"局游戏"<<endl<<"输了";
Color(8); cout<<lose;
Color(7); cout<<"局游戏"<<endl<<endl<<"单次游戏最少步数:";
Color(0); cout<<minc;
Color(7); cout<<"步\n"<<"单次游戏最大金币数:";
Color(12);cout<<maxg;
Color(7); cout<<"个"<<endl;
Color(0); cout<<endl<<"按任意键退出";
char end=getch();
return;
}
void Ret(){//结束,存档
freopen("走迷宫.txt","w",stdout);
cout<<name<<" "<<win<<" "<<lose<<" "<<minc<<" "<<maxg;
return;
}
/* 待开发
void map_table(int a){
system("cls");
string s1,s2,s3;
if(a==1)s1=" <<";
if(a==2)s2=" <<";
if(a==3)s3=" <<";
cout<<"+==============+"<<endl;
cout<<"| 地图大小 |"<<s1<<endl;
cout<<"+==============+"<<endl;
cout<<endl;
cout<<"+==============+"<<endl;
cout<<"| 自定义地图 |"<<s2<<endl;
cout<<"+==============+"<<endl;
cout<<endl;
cout<<"+==============+"<<endl;
cout<<"| 返回桌面 |"<<s3<<endl;
cout<<"+==============+"<<endl;
return;
}
*/
void map_tool(){
system("cls");
Color(7); cout<<"目前地图设置:";
Color(0); cout<<lg1<<"*"<<lg2<<endl<<endl;
Color(7); cout<<"请输入新地图的大小\n";
Color(5); cout<<"长 (05~30):";
char d1=getch();
Color(0); cout<<d1;
char d2=getch();
Color(0); cout<<d2<<endl;
Color(5); cout<<"宽 (05~70):";
char q1=getch();
Color(0); cout<<q1;
char q2=getch();
Color(0); cout<<q2<<endl;
lg1=(d1-'0')*10+(d2-'0');
lg2=(q1-'0')*10+(q2-'0');
return;
}
void wel(int a){//初始画面
string s1="",s2="",s3="",s4="";
if(a==1)s1=" <<";
if(a==2)s2=" <<";
if(a==3)s3=" <<";
if(a==4)s4=" <<";
Color(14);cout<<"+============+"<<endl<<"| ";
Color(0); cout<<"开始游戏";
Color(14);cout<<" |";
Color(1); cout<<s1<<endl;
Color(14);cout<<"+============+"<<endl<<endl<<"+============+"<<endl<<"| ";
Color(0); cout<<"玩家信息";
Color(14);cout<<" |";
Color(1) ;cout<<s2<<endl;
Color(14);cout<<"+============+"<<endl<<endl<<"+============+"<<endl<<"| ";
Color(0); cout<<"地图设置";
Color(14);cout<<" |";
Color(1); cout<<s3<<endl;
Color(14);cout<<"+============+"<<endl<<endl<<"+============+"<<endl<<"| ";
Color(0); cout<<"退出游戏";
Color(14);cout<<" |";
Color(1); cout<<s4<<endl;
Color(14);cout<<"+============+"<<endl;
Color(0);
}
int main(){
int now=1;
bool fl=in();//加载玩家信息
if(fl){
minc=0x3f3f3f3f;
cout<<"请你先设置用户名!\n(输入字符'/'代表结束)\n";
char nn;
while(true){
nn=getch();
if(nn=='/')break;
name+=nn;
cout<<nn;
}
system("cls");
}
while(true){
wel(now);
char choo=getch();
if(choo=='s'&&now<4)now++;
if(choo=='w'&&now>1)now--;
if(choo=='a'){
system("cls");
if(now==1)game();
if(now==2)gooa();
if(now==3)map_tool();
if(now==4){
Ret();
break;
}
}
system("cls");
}
// cout<<"走迷宫游戏欢迎你下次游玩!";
return 0;
}
复制到编译器!
下面是人机版
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
string map[1000][1000],map1[1000][1000],map2[1000][1000];
int mmap[1000][1000];
bool t[1000][1000],flag,f1;
void dfs(string map[1000][1000],int m,int n,int lm,int ln,int llm,int lln,int cnt){//m[m][n]
if(flag)return;
system("cls");
mmap[m][n]=cnt;
if((m==120||m==119)&&(n==120||n==119)){
flag=1;
for(int i=100;i<=120;i++){
for(int j=100;j<=120;j++){
map1[i][j]=map[i][j];
}
}
return;
}
if(!f1){
map[m][n]=" +";
if(llm==m)map[lm][ln]="--";
if(lln==n)map[lm][ln]=" |";
if(lm==m){
if(m>lm)map[m][n]="+-";
else map[m][n]="-+";
}
}
// cout<<flag<<endl;
for(int i=100;i<=120;i++){
for(int j=100;j<=120;j++){
if(i==m&&j==n){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(f1)map[i][j]="YO";
// continue;
}
if(map[i][j]=="##")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
cout<<map[i][j];
if(f1&&map[i][j]=="YO")map[i][j]=" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//标准
}
cout<<endl;
}
// Sleep(80);
if(t[m][n]==1)return;
t[m][n]=1;
int mm=m-1;
int nn=n-1;
int ma=m+1;
int na=n+1;
if(map[ma][n]!="##"&&t[ma][n]==0&&m>=100&&m<=120&&n>=100&&n<=120){
// if(!f1)map[ma][n]=" +";
dfs(map,ma,n,m,n,lm,ln,cnt+1);
map[ma][n]=" ";
}
if(map[m][na]!="##"&&t[m][na]==0&&m>=100&&m<=120&&n>=100&&n<=120){
// if(!f1)map[m][na]=" +";
dfs(map,m,na,m,n,lm,ln,cnt+1);
map[m][na]=" ";
}
if(map[mm][n]!="##"&&t[mm][n]==0&&m>=100&&m<=120&&n>=100&&n<=120){
// if(!f1)map[mm][n]=" +";
dfs(map,mm,n,m,n,lm,ln,cnt+1);
map[mm][n]=" ";
}
if(map[m][nn]!="##"&&t[m][nn]==0&&m>=100&&m<=120&&n>=100&&n<=120){
// if(!f1)map[m][nn]=" +";
dfs(map,m,nn,m,n,lm,ln,cnt+1);
map[m][nn]=" ";
}
return;
}
int main(){
cout<<"选择模式(1.人机 0.蛇):"<<endl;
cin>>f1;
cout<<"输入种子:"<<endl;
int s;
cin>>s;
cout<<"输入难度:"<<endl;
int dif;
cin>>dif;
int sc=10-dif;
while(s--){
for(int i=100;i<=120;i++){
for(int j=100;j<=120;j++){
if(rand()%sc==1)map[i][j]="##";
else map[i][j]=" ";
}
}
}
map[20][20]=" ";
dfs(map,100,100,100,100,100,100,1);
for(int i=100;i<=120;i++){
for(int j=100;j<=120;j++){
cout<<map1[i][j];
}
cout<<endl;
}
cout<<endl;
if(flag)cout<<endl<<"人机已到达终点!";
else cout<<endl<<"人机失败!";
return 0;
}
复制到编译器
教程后面会发在评论区
酷丁平台上的代码:
https://kuding1024.com/#/share?uuid=860ccf4d88f34721a857160056653690 (人机)
https://kuding1024.com/#/share?uuid=e7518c9842da490da7f76903d191cea3 (正常)
修练者
这段时间优化了人机版的代码。
他可以帮助你走一个存在解的迷宫。
现在它可以生成更好的路径。
代码如下:
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
string map[1000][1000],map1[1000][1000],map2[1000][1000];
int mmap[1000][1000],lg1,lg2;
bool t[1000][1000],flag,f1;
void dfs(string map[1000][1000],int m,int n,int lm,int ln,int llm,int lln,int cnt){//m[m][n]
if(flag)return;
cout<<345;
system("cls");
mmap[m][n]=cnt;
if((m==100+lg1)&&(n==100+lg2)){
flag=1;
for(int i=100;i<=100+lg1;i++){
for(int j=100;j<=100+lg2;j++){
map1[i][j]=map[i][j];
}
}
return;
}
if(!f1){
map[m][n]=" +";
if(llm==m)map[lm][ln]="--";
if(lln==n)map[lm][ln]=" |";
if(lm==m){
if(m>lm)map[m][n]="+-";
else map[m][n]="-+";
}
}
// cout<<flag<<endl;
for(int i=100;i<=100+lg1;i++){
for(int j=100;j<=100+lg2;j++){
if(i==m&&j==n){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
if(f1)map[i][j]="YO";
// continue;
}
if(map[i][j]=="##")SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
cout<<map[i][j];
if(f1&&map[i][j]=="YO")map[i][j]=" ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//标准
}
cout<<endl;
}
Sleep(20);
if(t[m][n]==1)return;
t[m][n]=1;
int mm=m-1;
int nn=n-1;
int ma=m+1;
int na=n+1;
if(map[ma][n]!="##"&&t[ma][n]==0&&m>=100&&m<=100+lg1&&n>=100&&n<=100+lg2){
// if(!f1)map[ma][n]=" +";
dfs(map,ma,n,m,n,lm,ln,cnt+1);
map[ma][n]=" ";
}
if(map[m][na]!="##"&&t[m][na]==0&&m>=100&&m<=100+lg1&&n>=100&&n<=100+lg2){
// if(!f1)map[m][na]=" +";
dfs(map,m,na,m,n,lm,ln,cnt+1);
map[m][na]=" ";
}
if(map[mm][n]!="##"&&t[mm][n]==0&&m>=100&&m<=100+lg1&&n>=100&&n<=100+lg2){
// if(!f1)map[mm][n]=" +";
dfs(map,mm,n,m,n,lm,ln,cnt+1);
map[mm][n]=" ";
}
if(map[m][nn]!="##"&&t[m][nn]==0&&m>=100&&m<=100+lg1&&n>=100&&n<=100+lg2){
// if(!f1)map[m][nn]=" +";
dfs(map,m,nn,m,n,lm,ln,cnt+1);
map[m][nn]=" ";
}
return;
}
void YH(string map1[1000][1000]){
map1[100][100]="ST";
map1[100+lg1][100+lg2]="ED";
for(int i=100;i<=100+lg1;i++){
for(int j=100;j<=100+lg2;j++){
if(map1[i][j]=="--"&&(map1[i-1][j]==" |"||map1[i+1][j]==" |"))map1[i][j]="-+";
if(map1[i][j]==" +"&&(map1[i][j-1]=="--"&&map1[i][j-1]=="-+"&&map1[i][j-1]==" +"))map1[i][j]="-+";
if(map1[i][j]=="-+"&&(map1[i][j+1]==" +"||map1[i][j+1]=="--")&&(map1[i][j+2]!="--"&&map1[i][j+2]!="-+"&&map1[i][j+2]!=" +")){
map1[i][j]=" +";
map1[i][j+1]="-+";
}
if(map1[i][j]==" |"&&(map1[i][j+1]=="--"||map1[i][j+1]=="-+"))map1[i][j]=" +";
if(map1[i][j]=="--")map1[i][j]="══";
if(map1[i][j]==" |")map1[i][j]=" ║";
if(map1[i][j]==" +"){
if(map1[i-1][j]!="##"&&map1[i-1][j]!=" "){
map1[i][j]=" ╚";
}else {
map1[i][j]=" ╔";
}
}
if(map1[i][j]=="-+"){
if(map1[i-1][j]!="##"&&map1[i-1][j]!=" "){
map1[i][j]="═╝";
}else {
map1[i][j]="═╗";
}
if(i==120&&j==119)map1[i][j]="═ ";
}
if(map1[i][j]=="══"&&map1[i-1][j]!="##"&&map1[i-1][j]!=" "){
map1[i][j]="═╝";
}
}
}
if(map1[100][101]!="##"&&map1[100][101]!=" ")map1[100][101]=" ╗";
return;
}
void ot(string map1[1000][1000]){
if(!flag){
cout<<endl<<"人机失败!";
return;
}
for(int i=100;i<=100+lg1;i++){
for(int j=100;j<=100+lg2;j++){
if(map[i][j]=="##") SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY);
else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
if(i==100&&j==100||i==100+lg1&&j==100+lg2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
cout<<map1[i][j];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
}
cout<<endl;
}
cout<<endl;
if(flag)cout<<endl<<"人机已到达终点!";
return;
}
int main(){
cout<<"输入大小"<<endl;
cin>>lg1>>lg2;
cout<<"选择模式(1.人机 0.蛇):"<<endl;
cin>>f1;
cout<<"输入难度:"<<endl;
int dif;
cin>>dif;
cout<<"输入种子:"<<endl;
int sc=10-dif;
for(int i=1;i<=1000;i++){
flag=0;
f1=0;
system("cls");
for(int a=1;a<=lg1;a++){
for(int j=1;j<=lg2;j++){
map[a][j]=" ";
map1[a][j]=" ";
}
}
int s=i;
while(s--){
for(int i=100;i<=100+lg1;i++){
for(int j=100;j<=100+lg2;j++){
if(rand()%sc==1)map[i][j]="##";
else map[i][j]=" ";
}
}
}
map[100][100]=" ";
dfs(map,100,100,100,100,100,100,1);
YH(map1);
ot(map1);
}
return 0;
}
/*
╝═╚║╗╔ 基本部分
*/
游戏截图: