上图:最后一个函数,把光标移动到相应的坐标位置上;
Public.h
#ifndef Public_H_
#define Public_H_
const int FAST = 1; //定义人的移动速度快速
const int MIDD = 2; //定义人的移动速度中速
const int SLOW = 3; //定义人的移动速度慢速
const int WALL = 1; //定义墙壁用1表示
const int ROAD = 0; //定义通道用0表示
enum objectiveDirections
{
SOUTH,NORTH,WEST,EAST //南(上),北(下),西(左),东(右)
};
#endif //Public_H_
MazeMap.h
class MazeMap
{
public:
MazeMap();
~MazeMap();
void setMazeMap(int *p,int m,int n);
void setMazeWall(char c);
bool checkWallOrNot(int mazeX, int mazeY);
void drawMap();
private:
char m_cWall;
char m_cRoad;
int m_IRow;
int m_IColumn;
int mazeMapArray[10][10];
};
MazeMap.cpp
#include <iostream>
#include "MazeMap.h"
#include "Public.H"
using namespace std;
MazeMap::MazeMap()
{
}
MazeMap::~MazeMap()
{
}
void MazeMap::setMazeMap(int *p,int m,int n)
{
for(int j=0;j<m;j++)
{
for (int k=0;k<n;k++)
{
mazeMapArray[j][k]=*(p+j*n+k);
}
}
m_IRow=m;
m_IColumn=n;
}
void MazeMap::setMazeWall(char c)
{
m_cWall=c;
m_cRoad=' ';
}
void MazeMap::drawMap()
{
for (int i=0;i<m_IRow;i++)
{
for (int j=0;j<m_IColumn;j++)
{
if(mazeMapArray[i][j]==1)
{
cout<<m_cWall;
}
else
{
cout<<m_cRoad;
}
}
cout<<endl;
}
}
MazePerson.h
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include "Public.H"
using namespace std;
class MazePerson
{
public:
MazePerson();
~MazePerson();
void setMap(int *p,int m,int n);
void setPersonPos(int currentX,int currrentY); //声明 设定人位置的方法
void setPersonChar(char c); //声明 设定人符号的方法
void setPersonSpeed(int v); //声明 设定人速度的方法
void savePrePosition(); //保存前一个坐标位置
void moveSouth(); //声明 向南走的方法
void moveNorth(); //声明 向北走的方法
void moveWest(); //声明 向西走的方法
void moveEast(); //声明 向东走的方法
void turnLeft(); //声明 向左转的方法
void turnRight(); //声明 向右转的方法
void goAhead(); //声明 向当前方向前进的方法?
bool moveForward(); //声明 可以进行主观判断如何前进的方法
void gotoxy(int x,int y); //声明 设定输出位置的方法
void drawPerson(); //声明 人的行走动画的方法
bool checkWallOrNot(int mazeX, int mazeY);
bool checkMazeDoor(int mazeX,int mazeY);
void start();
private:
int m_iPreX;
int m_iPreY;
int m_iCurX;
int m_iCurY;
int m_iManSpeed;
char m_cManPic;
bool m_bOutorNot;
int m_mazeArray[10][10];
int m_mazeWidth;
int m_mazeHeight;
objectiveDirections m_forward; //声明逻辑的前方
};
MazePerson.cpp
#include "MazePerson.h"
#include "MazeMap.h"
MazePerson::MazePerson()
{
m_iCurX = 1;
m_iCurY = 7;
m_iPreX = 10000;
m_iPreY = 10000;
m_iManSpeed = FAST;
m_forward = NORTH;
m_bOutorNot = false;
}
MazePerson::~MazePerson()
{
}
void MazePerson::setMap(int *p,int m,int n)
{
for(int j=0;j<m;j++)
{
for (int k=0;k<n;k++)
{
m_mazeArray[j][k]=*(p+j*n+k);
}
}
m_mazeWidth = n;
m_mazeHeight = m;
}
void MazePerson::setPersonPos(int coordinateX,int coordinateY)
{
m_iCurX = coordinateX;
m_iCurY = coordinateY;
}
void MazePerson::setPersonChar(char personChar)
{
m_cManPic = personChar;
}
void MazePerson::setPersonSpeed(int speed)
{
m_iManSpeed = speed * 50;
}
void MazePerson::savePrePosition()
{
m_iPreX = m_iCurX;
m_iPreY = m_iCurY;
}
void MazePerson::moveSouth()
{
savePrePosition();
m_iCurY = m_iCurY+1;
}
void MazePerson::moveNorth()
{
savePrePosition();
m_iCurY = m_iCurY-1;
}
void MazePerson::moveWest()
{
savePrePosition();
m_iCurX = m_iCurX-1;
}
void MazePerson::moveEast()
{
savePrePosition();
m_iCurX = m_iCurX+1;
}
/*
* 函数名称:gotoxy
* 函数功能:确定控制台中字符的输出位置
* 函数列表:
* x:横坐标
* y:纵坐标
*/
void MazePerson::gotoxy(int x,int y)
{
COORD cd;
cd.X =x;
cd.Y =y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//从一个特定的标准设备(标准输出)中取得一个句柄(用来标识不同设备的数值)。
SetConsoleCursorPosition(handle,cd);//设置控制台光标位置;
}
void MazePerson::drawPerson()
{
//在当前位置上绘制人
char mazeRoad = ' ';
gotoxy(m_iPreX,m_iPreY); //去光标上一个位置
cout << mazeRoad; //在光标上一个位置画空格
gotoxy(m_iCurX,m_iCurY);
cout << m_cManPic;
//为制作动画效果使用时间延迟
Sleep(m_iManSpeed);
}
void MazePerson::turnLeft()
{
switch(m_forward)
{
case NORTH: m_forward = WEST; break;
case WEST: m_forward = SOUTH; break;
case SOUTH: m_forward = EAST; break;
case EAST: m_forward = NORTH; break;
default: cout << "方向错误。" << endl;
}
}
void MazePerson::turnRight()
{
switch(m_forward)
{
case NORTH: m_forward = EAST; break;
case WEST: m_forward = NORTH; break;
case SOUTH: m_forward = WEST; break;
case EAST: m_forward = SOUTH; break;
default: cout << "方向错误。" << endl;
}
}
void MazePerson::goAhead()
{
switch(m_forward)
{
case NORTH: moveNorth(); break;
case WEST: moveWest(); break;
case SOUTH: moveSouth(); break;
case EAST: moveEast(); break;
default: cout << "方向不明,无法前进。" << endl;
}
//形成动画
drawPerson();
//检测是否到达出口
//如果到达出口,则提示
if(checkMazeDoor(m_iCurX,m_iCurY))
{
gotoxy(0,18);//光标移动到最下面
cout << "哈哈,终于出来啦!" << endl;
m_bOutorNot = true;
}
}
void MazePerson::start()
{
int i = 0;
while(!m_bOutorNot)
{
gotoxy(0,13);//光标移动到较最下面
cout << i;
i++;
moveForward();
}
}
bool MazePerson::checkWallOrNot(int mazeX, int mazeY)
{
if(m_mazeArray[mazeY][mazeX] == 1)
{
return true;
}
else
{
return false;
}
}
bool MazePerson::checkMazeDoor(int mazeX,int mazeY)
{
if(mazeX == 0 || mazeX == (m_mazeWidth - 1)||mazeY == 0 || mazeY == (m_mazeHeight - 1))
{
if(m_mazeArray[mazeY][mazeX] == ROAD)
{
return true;
}
}
else return false;
}
bool MazePerson::moveForward()
{
//判断如果前一个位置的坐标是10000,则直接在当前位置绘制人(描绘人的起点)
if(m_iPreX == 10000 || m_iPreY == 10000)
{
gotoxy(m_iCurX,m_iCurY);
cout << m_cManPic;
Sleep(1000);
}
//检查左侧是否是墙,如果是墙,检查前方是否是墙,如果不是墙则前进,前进后检查是否是出口
int leftX;
int leftY;
int forwardX;
int forwardY;
switch(m_forward)
{
case SOUTH: leftX = m_iCurX + 1; leftY = m_iCurY;
forwardX = m_iCurX; forwardY = m_iCurY + 1; break;
case NORTH: leftX = m_iCurX - 1; leftY = m_iCurY;
forwardX = m_iCurX; forwardY = m_iCurY - 1; break;
case WEST: leftX = m_iCurX; leftY = m_iCurY + 1;
forwardX = m_iCurX - 1; forwardY = m_iCurY; break;
case EAST: leftX = m_iCurX; leftY = m_iCurY - 1;
forwardX = m_iCurX + 1; forwardY = m_iCurY; break;
}
if(checkWallOrNot(leftX,leftY))
{
//检测前方也是墙壁
if(checkWallOrNot(forwardX,forwardY))
{
//按逆时针换一个方向
turnRight();
return false;
}
//检测前方是通路
else
{
//向前移动
goAhead();
return true;
}
}
//如果左侧是通路
else
{
Sleep(1000);
turnLeft();
goAhead();
return true;
}
}
demo.cpp
#include <iostream>//和命名空间是一对
#include <stdlib.h>//system函数
#include "MazeMap.h"
#include "MazePerson.h"
using namespace std;
int main()
{
int map[8][9]={
{WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL},
{WALL,ROAD,ROAD,ROAD,ROAD,ROAD,WALL,ROAD,WALL},
{WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
{WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
{WALL,WALL,ROAD,ROAD,ROAD,ROAD,WALL,ROAD,WALL},
{WALL,WALL,ROAD,WALL,WALL,ROAD,ROAD,ROAD,WALL},
{WALL,ROAD,ROAD,WALL,WALL,ROAD,WALL,ROAD,ROAD},
{WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,WALL},
};
MazeMap maze;
maze.setMazeMap(&map[0][0],8,9);
maze.setMazeWall('@');
maze.drawMap();
MazePerson mazer;
mazer.setMap(&map[0][0],8,9);
mazer.setPersonPos(1,7);
mazer.setPersonSpeed(FAST);
mazer.setPersonChar('z');
mazer.start();
system("pause");
return 0;
}
补充:
指针申请二维数组:
int **p=new int*[m];//动态申请二维数组m*n;
for(int i=0;i<m;i++)
{
p[i]=new int[n];
}
访问下标为[i][j]
的:
*(p+i*n+j)= ;