c和c++学了快一个学期了,感觉也编不出什么像样的程序,不是解决数学问题就是打印图形,感觉没什么劲。今天找了一个有趣的题目来做做,这是imooc上老师布置的一个案例。先不管算法好坏,来实现一个人走出迷宫的程序,要求能在屏幕上显示出人在走迷宫,并显示出走了几步。
第一次写这种面向对象的程序,很多地方写得都比较臃肿orz,枚举类型也不太会用,实践中遇到了各种各样的困难,暴露了我好多知识的漏洞,但最终还是一一克服了,这种感觉真的是挺好的,我以后还是要多做一些这样的实践才行,其实编这样的程序真的挺有趣的hh
定义两个类,分别来表示人和迷宫。迷宫用数组来表示,迷宫的墙用1来表示,路用0来表示。
小人通过右手法则走迷宫,每次先判断右手边的是不是墙,如果不是就往右走,否则就往左转,再继续判断右手边是否为墙,一直循环,直到走出迷宫为止。
头文件maze.h 用来定义枚举类型方向和速度,常量路、墙,还有坐标的结构体
#pragma once
enum direction { up, right, down, left }; //方向
enum speed { FAST = 100, NORMAL = 300, SLOW = 500 }; //速度
const int ROAD = 0; //路
const int WALL = 1; //墙
struct POX { //坐标
int x;
int y;
};
头文件MyMazer.h 用来声明人类
#pragma once
#include "maze.h"
#include "MyMazeMap.h"
class MyMazeMap;
class MyMazer
{
public:
MyMazer(int x = 0, int y = 0, speed s = NORMAL, direction d = direction::right, char Person_Char = 'T');
//构造函数,初始化数据成员
void setPersonPosition(int x, int y); //设置人物位置
void setPersonDirection(direction d); //设置人物朝向
void setPersonSpeed(speed s); //设置人物速度
void setPersonChar(char c); //设置人物符号
void setMap(MyMazeMap *m); //设置地图对象
void start(); //开始游戏
void turn(int d = 0); //转向 d=0顺时针 d=1逆时针
void gotoxy(int x, int y); //移动光标到(x,y)
void move(); //向前移动人物移动
void display(); //更新人物在屏幕上的位置
direction getManRightSide(); //返回人物的右侧方向
private:
char m_cPerson; //人物字符
direction m_dDirection; //人物朝向
speed m_sSpeed; //人物速度
POX m_sPosition; //人物当前位置
POX m_sLastPosition; //人物上次的位置,为了制作动画效果
MyMazeMap *m_pcMap; //地图对象
int m_iSteps; //步数
};
头文件MyMazeMap.h 用来声明迷宫类
#pragma once
#include "maze.h"
#include "MyMazer.h"
class MyMazeMap
{
public:
MyMazeMap(); //构造函数,初始化数据成员
void setMazeMap(int* map, int r, int c); //设置地图,高,宽
void setMazeWall(char wall); //设置墙的符号
void drawMap(); //绘制地图
bool isMazeBound(int x, int y, direction d); //判断(x,y)的d方向上是否是墙
bool isBound(int x, int y); //判断(x,y)是否是墙
bool isOut(int x, int y); //判断是否走出迷宫
int getR(); //返回地图高
int getC(); //返回的图宽
private:
char m_cWall; //墙的符号
char m_cRoad; //路的符号
int m_r; //高
int m_c; //宽
int* m_piMap; //地图
};
源文件MyMazer.cpp 用来实现MyMazer类的成员函数
#include "MyMazer.h"
#include
#include
MyMazer::MyMazer(int x, int y, speed s, direction d, char Person_Char) :m_sPosition{ x,y }, m_sSpeed(s), m_dDirection(d), m_cPerson(Person_Char)
{ //初始化
m_sLastPosition = m_sPosition;
m_pcMap = NULL;
m_iSteps = 0;
}
void MyMazer::setPersonPosition(int x, int y)
{ //设置人物的位置
m_sPosition.x = x;
m_sPosition.y = y;
}
void MyMazer::setPersonDirection(direction d)
{ //设置人物的朝向
m_dDirection = d;
}
void MyMazer::setPersonSpeed(speed s)
{ //设置人物的速度
m_sSpeed = s;
}
void MyMazer::setPersonChar(char c)
{ //设置人物字符
m_cPerson = c;
}
void MyMazer::setMap(MyMazeMap * m)
{ //设置地图对象
m_pcMap = m;
}
void MyMazer::start()
{ //游戏开始,使用右手规则走迷宫
if (!m_pcMap) {
std::cout << "You haven't set map, please use setMap() to continue" << std::endl;
return;
}
gotoxy(m_sPosition.x, m_sPosition.y);
std::cout << m_cPerson;
while (1) {
while(m_pcMap->isMazeBound(m_sPosition.x, m_sPosition.y, getManRightSide())) {
turn(1);
}
turn();
move();
if (m_pcMap->isOut(m_sPosition.x, m_sPosition.y)) break;
display();
}
gotoxy(0, m_pcMap->getR());
std::cout << "闯关成功!!!" << std::endl << "总共走了" << m_iSteps << "步" << std::endl;
}
void MyMazer::turn(int d)
{
if (d == 0) { //顺时针转向
if (m_dDirection == direction::up) m_dDirection = direction::right;
else if (m_dDirection == direction::right) m_dDirection = direction::down;
else if (m_dDirection == direction::down) m_dDirection = direction::left;
else m_dDirection = direction::up;
}
else { //逆时针转向
if (m_dDirection == direction::up) m_dDirection = direction::left;
else if (m_dDirection == direction::left) m_dDirection = direction::down;
else if (m_dDirection == direction::down) m_dDirection = direction::right;
else m_dDirection = direction::up;
}
}
void MyMazer::gotoxy(int x, int y)
{ //将光标移动至(x,y)
if (x < 0 || y < 0) return;
COORD cd;
cd.X = x;
cd.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle, cd);
}
void MyMazer::move()
{ //向前移动一步
m_sLastPosition = m_sPosition;
switch (m_dDirection) {
case direction::up: m_sPosition.y--; break;
case direction::right: m_sPosition.x++; break;
case direction::down: m_sPosition.y++; break;
case direction::left: m_sPosition.x--; break;
}
m_iSteps++;
}
void MyMazer::display()
{ //显示函数
Sleep(m_sSpeed);
gotoxy(m_sLastPosition.x, m_sLastPosition.y);
std::cout << " ";
gotoxy(m_sPosition.x, m_sPosition.y);
std::cout << m_cPerson;
gotoxy(0, m_pcMap->getR());
std::cout << "已经走了" << m_iSteps << "步了" << std::endl;
}
direction MyMazer::getManRightSide()
{ //读取人物朝向右侧的方向
direction ret;
switch (m_dDirection) {
case direction::up:ret = direction::right; break;
case direction::right:ret = direction::down; break;
case direction::down:ret = direction::left; break;
case direction::left:ret = direction::up; break;
}
return ret;
}
源文件MyMazeMap.cpp 用来实现MyMazeMap类的成员函数
#include "MyMazeMap.h"
#include
#include
MyMazeMap::MyMazeMap()
{ //初始化数据成员
m_c = 0;
m_r = 0;
m_cRoad = ' ';
m_cWall = '*';
}
void MyMazeMap::setMazeMap(int * map, int r, int c)
{ //设置迷宫
m_piMap = map;
m_r = r;
m_c = c;
}
void MyMazeMap::setMazeWall(char wall)
{ //设置表示墙的符号
m_cWall = wall;
}
void MyMazeMap::drawMap()
{ //绘制地图
system("cls");
for (int i = 0; i < m_r; i++) {
for (int j = 0; j < m_c; j++) {
if (*(m_piMap + m_c*i + j) == WALL) std::cout << m_cWall;
else std::cout << m_cRoad;
}
std::cout << std::endl;
}
}
bool MyMazeMap::isMazeBound(int x, int y, direction d)
{ //判断面前的是不是墙
bool Bound = false;
switch (d) {
case direction::up:
if (isBound(x, y - 1)) Bound = true;
break;
case direction::right:
if (isBound(x + 1, y)) Bound = true;
break;
case direction::down:
if (isBound(x, y + 1)) Bound = true;
break;
case direction::left:
if (isBound(x - 1, y)) Bound = true;
break;
}
return Bound;
}
bool MyMazeMap::isBound(int x, int y)
{ //判断(x,y)是不是墙
if (isOut(x,y)) return false;
if (*(m_piMap + m_c*y + x) == WALL) return true;
return false;
}
bool MyMazeMap::isOut(int x, int y)
{ //判断是否走出迷宫
if (x > m_c - 1 || x<0 || y> m_r - 1 || y < 0) return true;
return false;
}
int MyMazeMap::getR()
{ //返回迷宫的行数
return m_r;
}
int MyMazeMap::getC()
{ //返回迷宫的列数
return m_c;
}
最后就是源文件maze.cpp了 main函数在这里呢
#include
#include
#include "maze.h"
#include "MyMazeMap.h"
#include "MyMazer.h"
using namespace std;
const int SUCCESS = 0;
int main()
{
int map[10][18] = {
{ WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL, },
{ WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,WALL,ROAD,ROAD,ROAD,ROAD,WALL,WALL,WALL,WALL,WALL, },
{ WALL,ROAD,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,WALL,ROAD,WALL, },
{ WALL,WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,WALL,ROAD,WALL, },
{ WALL,WALL,ROAD,ROAD,ROAD,ROAD,ROAD,ROAD,ROAD,ROAD,ROAD,WALL,ROAD,WALL,ROAD,ROAD,ROAD,WALL, },
{ WALL,WALL,ROAD,WALL,ROAD,WALL,WALL,WALL,ROAD,WALL,ROAD,ROAD,ROAD,WALL,ROAD,WALL,WALL,WALL, },
{ WALL,ROAD,ROAD,WALL,ROAD,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,WALL, },
{ WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,WALL, },
{ WALL,ROAD,WALL,WALL,ROAD,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,ROAD,ROAD,ROAD,WALL,WALL,WALL, },
{ WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL, },
};
MyMazeMap maze;
maze.setMazeMap(&map[0][0], 10, 18); //设置地图
maze.drawMap(); //绘制地图
MyMazer mazer;
mazer.setPersonPosition(16, 2); //设置开始时人物位置
mazer.setPersonDirection(direction::up); //设置开始时人物朝向 up right down left
mazer.setPersonSpeed(FAST); //设置速度 FAST NORMAL SLOW
mazer.setPersonChar('#'); //设置表示人的字符
mazer.setMap(&maze); //设置人物所在地图
mazer.start(); //游戏开始
system("pause");
return SUCCESS;
}