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

这是我自己写的迷宫程序,发现我的程序有什么问题,请多多指教

标签:
C++

迷宫

ifndef MAZE_H
define MAZE_H

//#include"Person.h"
class Maze {
private:
char wall;
public:
char m_cmap[10][10];
Maze();
void setMap(char map[][10]);
void drawMap();
bool inBorder(int x,int y);
char setWall(char c);
};

endif

ifndef PERSON_H
define PERSON_H
include<Windows.h>
include"Maze.h"

class Person
{
private:
char c;//表示人的字符
int direction;//方向
COORD pos;//新位置
COORD lastPos;//旧位置
int count=0;//记步数
int sd;//速度
public:
enum e{W,S,A,D,Q,fast=10, medium=100, slow=200};
Maze maze;
Person();
void setPosition(int ix,int iy);
void setSpeed(int i);
void setChar(char cc);
void getSpeed()
{
return Sleep(sd);
}
void start();
void move();
void turn();
void go(int x, int y);
void setDirection(e e1) { direction = e1; }
};

endif

迷宫类的定义

include"Maze.h"
include<iostream>
include<sstream>

using std::cout;
using std::endl;
Maze::Maze()
{
wall = '*';
for (int i = 0; i < 10; ++i)
for (int j=0; j < 10; ++j)
m_cmap[i][j] = wall;
}
void Maze::setMap(char map[][10])
{
for (int i = 0; i < 10; ++i)
{
for (int j=0; j < 10; ++j)
if (map[i][j] == 0)
m_cmap[i][j] = ' ';
else
m_cmap[i][j] = wall;
}

}
void Maze::drawMap()
{
for (int i = 0; i < 10; ++i)
{
for (int j=0; j < 10; ++j)
cout << m_cmap[i][j];
cout << endl;
}
}
bool Maze::inBorder(int x, int y)
{
if (x < 10 && y < 10 && x >= 0 && y >= 0)
return true;
else
return false;
}
char Maze::setWall(char c)
{
return wall = c;
}

人类的定义

include<iostream>
include"Person.h"
include<Windows.h>
include"Maze.h"

using std::cout;
using std::endl;
Person::Person() {}
void Person::setPosition(int ix, int iy)
{
pos.X = ix;
pos.Y = iy;
lastPos.X = ix;
lastPos.Y = iy;
go(pos.X, pos.Y);
cout << c;
}
void Person::setSpeed(int i)
{
sd = i;
}
void Person::setChar(char cc)
{
c = cc;
}
void Person::turn()
{
switch (direction)
{
case W://往北
{
if (!maze.inBorder( pos.Y-1,pos.X))
{
direction = Q;
break;
}
else if (maze.m_cmap[pos.Y][pos.X + 1] == ' ' && maze.inBorder(pos.Y, pos.X + 1))//往右转 卡成翔 为什呢maze.m_cmap[pos.X + 1]不是maze.m_cmap[pos.X]?索引到对应的位置
{ //全部都不符合 函数是go?? 我换了xy就好了一点点为什么吖?按道理不是这样xy吗 你想想【3】【9】在哪里最下面啊就是入口那里?不是第3+1行9+1列?=下就是说光标那个函数刚好和我的数组的xy相反了?
lastPos.X=pos.X;
lastPos.Y=pos.Y;
pos.X += 1;
++count;
direction = D;
break;
}
else if (maze.m_cmap[pos.Y - 1][pos.X] == ' ' && maze.inBorder(pos.Y -1,pos.X )) {
lastPos.X=pos.X;
lastPos.Y=pos.Y;
pos.Y -= 1;
++count;
direction = W;
break;
}
else if (maze.m_cmap[pos.Y][pos.X - 1] == ' ' && maze.inBorder( pos.Y,pos.X - 1)) {
lastPos.X=pos.X;
lastPos.Y=pos.Y;
pos.X -= 1;
++count;
direction = A;
break;
}
else if(maze.inBorder(pos.Y + 1,pos.X )){
lastPos.X=pos.X;
lastPos.Y=pos.Y;
pos.Y += 1;
++count;
direction = S;
break;
}
}
case D: //往东
{
if (!maze.inBorder(pos.X+1, pos.Y))
{
direction = Q;
break;
}
else if (maze.m_cmap[pos.Y+1][pos.X] == ' ' && maze.inBorder( pos.Y+1 ,pos.X))
{
lastPos.X=pos.X;
lastPos.Y=pos.Y;
pos.Y += 1;
++count;
direction = S;
break;
}
else if (maze.m_cmap[pos.Y][pos.X+1] == ' ' && maze.inBorder(pos.Y, pos.X+1)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.X+= 1;
direction = D;
break;
}
else if (maze.m_cmap[pos.Y-1][pos.X] == ' ' && maze.inBorder(pos.Y-1,pos.X )) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.Y -= 1;
direction = W;
break;
}
else if (maze.inBorder(pos.Y, pos.X-1)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.X-= 1;
direction = A;
break;
}
else { break; }
}//
case S://往南
{
if (!maze.inBorder(pos.X, pos.Y+1))
{
direction = Q;
break;
}
else if (maze.m_cmap[pos.Y][pos.X-1] == ' ' && maze.inBorder(pos.Y,pos.X - 1 ))
{
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.X -= 1;
direction = A;
break;
}
else if (maze.m_cmap[pos.Y + 1][pos.X] == ' ' && maze.inBorder(pos.Y + 1, pos.X)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.Y += 1;
direction = S;
break;
}
else if (maze.m_cmap[pos.Y][pos.X + 1] == ' ' && maze.inBorder(pos.Y,pos.X + 1 )) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.X += 1;
direction = D;
break;
}
else if (maze.inBorder(pos.Y -1, pos.X)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.Y -= 1;
direction = W;
break;
}
else { pos.Y += 1; break; }
}
case A: //往西
{
if (!maze.inBorder(pos.X-1, pos.Y))
{
direction = Q;
break;
}
else if (maze.m_cmap[pos.Y-1][pos.X] == ' ' && maze.inBorder(pos.Y-1,pos.X ))
{
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.Y -= 1;
direction = W;
break;
}
else if (maze.m_cmap[pos.Y][pos.X-1] == ' ' && maze.inBorder(pos.Y, pos.X-1)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.X -= 1;
direction = A;
break;
}
else if (maze.m_cmap[pos.Y+1][pos.X] == ' ' && maze.inBorder(pos.Y+1, pos.X)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.Y += 1;
direction = S;
break;
}
else if (maze.inBorder( pos.Y,pos.X+1)) {
lastPos.X=pos.X;lastPos.Y=pos.Y;++count;
pos.X += 1;
direction = D;
break;
}
else { pos.X -= 1; break; }
}
case Q: //出口
break;
}
}
void Person::move()//人移动
{
go(lastPos.X, lastPos.Y);
getSpeed();cout << " ";
go(pos.X, pos.Y);
getSpeed();cout << c;
go(0, 20); cout << "Step number: " << count;getSpeed();
lastPos.X = pos.X; lastPos.Y = pos.Y;
}
void Person::start()//开始走
{
while (direction!=Q)
{
turn();
move();
}
}
void Person::go(int x, int y) //定位光标并隐藏
{
COORD cd;
cd.X = x;
cd.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle, cd);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}

调试程序

include<iostream>
include<stdlib.h>
include<string>
include<Windows.h>
include"Maze.h"
include"Person.h"

using std::cout;
using std::endl;
void main()
{
char map[10][10] =
{
{1,1,1,1,0,1,1,1,1,1},
{1,1,1,1,0,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,0,1,1,1,1,1,0,1},
{1,1,0,1,1,1,1,0,0,1},
{1,1,0,0,0,0,1,0,1,1},
{1,1,1,1,1,0,0,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,0,1,0,1,0,1,0,1,1},
{1,0,1,1,1,1,1,1,1,1},
};
Person p;
p.maze.setWall('!');
p.maze.setMap(map);
p.maze.drawMap();
p.setChar('T');
p.setPosition(1, 9);
p.setSpeed(p.medium);//中速
p.setDirection(p.W);
p.start();
cout << endl;
p.go(0, 21);
cout << "祝贺走出迷宫" << endl;
system("pause");
}

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消