#include <iostream>
#include <conio.h>
#include<stdlib.h>
using namespace std;
#define ESC 27
//void SelectSort(int * iArrac,int iSize);
//record the Position//记录位置
typedef struct _tagPoint//定义位置结构体
{
int r;//行
int c;//列
}Point;
//reloaded the != operator
bool operator !=(const Point & pt0,const Point & pt1)//重载!=号
{
return !((pt0.r == pt1.r) && pt0.c == pt1.c);
}
enum enumDirect{dirUp = 8,dirDown = 2,dirLeft = 4,dirRight = 6};//枚举类型定义移动的方向
//Show cour Position//打印位置
void ShowPosition(const Point & ptPos)//显示所在位置
{
cout << "我们所在的位置是第"
<< ptPos.r + 1
<< "行,第"
<< ptPos.c + 1
<< "列"<<endl;
}
bool CanMove(Point ptTmp,char * a,int iRow,int iCol)//检查是否可以移动,数组元素是否为0
{
return *(a + ptTmp.r * iCol + ptTmp.c) == 0;//返回0
}
void Showa(Point ptTmp,char * a,int iRow,int iCol)//显示地图
{
for(int i = 0;i < iRow;i++)
{
for(int j = 0;j < iCol;j++)
{
if((i == ptTmp.r) && (j == ptTmp.c))//判断是否为可移动点
cout << " A ";//打印人物点
else
cout << " " << char(42*char(*(a + i * iCol + j)))<< " ";//输出地图
}
cout << endl;
}
}
void disp()
{
{char a[10][10] = {
{1,1,1,1,1,1,1,1,1,1},//定义地图数组
{1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1},
{1,0,1,1,1,1,0,1,1,1},
};//打印地图数组
Point ptStart = {9,1};//定义入口位置
Point ptEnd = {9,6};//定义出口位置
Point ptPos = {-1,-1};//定义打印点的起始位置
int iDirect = dirDown;//定义按下按钮
bool bStart = true;//定义是否开始
while(ptPos != ptEnd && ptPos != ptStart && iDirect!=27)//是否为出口或者入口
{
system("cls");//清屏
//Showa(ptPos,*a,10,10);
Showa(ptPos,(char *)a,10,10);//打印地图
cout << "移动操作提示:" << endl;//给予提示
cout << "8-- 上, 2-- 下, 4-- 左, 6-- 右!" << endl;//给出控制提示
ShowPosition(ptPos);//打印输入的坐标
iDirect = getch() - '0';//获取用户输入方向,初始化为0。
if(bStart) //判断游戏是否开始
{
if(iDirect == dirDown)//开始的时候判断用户在入口处输入的方向是否正确
{
cout << "\07";//警报声输出
cout << "无法移动!" << endl;//提示无法移动
continue;//继续游戏
}
ptPos = ptStart;//设置所处位置状态
bStart = false;//未开始
}
Point ptTmp = ptPos;//定义结构体ptTmp并赋予开始位置
switch(iDirect)//方向判断及位置移动
{
case dirUp:
ptTmp.r--;//往上移动一格, 列向上移动一位
break;
case dirDown:
ptTmp.r++;//往下移动一格,列向下移动一位
break;
case dirLeft:
ptTmp.c--;//往左移动一格,行向左移动一位
break;
case dirRight:
ptTmp.c++;//往右移动一格,行向右移动一位
break;
}
if(CanMove(ptTmp,(char *)a,9,10))//判断是否可以可以移动
{
ptPos = ptTmp;//可以移动的情况
ShowPosition(ptPos);//打印游戏移动的位置
cout << "\07";//输出一声警报声
}
else//不可移动则的提示
{
cout << "\07\07\07\07\07";//输出连续警报声
cout << "不能移动!" << endl;//提示无法移动
}
}
}
cout << "恭喜你,成功走出迷宫!" << endl;
cout<<"你太强大了!";
}
void main()//主函数
{
char c='0';system("CLS");//清屏
while(c!=27)
{
cout<<"欢迎加入走迷宫游戏,祝您游戏愉快。\n\n";
cout<<"1.输入数字1进行游戏\n2.输入Esc退出游戏\n请选择:";
c=getch();system("CLS");//
switch(c)//捕捉输入
{
case '1':disp();break;
case 27:cout<<"欢迎下次再来";break;
}
cout<<"按任意键返回主菜单"<<endl;c=getch();system("CLS");//清屏
}
}
走迷宫
最新推荐文章于 2021-05-18 13:51:31 发布