为了账号安全,请及时绑定邮箱和手机立即绑定

没有文件或目录,就是include"maze.h"

没有文件或目录,就是include"maze.h"

C#
forever丶会 2016-09-14 09:25:51
/maze.h ifndef MAZE_H define MAZE_H include <iostream> include <ctime> include <cstring> using namespace std; const int SEC = CLOCKS_PER_SEC; static const int row = 20;//迷宫大小 static const int column = 20; enum{north=1,south=2,west=3,east=4}; enum{fast=1,slow=2}; enum{wall='0',road=' ',eage='1'}; enum{turnRight,turnLeft,turnBack}; class mazePerson { private: char name; int startx;//开始位置 int starty; int px;//当前位置 int py; int dir;//小人的朝向 public: mazePerson(char name,int sx,int sy,int d); ~mazePerson(); void setDir(int turn); char getName(){return name;}; int getStartx(){return startx;} int getStarty(){return starty;} int getPx(){return px;}; int getPy(){return py;}; int getDir(){return dir;}; void moveFront(); }; class Maze { char map[row][column]; mazePerson per; public: Maze(char arr[row][column],char p,int sx,int sy,int d); Maze(); ~Maze(); void changeMap(); bool move(); bool isStartxy(); bool isExit();//是否到达出口 char getRight();//读取右边信息 char getLeft();//读取左边信息 char getFront();//读取前方信息 //char getRear(); void disMap(); void start(int speed); }; endif // MAZE_H //maze.cpp include "maze.h" static int steps = 0; //结构函数,初始化小人 mazePerson::mazePerson(char n,int sx,int sy,int d) { name = n; startx = sx; starty = sy; px = startx; py = starty; dir = d; } mazePerson::~mazePerson() { } //重新设置小人方向(turnLeft左转, turnBack向后转,else右转 void mazePerson::setDir(int turn) { switch(dir) { case north: if(turn == turnLeft) dir = west; else if(turn == turnBack) dir = south; else dir = east; break; case south: if(turn == turnLeft) dir = east; else if(turn == turnBack) dir = north; else dir = west; break; case west: if(turn == turnLeft) dir = south; else if(turn == turnBack) dir = east; else dir = north; break; case east: if(turn == turnLeft) dir = north; else if(turn == turnBack) dir = west; else dir = south; break; } } //往前移动 void mazePerson::moveFront() { switch(dir) { case north:px--; break; case south:px++; break; case west:py--; break; case east:py++; break; } } //结构函数,初始化迷宫 Maze::Maze(char arr[row][column],char name,int sx,int sy,int d): per(name,sx,sy,d) { int i,j; for(i=0;i<row;i++) strcpy(map[i],arr[i]); map[per.getStartx()][per.getStarty()] = per.getName(); } Maze::~Maze() { cout << "you get it!\n"; } //shifou shi kaishi weizhi bool Maze::isStartxy() { if(per.getStartx()==per.getPx() && per.getStarty()==per.getPy()) return true; else return false; } //判断是否到达出口 bool Maze::isExit() { if(isStartxy()&&steps == 0) return false; else if(getLeft()==eage getRight()==eage getFront()==eage) return true; } //输出迷宫信息(地图和小人位置) void Maze::disMap() { int i,j; for(i=0;i<row;i++) { cout << map[i] << endl; } } //读取小人左右和前方的信息 char Maze::getRight() { switch(per.getDir()) { case north:return map[per.getPx()][per.getPy()+1]; break; case south:return map[per.getPx()][per.getPy()-1]; break; case west:return map[per.getPx()-1][per.getPy()]; break; case east:return map[per.getPx()+1][per.getPy()]; break; } } char Maze::getLeft() { switch(per.getDir()) { case north:return map[per.getPx()][per.getPy()-1]; break; case south:return map[per.getPx()][per.getPy()+1]; break; case west:return map[per.getPx()+1][per.getPy()]; break; case east:return map[per.getPx()-1][per.getPy()]; break; } } char Maze::getFront() { switch(per.getDir()) { case north:return map[per.getPx()-1][per.getPy()]; break; case south:return map[per.getPx()+1][per.getPy()]; break; case west:return map[per.getPx()][per.getPy()-1]; break; case east:return map[per.getPx()][per.getPy()+1]; break; } } //移动小人,并更新迷宫信息 bool Maze::move() { if(getRight()==road)//如果右边有路,右转 {     per.setDir(turnRight);     map[per.getPx()][per.getPy()] = road;     per.moveFront();     changeMap(); } else if(getFront()==road)//如果前方有路,直走 {     map[per.getPx()][per.getPy()] = road;     per.moveFront();     changeMap(); } else if(getLeft()==road) {     per.setDir(turnLeft);//如果左边有路,左转     map[per.getPx()][per.getPy()] = road;     per.moveFront();     changeMap(); } else if(getRight()==wall&&getLeft()==wall&&getFront()==wall) {     per.setDir(turnBack);//如果左右前都是墙向后转     return false; } return true; } //开始走迷宫,speed(fast/slow)代表速度 void Maze::start(int speed) { for(;!isExit();) { system("cls"); if(move()) steps++; disMap(); clock_t time = clock(); for(;clock()-time < speed * SEC / 5;); } if(isStartxy())//如果走回开始位置,地图错误 { cout << "Map error!Dont have exit!"; return; } else cout << "恭喜你,用了" << steps << "步" << endl; } //更新小人位置 void Maze::changeMap() { map[per.getPx()][per.getPy()] = per.getName(); } //main.cpp include "maze.h" /* Maze(char arr[row][column],char p,int sx,int sy,int d); 初始化迷宫类 参数1:地图(自己随便设置,3,4,5参数要自己改) 参数2:表示小人的字符 参数3,4:开始坐标 参数5:小人的朝向 小人默认是靠右走,如果想靠左走, 把maze里面start函数的第一和第三个if语句交换一下 / char per =''; int main(int argc, char *argv[]) { char arr[row][column] = { "1111111111111111111", "100000000000000001", "1000000000 00001", "1000000000 00 00001", "1000000 00 001", "100000 0000000 001", "100000 00000000 001", "10 00 00 001", "10 000000 00 00 001", "10 000000 00 00 001", "10 0 00 00 001", "10 0 000000 00 001", "1 0 0000 000 001", "10 0 0 00 001", "10 0 00 0 00 000001", "10 000 0 00 00001", "1000000 0 000 00001", "10 0 00001", "10 0000000000000001", "1111111111111111111" }; Maze maz(arr,per,12,1,east); maz.start(fast); cout << "Bye!\n"; return 0; }
查看完整描述

1 回答

?
汉江怪人

TA贡献50条经验 获得超11个赞

放在一个工程里了吗,在一个工程的话路径一样吗

查看完整回答
反对 回复 2016-09-14
  • 1 回答
  • 0 关注
  • 1800 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信