慕课网C++课程封装篇作业,自动走迷宫;
https://www.imooc.com/learn/405
遇到的问题与解决方案:
1.在构建地图的MyMazeMap类和小人移动的MyMazer类的交互上出现问题。
解决方案:在MyMazer类中在堆中建立MyMazeMap类对象map,使用拷贝构造函数将主程序中的建立的maze对象拷贝进入MyMazer类对象map中,使得构建地图的成员和成员函数可以被利用到MyMazer的方法中。
即通过map.()
2.光标的移动方式与迷宫建立的坐标系的差异
使用头文件windows.h中的光标相关操作。
void
MyMazer::gotoxy(int x, int y)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = y;
pos.Y = x;
SetConsoleCursorPosition(hOut, pos);
}
光标的移动方式是以,屏幕水平方向为x轴,竖直方向为y轴。
迷宫的建立方式则是以,屏幕水平方向为y轴,竖直方向为x轴。
int map[8][9] = {
{WALL,WALL,WALL,WALL,WALL,WALL,WALL,ROAD,WALL},
{WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
{WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
{WALL,WALL,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,WALL,WALL,WALL,WALL},
{WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,WALL}
};
所以在以上光标移动函数gotoxy中将,x,y对调输入。
3.行动方式选择
解决方法:转向函数+上下左右前进函数
上下左右前进函数负责在抹去原来位置的人,并且在他旁边指定方向的一格处输出新的人。
转向函数则是通过走迷宫的左手法则,即无论在什么方向上都可以进行判断:
是否已在迷宫最外层——是,SUCCESS
|
|
不是,左边是否有墙——没有,左转。
|
|
有,前方是否有墙——没有,前进
|
|
有,右方是否有墙——没有,右转
|
|
有,后退;
通过switch对转向函数的输出做判断,调用前进函数。
4.如何控制速度;
Sleep语句
Sleep(n)(windows.h)
程序暂停n毫秒。
5.如何在程序运行中退出程序
exit(0),正常运行后退出程序。
C++新手,还会继续努力。
代码如下
#include <iostream>
#include <stdlib.h>
#include "MyMazeMap.h"
#include "MyMazer.h"
using namespace std;
const int WALL = 1;
const int ROAD = 0;
const int SLOW = 3;
const int MEDIUM = 2;
const int FAST = 1;
const int SUCCESS = 0;
int main()
{
int map[8][9] = {
{WALL,WALL,WALL,WALL,WALL,WALL,WALL,ROAD,WALL},
{WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
{WALL,WALL,WALL,WALL,ROAD,WALL,WALL,ROAD,WALL},
{WALL,WALL,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,WALL,WALL,WALL,WALL},
{WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL,WALL}
};
MyMazeMap maze;
maze.setMazeMap(&map[0][0], 8, 9);
maze.setMazeWall('*');
//maze.showFactor();
maze.drawMap();
MyMazer mazer;
mazer.map = maze;
mazer.setPersonPosition(7, 1);
mazer.setPersonSpeed(FAST);
mazer.setPersonChar('T');
mazer.start();
cout << endl;
system("pause");
return SUCCESS;
}
#pragma once
#include<iostream>
using namespace std;
class MyMazeMap
{
public:
MyMazeMap();
MyMazeMap(const MyMazeMap& C);
void setMazeMap(int *mazeArr, int x, int y);
void setMazeWall(const char wallSign);
void setMazeWay(const char waySign);
void showFactor();
void drawMap();
bool checkWall(int x, int y);
int getmazeX();
int getmazeY();
char m_wallSign;
char m_waySign;
private:
int m_mazeArr[10][10];
int m_mazeX;
int m_mazeY;
};
#include<iostream>
#include"MyMazeMap.h"
using namespace std;
MyMazeMap::MyMazeMap()
{
m_wallSign = '+';
m_waySign = ' ';
}
MyMazeMap::MyMazeMap(const MyMazeMap &b)
{
m_wallSign=b.m_wallSign;
m_waySign = b.m_waySign;
m_mazeX = b.m_mazeX;
m_mazeY = b.m_mazeY;
for (int i = 0; i < m_mazeX; i++)
{
for (int j = 0; j < m_mazeY; j++) {
m_mazeArr[i][j] = b.m_mazeArr[i][j];
}
}
}
void
MyMazeMap::setMazeMap(int *mazeArr, int x, int y)
{
m_mazeArr[0][0] = *mazeArr;
m_wallSign = '+';
m_waySign = ' ';
int k = 0;
if (x > 10) {
x = 10;
}
if (y > 10) {
y = 10;
k = y - 10;
}
m_mazeX = x;
m_mazeY = y;
for (int i = 0; i < m_mazeX; i++)
{
for (int j = 0; j < m_mazeY; j++) {
m_mazeArr[i][j] = *mazeArr;
*mazeArr++;
}
*mazeArr = *mazeArr+k;
}
}
void
MyMazeMap::setMazeWall(const char wallSign)
{
m_wallSign = wallSign;
}
void
MyMazeMap::setMazeWay(const char waySign)
{
m_waySign = waySign;
}
void
MyMazeMap::showFactor()
{
cout << m_mazeArr[0][0] << endl;
cout << m_mazeX << endl;
cout << m_mazeY << endl;
cout << m_wallSign << " " << m_waySign << endl;
}
void
MyMazeMap::drawMap()
{
for (int i = 0; i < m_mazeX; i++)
{
for (int j = 0; j < m_mazeY; j++) {
if (m_mazeArr[i][j] == 1)
cout << m_wallSign;
else
cout << m_waySign;
}
cout << endl;
}
}
bool
MyMazeMap::checkWall(int x, int y)
{
if (x==-1||y==-1||x==getmazeX()||y==getmazeY())
{
return false;
}
else if (m_mazeArr[x][y] == 1)
{
return true;
}
else return false;
}
int
MyMazeMap::getmazeX()
{
return m_mazeX;
}
int
MyMazeMap::getmazeY()
{
return m_mazeY;
}
#pragma once\
#include<iostream>
#include"MyMazeMap.h"
using namespace std;
class MyMazer
{
public:
MyMazer();
MyMazeMap map;
void setPersonPosition(int x, int y);
void setPersonSpeed(int speed);
void setPersonChar(char personchar);
void start();
private:
int LastPersonX;
int LastPersonY;
int PersonX;
int PersonY;
int SleepTime;
char PersonChar;
int direction;
private:
void gotoxy(int x, int y);
void direction_up();
void direction_down();
void direction_left();
void direction_right();
int turnDirection();
};
#include<iostream>
#include <windows.h>
#include<stdio.h>
#include"MyMazer.h"
#include"MyMazeMap.h"
using namespace std;
const int UP = 0;
const int LEFT = 1;
const int DOWN = 2;
const int RIGHT = 3;
void
MyMazer::gotoxy(int x, int y)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = y;
pos.Y = x;
SetConsoleCursorPosition(hOut, pos);
}
MyMazer::MyMazer()
{
direction = UP;
}
void
MyMazer::setPersonPosition(int x, int y)
{
PersonX = x;
PersonY = y;
gotoxy(PersonX, PersonY);
}
void
MyMazer::setPersonSpeed(int speed)
{
SleepTime = speed * 500;
}
void
MyMazer::setPersonChar(char personchar)
{
PersonChar = personchar;
cout << PersonChar;
}
void
MyMazer::start()
{
while (true)
{
switch (MyMazer::turnDirection())
{
case UP:direction = UP; direction_up(); break;
case RIGHT:direction = RIGHT; direction_right(); break;
case LEFT:direction = LEFT; direction_left(); break;
case DOWN:direction = DOWN; direction_down(); break;
}
}
}
void
MyMazer::direction_up()
{
LastPersonX = PersonX;
LastPersonY = PersonY;
PersonX = PersonX - 1;
gotoxy(LastPersonX, LastPersonY);
cout << map.m_waySign;
gotoxy(PersonX, PersonY);
cout << PersonChar;
Sleep(SleepTime);
}
void
MyMazer::direction_down()
{
LastPersonX = PersonX;
LastPersonY = PersonY;
PersonX = PersonX + 1;
gotoxy(LastPersonX, LastPersonY);
cout << map.m_waySign;
gotoxy(PersonX, PersonY);
cout << PersonChar;
Sleep(SleepTime);
}
void
MyMazer::direction_left()
{
LastPersonX = PersonX;
LastPersonY = PersonY;
PersonY = PersonY-1;
gotoxy(LastPersonX, LastPersonY);
cout << map.m_waySign;
gotoxy(PersonX, PersonY);
cout << PersonChar;
Sleep(SleepTime);
}
void
MyMazer::direction_right()
{
LastPersonX = PersonX;
LastPersonY = PersonY;
PersonY = PersonY + 1;
gotoxy(LastPersonX, LastPersonY);
cout << map.m_waySign;
gotoxy(PersonX, PersonY);
cout << PersonChar;
Sleep(SleepTime);
}
int
MyMazer::turnDirection()
{
if (direction == UP)
{
if (PersonX == 0)
{
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << "successful" << endl;
system("pause");
exit(0);
}
if (map.checkWall(PersonX,PersonY-1))
{
if (!map.checkWall(PersonX - 1, PersonY))
return UP;
else if (!map.checkWall(PersonX, PersonY + 1))
return RIGHT;
else
return DOWN;
}
else
return LEFT;
}
if (direction == RIGHT)
{
if (PersonY == map.getmazeY())
{
cout << endl << endl << endl << endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout << "successful" << endl;
system("pause");
exit(0);
}
if (map.checkWall(PersonX-1, PersonY))
{
if (!map.checkWall(PersonX, PersonY + 1))
return RIGHT;
else if (!map.checkWall(PersonX + 1, PersonY))
return DOWN;
else
return LEFT;
}
else
return UP;
}
if (direction == DOWN)
{
if (PersonX == map.getmazeX())
{
cout << endl << endl << endl;
cout << "successful" << endl;
system("pause");
exit(0);
}
if (map.checkWall(PersonX, PersonY+1))
{
if (!map.checkWall(PersonX+1, PersonY))
return DOWN;
else if (!map.checkWall(PersonX, PersonY-1))
return LEFT;
else
return UP;
}
else
return RIGHT;
}
if (direction == LEFT)
{
if (PersonY == 0)
{
cout << endl << endl << endl;
cout << "successful" << endl;
system("pause");
exit(0);
}
if (map.checkWall(PersonX+1, PersonY ))
{
if (!map.checkWall(PersonX, PersonY-1))
return LEFT;
else if (!map.checkWall(PersonX-1, PersonY))
return UP;
else
return RIGHT;
}
else
return DOWN;
}
}