最近自己看了慕课网的课,写了迷宫的代码,放在这里。 #include <iostream> #include <windows h=""> #include "MazeMap.h" #include "Mazer.h" using namespace std; #define WALL 0 #define ROAD 1 const int SUCCESS = 0; int main() { int map[8][12] = { { WALL,WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL }, { ROAD, ROAD, ROAD, ROAD, ROAD, WALL, WALL, ROAD, WALL, WALL, ROAD, WALL }, { WALL, WALL, WALL, WALL, ROAD, WALL, WALL, ROAD, WALL, WALL, ROAD, WALL }, { WALL, WALL, WALL, WALL, ROAD, WALL, WALL, ROAD, WALL, WALL, ROAD, WALL }, { WALL, WALL, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, ROAD, WALL, ROAD, WALL }, { WALL, WALL, ROAD, WALL, WALL, ROAD, WALL, WALL, ROAD, ROAD, ROAD, WALL }, { WALL, ROAD, ROAD, WALL, WALL, ROAD, WALL, WALL, WALL, WALL, WALL, WALL }, { WALL, ROAD, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL } }; MyMazeMap maze; maze.setMazeMap(*map, 8, 12);//创建map的一个迷宫 maze.setMazeWall('*');//墙的表示字符 maze.drawMap();//控制台显示迷宫 face_to faceto;//人进入迷宫的朝向 faceto = up; MyMazer mazer(maze);//走迷宫的人的对象 mazer.getMapIntance(7, 1);//迷宫入口位置 mazer.setStarFace_to(up);//设置人开始的朝向 mazer.getSpeed(slow);//设置人行走的速度 mazer.BeginMap();//开始走迷宫 system("pause"); return 0; } </windows></iostream> #ifndef MAZEMAP_H #define MAZEMAP_H #include"Coordinate.h" class MyMazeMap { public: MyMazeMap(); ~MyMazeMap(); void setMazeMap(int *p,int row,int col); void setMazeWall(char c); void setMazeRoad(char cc); void drawMap(); void getIntrance(Coordinate point); int **getMazer_map(); public: int Maze_row; int Maze_col; char wall; char road; int *maze_map; int **m_pMap; Coordinate entrance; }; #endif #include"MazeMap.h" #include<iostream> using namespace std; MyMazeMap::MyMazeMap() { } MyMazeMap::~MyMazeMap() { } void MyMazeMap::setMazeMap(int *pl, int row, int col) { Maze_row = row; Maze_col = col; maze_map = pl; m_pMap = new int*[Maze_row]; for (int i = 0; i < Maze_row; i++) m_pMap[i] = new int[Maze_col]; int *p = pl; for (int i = 0; i < Maze_row; i++) { for (int j = 0; j < Maze_col; j++) { m_pMap[i][j] = *p; p++; } } } void MyMazeMap::setMazeWall(char c) { wall = c; } void MyMazeMap::drawMap() { for (int i = 0; i < Maze_row; ++i) { for (int j = 0; j < Maze_col; ++j) { if (*(maze_map++) == 0) cout << wall << " "; else cout << " "; } cout << endl; } } void MyMazeMap::getIntrance(Coordinate point) { entrance = point; } void MyMazeMap::setMazeRoad(char cc) { road = cc; } int** MyMazeMap::getMazer_map() { return m_pMap; } </iostream> #ifndef MAZER_H #define MAZER_H #include <iostream> #include "MazeMap.h" //#include"Coordinate.h" enum face_to {right,up,left,down }; enum speed{fast,middle,slow}; class MyMazer { public: MyMazer(); ~MyMazer(); MyMazer(MyMazeMap map); Coordinate turnto(face_to myface, int locx, int locy); void setStarFace_to(face_to star_face); void getMapIntance(int x, int y); void BeginMap(); void getSpeed(speed sp); /*void SetPersonPosition(int a, int b);*/ public: MyMazeMap myMap; char person_single; face_to person_faceto; Coordinate person_location; Coordinate Intrance; int **map; speed person_speed; int step_num; }; #endif </iostream> #include"Mazer.h" #include<iostream> #include <windows h=""> //#include "conio.h" //using namespace std; MyMazer::MyMazer() { person_single = 'T'; person_faceto = up; person_location.x=0; person_location.y = 0; step_num = 0; } MyMazer::~MyMazer() { } MyMazer::MyMazer(MyMazeMap map1) { myMap = map1; step_num = 0; map = NULL; map = myMap.getMazer_map(); person_single = 'T'; person_faceto = up; person_location.x = 0; person_location.y = 0; } Coordinate MyMazer::turnto(face_to myface,int locx,int locy) { person_faceto = myface; /*std::cout << "person_face:" << person_faceto << std::endl;*/ int x = locx; int y = locy; //std::cout << "loc_x and loc_y:" << x << "," << y << std::endl; //std::cout << "map_val:" << map[x][y] << std::endl; int row = myMap.Maze_row; int col = myMap.Maze_col; if (person_faceto == up)//如果朝向往上 { if (map[x][y + 1] == 1)//map[x][y+1] { person_location.y = y + 1;//往右走 person_faceto = right;//同时改变朝向 } else if (map[x-1][y] == 1) { person_location.x = x- 1;//往上走 person_faceto = up; } else if (map[x][y - 1] == 1) { person_location.y = y - 1;//往左走 person_faceto = left; } else if (map[x+1][y ] == 1) { person_location.x = x + 1;//往下走 person_faceto = down; } } else if (person_faceto == right) { if (map[x + 1][y] == 1) { person_location.x = x + 1;//往下走 person_faceto = down; } else if (map[x ][y+1] == 1) { person_location.y = y + 1;//往右走 person_faceto = right; } else if (map[x -1][y] == 1) { person_location.x = x - 1;//往上走 person_faceto = up; } else if (map[x ][y-1] == 1) { person_location.y = y - 1;//往左走 person_faceto = left; } } else if (person_faceto == left) { if (map[x - 1][y] == 1) { person_location.x = x - 1;//往上走 person_faceto = up; } else if (map[x][y-1] == 1 ) { person_location.y = y - 1;//往左走 person_faceto = left; } else if (map[x + 1][y] == 1) { person_location.x = x + 1;//往下走 person_faceto = down; } else if (map[x ][y+1] == 1) { person_location.y = y + 1;//往右走 person_faceto = right; } } else if (person_faceto == down) { if (map[x ][y-1] == 1) { person_location.y = y - 1;//往左走 person_faceto = left; } else if (map[x + 1][y] == 1) { person_location.x = x + 1;//往下走 person_faceto = down; } else if (map[x ][y+1] == 1) { person_location.y = y + 1;//往右走 person_faceto = right; } else if (map[x - 1][y] == 1) { person_location.x = x - 1;//往上走 person_faceto = up; } } return person_location; } void MyMazer::setStarFace_to(face_to star_face) { person_faceto = star_face; } void MyMazer::getMapIntance(int x, int y) { Intrance.x = x; Intrance.y = y; } void MyMazer::getSpeed(speed sp) { person_speed = sp; } void MyMazer::BeginMap() { int row = myMap.Maze_row; int col = myMap.Maze_col; person_location = Intrance; int x = person_location.x; int y = person_location.y; int stop_time=0; if (person_speed == fast) stop_time = 600; else if (person_speed == middle) stop_time == 1000; else stop_time == 1400; do { Coordinate loca= turnto(person_faceto,x,y); x = loca.x; y = loca.y; std::cout << x<< "," << y<< std:: endl; step_num++; //刷新一次显示 system("cls"); //显示迷宫图 for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { if (i == x&&j == y) { std::cout << "T "; continue; } if (map[i][j] == 0) std::cout << "* "; else std::cout << " "; } std::cout << std::endl; } Sleep(stop_time); } while (x != 0 && x != row-1 && y != 0 && y != col-1); std::cout << step_num+1; } </windows></iostream> #ifndef COORDINATE_H #define COORDINATE_H class Coordinate { public: Coordinate(){} ~Coordinate(){} Coordinate(int a, int b) { x = a; y = b; } int x; int y; }; #endif