C++代码实现简单走迷宫(更新)

2017-04-05  本文已影响0人  Gaomi

将迷宫地图静态化,更合理一点。代码改动不大。

//MazePerson.cpp
//By MiGao
#include "MazePerson.h"
#include "MazeMap.h"
#include <iostream>
using namespace std;
MazePerson::MazePerson(char c, int count){
    m_cPerson = c; this->count = count;
}
MazePerson::~MazePerson(){

}
void MazePerson::start(){
    m_iDirection = 0;
    while (m_Posi.Y != 0){
        turn(m_Posi.X, m_Posi.Y);
        move();
    }
    gotoxy(0, 11);
    cout << "Success!" << endl;
}
void MazePerson::move(){
    gotoxy(m_LastPosi.X, m_LastPosi.Y);
    cout << ' '; Sleep(200); count++;
    gotoxy(m_Posi.X, m_Posi.Y); cout << m_cPerson;
    gotoxy(0, 10); cout << "Step number: " << count; Sleep(300);
    m_LastPosi.X = m_Posi.X; m_LastPosi.Y = m_Posi.Y;//renew position
}
void MazePerson::turn(int x, int y){
    int w_x, w_y, a_x, a_y, s_x, s_y, d_x, d_y;//w=↑, a=←, s=↓, d=→
    switch(m_iDirection){
    case 0: w_x = x; w_y = y - 1; a_x = x - 1; a_y = y; s_x = x; s_y = y + 1; d_x = x + 1; d_y = y; break;
    case 1: w_x = x - 1; w_y = y; a_x = x; a_y = y + 1; s_x = x + 1; s_y = y; d_x = x; d_y = y - 1; break;
    case 2: w_x = x; w_y = y + 1; a_x = x + 1; a_y = y; s_x = x; s_y = y - 1; d_x = x - 1; d_y = y; break;
    case 3: w_x = x + 1; w_y = y; a_x = x; a_y = y - 1; s_x = x - 1; s_y = y; d_x = x; d_y = y + 1; break;
    default:;
    }
    if(!MazeMap::inBorder(w_x, w_y) || MazeMap::m_iMap[w_y][w_x] == '*')
        if(MazeMap::m_iMap[d_y][d_x] == '*' || !MazeMap::inBorder(d_x, d_y))
            if(MazeMap::m_iMap[a_y][a_x] == '*' || !MazeMap::inBorder(a_x, a_y)){
                m_iDirection = (m_iDirection + 2) % 4;
                m_Posi.X = s_x; m_Posi.Y = s_y;/*U-Turn*/
            }
            else{
                m_iDirection = (m_iDirection + 1) % 4;
                m_Posi.X = a_x; m_Posi.Y = a_y;/*Turn to left*/
            }
        else{
            m_iDirection = (m_iDirection + 3) % 4;
            m_Posi.X = d_x; m_Posi.Y = d_y;/*Turn to right*/
        }
    else if(MazeMap::m_iMap[d_y][d_x] == ' '){
        m_iDirection = (m_iDirection + 3) % 4;
        m_Posi.X = d_x; m_Posi.Y = d_y;/*Turn to right*/
    }
    else{
        m_Posi.X = w_x;
        m_Posi.Y = w_y;/*forward*/
    }
}
void MazePerson::gotoxy(int x, int y){
    COORD co;
    co.X = x;
    co.Y = y;
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(handle, co);
}
void MazePerson::setPersonPosi(int x, int y){
    m_Posi.X = x; m_Posi.Y = y;
    m_LastPosi.X = x; m_LastPosi.Y = y;
    gotoxy(m_Posi.X, m_Posi.Y);
    cout << m_cPerson;
}
//MazeMap.cpp
//By MiGao
#include "MazeMap.h"
#include <iostream>
using namespace std;
MazeMap::MazeMap(){

}
MazeMap::~MazeMap(){

}
void MazeMap::setMap(int map[][10]){
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
            if(map[i][j] == 0)
                m_iMap[i][j] = ' ';
            else
                m_iMap[i][j] = '*';
}
void MazeMap::drawMap(){
    for (int i = 0; i < 10; i++){
        for (int j = 0; j < 10; j++)
            cout << m_iMap[i][j];
        cout << endl;
    }
}
bool MazeMap::inBorder(int x, int y){
    if(x <10 && y < 10 && x >= 0 && y >= 0)
        return true;
    return false;
}
char MazeMap::m_iMap[10][10] = {0};//static class member
//MazePerson.h
//By MiGao
#include "windows.h"
class MazePerson{
public:
    MazePerson(char c = 'T', int count = 0);
    ~MazePerson();
    void start();
    void turn(int x, int y);
    void gotoxy(int x, int y);
    void setPersonPosi(int x, int y);
    void move();
public:
    char m_cPerson;
    int m_iDirection;
    int count;
    COORD m_Posi;
    COORD m_LastPosi;
};
//MazeMap.h
//By MiGao
class MazeMap{
public:
    MazeMap();
    ~MazeMap();
    void setMap(int map[][10]);
    void drawMap();
    static bool inBorder(int x, int y);
public:
    static char m_iMap[10][10];
};
//maze.cpp
//By MiGao
#include "MazePerson.h"
#include "MazeMap.h"
#include <iostream>
using namespace std;
int main(){
    int map[10][10] ={
        {8,8,8,8,8,8,8,8,0,8},
        {8,8,8,8,8,8,8,8,0,8},
        {8,8,8,8,8,8,8,8,0,8},
        {8,8,8,0,8,8,8,8,0,8},
        {8,8,8,0,8,8,8,8,0,8},
        {8,8,8,0,0,0,0,8,0,8},
        {8,8,0,0,8,8,0,8,0,8},
        {8,8,0,8,8,8,0,0,0,8},
        {8,8,0,8,8,8,8,8,0,8},
        {8,8,0,8,8,8,8,8,8,8},
    };
    MazeMap maze;
    maze.setMap(map);
    maze.drawMap();
    MazePerson mazer;
    mazer.setPersonPosi(2, 9);
    mazer.start();
    system("pause");
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读