#include <iostream>
#include <conio.h>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main(){
char maze[18][24]= {"************** ",//1
" * * ",//2
" ************ * ** * ",//3
" * **** * ",//4
" ********** * * * ",//5
" ** * * ",//6
" ** ***** ***** * ** ",//7
" * * ",//8
" * ********** ",//9
" * * * * $ ",//10
" **** * * ****** ",//11
" * * * * * * ",//12
" * ****** * ** * * * ",//13
" * * ** * * * ",//14
" ********** ** * ",//15
" * ",//16
"*** ***",//17
"***********************"};//18
int x,y;
int count = 0;
srand(time(0));
x = rand() % 18;
y = rand() % 23;
while(maze[x][y] != ' '){
x = rand() % 18;
y = rand() % 23;
}
maze[x][y] = 'i';
int i,j;
for(i = 0;i < 18;++i){
for(j = 0;j < 23;++j){
cout << maze[i][j] << " ";
}
cout << endl;
}
char c;
while(true){
c = getch();
++count;
system("cls");
if(c == 'w'){
if(maze[x-1][y] != ''){
maze[x][y] = ' ';
--x;
maze[x][y] = 'i';
}
}
if(c == 'a'){
if(maze[x][y-1] != ''){
maze[x][y] = ' ';
--y;
maze[x][y] = 'i';
}
}
if(c == 's'){
if(maze[x+1][y] != ''){
maze[x][y] = ' ';
++x;
maze[x][y] = 'i';
}
}
if(c == 'd'){
if(maze[x][y+1] != ''){
maze[x][y] = ' ';
++y;
maze[x][y] = 'i';
}
}
for(i = 0;i < 18;++i){
for(j = 0;j < 23;++j){
cout << maze[i][j] << " ";
}
cout << endl;
}
if(maze[9][20] == 'i'){
maze[0][14] = ' ';
MessageBox(0,"取到宝箱","提示",MB_OK);
}
if(x == 0 && y == 14 && maze[9][20] == ' '){
string steps = "走出迷宫,使用步数为:"; //messagebox显示使用步数
char sum[100];
itoa(count,sum,10); //将记录玩家步数的变量sum,转成字符串,赋值给iToA数组
steps += sum;
MessageBox(0,steps.c_str(),"提示",MB_OK);//将获得的字符串显示在messagebox上
break;
}
if(x<0){
MessageBox(0,"请重新开始","提示",MB_OK);
break;
}
}
return 0;
}