大家好,你们想要的迷宫小游戏最新版本来了!!!
本次更新内容:
1:增加动画效果设置
2:修复了一些错误
废话不多说,直接上代码!!!!!
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int n,i,j,f,mp[51][51],vis[51][51],x=1,y=1,ch,e,c,p,t;
string s;
struct h{
int a[51][51];
int n;
}a[1001];
void edit();
void game();
void start();
void stop(){
system("cls");
cout<<"是否退出?\n1:是\n2:否\n";
ch=getch();
if(ch=='1'){
system("cls");
cout<<"正在退出…";
Sleep(2000);
exit(0);
}
else{
system("cls");
cout<<"我将为您返回主界面…";
Sleep(2000);
start();
return;
}
}
void read(){
n=0;
getline(cin,s);
for(i=0;i<s.size();i++){
if(s[i]<'0'||s[i]>'9'){
n=0;
return;
}
n=n*10+s[i]-'0';
}
}
void save(){
a[++c].n=n;
for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[c].a[i][j]=mp[i][j];
}
void go(int x,int y){
COORD pos;
pos.X=x-1;
pos.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
return;
}
void start(){
e=0;
system("cls");
cout<<"迷宫小游戏 V3.2\n1:简单(10*10)\n2:中等(15*15)\n3:困难(20*20)\n4:自定义(n*n)(2~29)\n5:规则\n6:迷宫编辑器\n7:设置\n8:历史记录\n按其他退出\n";
go(1,11);
ch=getch();
if(ch>='1'&&ch<='3')n=5*(ch-47);
else if(ch=='4'){
cout<<"\n请输入n:\n";
read();
while(n<2||n>29){
cout<<"输入错误\n";
read();
}
}
else if(ch=='5'){
system("cls");
cout<<"1表示道路,0表示障碍,光标处表示玩家位置\n只能通过1,玩家走到0,游戏失败\n按w、a、s、d控制上下左右的方向\n左上角为起点,右下角为终点\n按任意键退出";
getch();
start();
return;
}
else if(ch=='6'){
edit();
return;
}
else if(ch=='7'){
system("cls");
cout<<"背景颜色\na:黑色\nb:红色\nc:蓝色\nd:黄色\ne:绿色\nf:白色\n\n光标处表示选中颜色\n\n0:迷宫输出特效"<<(t?"开":"关")<<"\n\n1:确定";
ch=0;
for(;;){
ch=getch();
if(ch=='a')system("color 07");
else if(ch=='b')system("color 47");
else if(ch=='c')system("color 17");
else if(ch=='d')system("color 67");
else if(ch=='e')system("color 27");
else if(ch=='f')system("color f0");
if(ch>='a'&&ch<='f')go(1,ch-'a'+2);
if(ch=='1'){
start();
return;
}
else if(ch=='0'){
t^=1;
go(16,11);
if(t)cout<<"开";
else cout<<"关";
}
}
getch();
start();
}
else if(ch=='8'){
system("cls");
if(c==0){
cout<<"您没有历史记录\n按任意键退出";
getch();
start();
}
else{
p=1;
cout<<"您有"<<c<<"条历史记录\n按w、s切换\n1:重玩\n2:退出\n3:清空\n编号 大小\n";
for(i=1;i<=c;i++)printf("%4d %d*%d\n",i,a[i].n,a[i].n);
go(1,7);
for(;;){
ch=getch();
if(ch=='w'&&p>1)p--;
else if(ch=='s'&&p<c)p++;
else if(ch=='1'){
e=1;
memcpy(mp,a[p].a,sizeof(a[p].a));
n=a[p].n;
game();
return;
}
else if(ch=='2'){
start();
return;
}
else if(ch=='3'){
system("cls");
cout<<"正在清空…";
Sleep(1000);
c=0;
start();
return;
}
go(1,6+p);
}
}
}
else stop();
game();
return;
}
void dfs(int x,int y){
if(x<1||x>n||y<1||y>n)return;
if(vis[x][y]==0)return;
if(x==n&&y==n){
f=1;
return;
}
vis[x][y]=0;
dfs(x+1,y);
dfs(x-1,y);
dfs(x,y+1);
dfs(x,y-1);
return;
}
void make(){
srand(time(0));
f=0;
while(!f){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==1&&j==1||i==n&&j==n)mp[i][j]=1;
else mp[i][j]=rand()%2;
}
}
memcpy(vis,mp,sizeof(mp));
dfs(1,1);
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cout<<mp[i][j];
if(t)Sleep(5);
}
cout<<"\n";
}
go(1,1);
return;
}
void change(int x,int y){
go(y,x);
if(!(x==1&&y==1||x==n&&y==n)){
mp[x][y]^=1;
cout<<mp[x][y];
}
}
void edit(){
e=x=y=1;
f=0;
system("cls");
cout<<"迷宫编辑器\n按空格键切换当前位置的属性(左上角和右下角不可以切换),按w、a、s、d控制上下左右的位置,按i、j、k、l一边控制上下左右的位置,一边切换当前位置的属性\n请输入迷宫的边长(2~50)\n";
read();
while(n<2||n>50){
cout<<"输入错误\n";
read();
}
system("cls");
cout<<"生成模板中…";
Sleep(1000);
system("cls");
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==1&&j==1||i==n&&j==n)mp[i][j]=1;
else mp[i][j]=0;
cout<<mp[i][j];
if(t)Sleep(5);
}
cout<<"\n";
}
cout<<"1:完成\n2:退出\n3:保存\n";
go(1,1);
for(;;){
ch=getch();
if(ch=='1'){
memcpy(vis,mp,sizeof(mp));
dfs(1,1);
if(!f){
go(n,n+3);
cout<<"\n此迷宫有问题\n按任意键退出";
getch();
start();
return;
}
else{
game();
return;
}
}
else if(ch=='2'){
start();
return;
}
else if(ch=='3'){
f=0;
memcpy(vis,mp,sizeof(mp));
dfs(1,1);
if(!f){
go(n,n+3);
cout<<"\n此迷宫有问题\n无法保存\n按任意键退出";
getch();
start();
return;
}
else{
save();
system("cls");
cout<<"保存成功\n按任意键退出";
getch();
start();
return;
}
}
else if(ch=='a'&&y>1)y--;
else if(ch=='w'&&x>1)x--;
else if(ch=='d'&&y<n)y++;
else if(ch=='s'&&x<n)x++;
else if(ch==' ')change(x,y);
else if(ch=='j'&&y>1)change(x,--y);
else if(ch=='i'&&x>1)change(--x,y);
else if(ch=='l'&&y<n)change(x,++y);
else if(ch=='k'&&x<n)change(++x,y);
go(y,x);
}
return;
}
void game_stop(){
cout<<"1:返回主界面\n2:退出";
ch=getch();
if(ch=='1'){
system("cls");
cout<<"我将为您返回主界面…";
Sleep(1000);
start();
return;
}
else{
stop();
return;
}
}
void game(){
system("cls");
cout<<"开始!";
Sleep(1000);
system("cls");
if(!e)make();
else{
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cout<<mp[i][j];
if(t)Sleep(5);
}
cout<<"\n";
}
go(1,1);
}
save();
x=y=1;
while(x<n||y<n){
ch=getch();
if(ch=='a'&&y>1)y--;
else if(ch=='w'&&x>1)x--;
else if(ch=='d'&&y<n)y++;
else if(ch=='s'&&x<n)x++;
if(mp[x][y]==0){
Sleep(500);
go(n,n);
cout<<"\n你失败了!!!\n";
Sleep(1000);
game_stop();
}
go(y,x);
}
cout<<"\n恭喜你,成功!!!\n";
Sleep(1000);
game_stop();
}
int main(){
start();
return 0;
}