個人第一篇博文:C++新手村的復活點-致敬經典-小遊戲走迷宮

寫在開始:這個博客建於大二下學期。2年多的學習,從網上借鑑的大牛經驗,代碼,指導數不勝數,而其中大部分來自別人的博客,因而期待有一天也能把本身在學習過程當中的一些經驗拿出來與你們分享。後來大概以爲離大牛太遠,就沒敢再奢望指導別人,反而但願有大牛能偶爾看到,給予指導,也算不負初衷。ios

其實我凝望了C++已經快2年了,也沒見它回望於我。標題叫新手村的復活點,大概是由於在開始的地方就一直被小問題打倒吧。算法

下面是最近兩天寫的一個小程序,算是對C++的一個小小的複習,這個小遊戲已經拖了半年,已經忘記了當時寫它的初衷。小程序

如下是main.cppwindows

#include <iostream>
#include <windows.h>
#include <string>
#include "mazeMap.h"
#include "mazePerson.h"

using namespace std;

int main()
{
    int Map[8][8] = {//要求地圖入口在最下面,出口在最右面
        {0,0,0,0,0,0,0,0},//map[0][0:7]
        {0,0,1,1,0,0,0,0},//map[1][0:7]
        {0,0,0,1,1,1,1,1},//map[2][0:7]
        {0,1,1,0,0,1,0,0},//map[3][0:7]
        {0,0,1,0,0,1,1,0},//map[4][0:7]
        {0,1,1,0,1,1,0,0},//map[5][0:7]
        {0,1,1,1,1,0,0,0},//map[6][0:7]
        {0,0,1,0,0,0,0,0} //map[7][0:7]
    };
    mazeMap map1;
    map1.setmazeMap((int **)Map,8,8);//傳入地圖數組參數
    map1.setmazeWall('*');//設置牆的樣式
    mazePerson People(map1);
    Faceto faceto;
    faceto = _up;
    People.setPersonSpeed(MID);//設置移動速度
    People.setPersonShap('T');//設置人物樣式
    People.start(faceto);
    return 0;
}

如下是mazeMap.h數組

#ifndef MAZEMAP_H_INCLUDED
#define MAZEMAP_H_INCLUDED
#include <string>
const bool Wall(0);

class mazeMap{
public:
    mazeMap();
    mazeMap(const mazeMap &_mazeMap);
    ~mazeMap();
    void setmazeMap(int **_mazeArr,int _col,int _row);
    void setmazeWall(char _wall);
    void drawMap();
    void detTerpoint(int *originPoint,int *destination);
    int getCol();
    int getROw();
    int **getArradd();
    char m_chWall;
    char m_chRoad = ' ';
private:

    int m_iCol;
    int m_iRow;
    int m_iOriginpoint[2];
    int m_iTerpoint[2];
    int **m_mazeArr;
};


#endif // MAZEMAP_H_INCLUDED

如下是mazeMap.cpp學習

#include <iostream>
#include <string>
#include "mazeMap.h"
using namespace std;

mazeMap::mazeMap()
{
    m_chWall = ' ';
    m_chRoad = ' ';
    m_iCol = 0;
    m_iRow = 0;
    m_iOriginpoint[0] = 0;
    m_iOriginpoint[1] = 0;
    m_iTerpoint[0] = 0;
    m_iTerpoint[1] = 0;
    m_mazeArr = NULL;
}

mazeMap::mazeMap(const mazeMap &_mazeMap)
{
    m_iCol = _mazeMap.m_iCol;
    m_iRow = _mazeMap.m_iRow;
    m_chRoad = _mazeMap.m_chRoad;
    m_chWall = _mazeMap.m_chWall;
    m_mazeArr = _mazeMap.m_mazeArr;
    m_iOriginpoint[0] = _mazeMap.m_iOriginpoint[0];
    m_iOriginpoint[1] = _mazeMap.m_iOriginpoint[1];
    m_iTerpoint[0] = _mazeMap.m_iTerpoint[0];
    m_iTerpoint[1] = _mazeMap.m_iTerpoint[1];
}

mazeMap::~mazeMap()
{
   for (int i = 0;i < m_iRow;i++)
   {
       delete []m_mazeArr[i];
       m_mazeArr[i] = NULL;
   }
}
void mazeMap::setmazeMap(int **mazeArr,int _col,int _row)
{
    m_mazeArr = mazeArr;
    m_iCol = _col;
    m_iRow = _row;
}

void mazeMap::setmazeWall(char _wall)
{
    m_chWall = _wall;
}

void mazeMap::detTerpoint(int *originPoint,int *destination)//迷宮地圖入口在最下面,出口在最上面
{
    int i = 0;
    int j = m_iRow-1;
    for (i = 0;i<m_iCol;i++)//出口在最右邊一列
    {
       if((*(m_mazeArr + m_iCol*i +j))!=Wall)
       {
           m_iTerpoint[0] = i;
           m_iTerpoint[1] = j;
       }
    }
    i = m_iCol-1;
    for (j = 0;j<m_iRow;j++)
    {
       if((*(m_mazeArr + m_iCol*i +j))!=Wall)
       {
           m_iOriginpoint[0] = i;
           m_iOriginpoint[1] = j;
       }
    }
    destination[0] = m_iTerpoint[0];
    destination[1] = m_iTerpoint[1];
    originPoint[0] = m_iOriginpoint[0];
    originPoint[1] = m_iOriginpoint[1];

}

void mazeMap::drawMap()
{
    for (int i = 0;i < m_iCol;i++)
    {
        for (int j = 0;j < m_iRow;j++)
        {
            if (*(m_mazeArr + m_iCol*i + j) == Wall)
            {
                cout<<m_chWall;
            }
            else
            {
                cout<<m_chRoad;
            }
        }
        cout<<endl;
    }
}

int mazeMap::getCol()
{
    return m_iCol;
}

int mazeMap::getROw()
{
    return m_iRow;
}

int **mazeMap::getArradd()
{
    return m_mazeArr;
}

 

如下是mazePerson.hspa

#ifndef MAZEPERSON_H_INCLUDED
#define MAZEPERSON_H_INCLUDED
#include <string>
#include <iostream>
#include <windows.h>
#include "mazeMap.h"
using namespace std;
enum Faceto{_up,_down,_left,_right};
enum Speed{LOW,MID,FAST};
class mazePerson{
public:
    mazePerson();
    mazePerson(mazeMap _mazeMap);
    ~mazePerson();
    void setPersonPosition(int _row,int _col);
    void setPersonSpeed(Speed _speed);
    void setPersonShap(char _Person);
    Faceto turn(Faceto _faceto);
    void start(Faceto _faceto);
    void gotoyx(int x,int y);
    mazeMap m_mazeMap;
private:
    char m_chPerson;
    Faceto m_Faceto;
    int m_iNowLocal[2];
    int m_iPreviousLocal[2];
    int destination[2] = {0,0};
    int originPoint[2] = {0,0};
    Speed m_Speed;
};

#endif // MAZEPERSON_H_INCLUDED

 

如下是mazePerson.cppcode

#include <iostream>
#include <string>
#include <windows.h>
#include "mazePerson.h"
using namespace std;

mazePerson::mazePerson()
{
    m_chPerson = 'Y';
    m_Faceto = _up;
    m_Speed = LOW;
    m_iNowLocal[0] = 0;//
    m_iNowLocal[1] = 1;//
    m_iPreviousLocal[0] = 0;
    m_iPreviousLocal[1] = 0;
    mazeMap m_mazeMap;
}

mazePerson::mazePerson(mazeMap _mazeMap)
{
    m_chPerson = 'Y';
    m_Faceto = _up;
    m_Speed = LOW;
    m_iNowLocal[0] = 0;//coord X
    m_iNowLocal[1] = 1;//coord Y
    m_iPreviousLocal[0] = 0;
    m_iPreviousLocal[1] = 0;
    m_mazeMap = _mazeMap;
    m_mazeMap.drawMap();
    m_mazeMap.detTerpoint(originPoint,destination);
}

mazePerson::~mazePerson()
{
//    delete []m_iNowLocal;
//    m_iNowLocal = NULL;
//    delete []m_iPreviousLocal;
//    m_iPreviousLocal = NULL;
}

void mazePerson::setPersonPosition(int _row,int _col)
{
    m_iNowLocal[0] = _row;
    m_iNowLocal[1] = _col;
}

void mazePerson::setPersonSpeed(Speed _speed)
{
    m_Speed = _speed;
}

void mazePerson::setPersonShap(char _Person)
{
    m_chPerson = _Person;
}

Faceto mazePerson::turn(Faceto _faceto)//基於右手算法,定義下一步往哪走
{
    m_Faceto = _faceto;
    if(m_Faceto==_up)
    {
        if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]+1))!=Wall)//先判斷右邊界
        {
            m_Faceto = _right;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]-1) + m_iNowLocal[1]))!=Wall)//再判斷上邊界
        {
            m_Faceto = _up;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]-1))!=Wall)//再判斷左邊界
        {
            m_Faceto = _left;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]+1) + m_iNowLocal[1]))!=Wall)//再判斷下邊界
        {
            m_Faceto = _left;
        }
    }
    else if(m_Faceto==_right)
    {
        if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]+1) + m_iNowLocal[1]))!=Wall)//先判斷下邊界
        {
            m_Faceto = _down;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]+1))!=Wall)//再判斷右邊界
        {
            m_Faceto = _right;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]-1) + m_iNowLocal[1]))!=Wall)//再判斷上邊界
        {
            m_Faceto = _up;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]-1))!=Wall)//再判斷左邊界
        {
            m_Faceto = _left;
        }
    }
    else if(m_Faceto==_down)
    {
        if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]-1))!=Wall)//先判斷左邊界
        {
            m_Faceto = _down;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]+1) + m_iNowLocal[1]))!=Wall)//再判斷下邊界
        {
            m_Faceto = _right;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]+1))!=Wall)//再判斷右邊界
        {
            m_Faceto = _up;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]-1) + m_iNowLocal[1]))!=Wall)//再判斷上邊界
        {
            m_Faceto = _up;
        }
    }
    else if(m_Faceto==_left)
    {
        if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]-1) + m_iNowLocal[1]))!=Wall)//先判斷上邊界
        {
            m_Faceto = _up;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]-1))!=Wall)//再判斷左邊界
        {
            m_Faceto = _left;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*(m_iNowLocal[0]+1) + m_iNowLocal[1]))!=Wall)//再判斷下邊界
        {
            m_Faceto = _down;
        }
        else if((*(m_mazeMap.getArradd() + m_mazeMap.getCol()*m_iNowLocal[0] + m_iNowLocal[1]+1))!=Wall)//再判斷右邊界
        {
            m_Faceto = _up;
        }
    }

    return m_Faceto;
}

void mazePerson::start(Faceto _faceto)
{
    char tempchPer = 'Y';
    tempchPer = m_chPerson;
    m_iNowLocal[0]=originPoint[0];
    m_iNowLocal[1]=originPoint[1];
    int speed_num = 0;
    if(m_Speed == LOW)
    {
        speed_num = 2;
    }
    else if(m_Speed == MID)
    {
        speed_num = 1;
    }
    else if(m_Speed == FAST)
    {
        speed_num = 0.5;
    }
    m_Faceto = _faceto;
    //畫第一個點的位置
    m_iPreviousLocal[0] = m_iNowLocal[0];
    m_iPreviousLocal[1] = m_iNowLocal[1];
    setPersonShap(' ');
    gotoyx(m_iPreviousLocal[1],m_iPreviousLocal[0]);
    _sleep(0.05*1000);//delay 0.1s
    setPersonShap(tempchPer);
    gotoyx(m_iNowLocal[1],m_iNowLocal[0]);
    _sleep(speed_num*1000);//delay 1s
    while(1)
    {
        m_iPreviousLocal[0] = m_iNowLocal[0];
        m_iPreviousLocal[1] = m_iNowLocal[1];
        switch(m_Faceto)
           {
                case 0://_up
                {
                    m_iNowLocal[0]=m_iNowLocal[0]-1;
                    break;
                }
                case 1://_down
                {
                    m_iNowLocal[0]=m_iNowLocal[0]+1;
                    break;
                }
                case 2://_left
                {
                    m_iNowLocal[1]=m_iNowLocal[1]-1;
                    break;
                }
                case 3://_right
                {
                    m_iNowLocal[1]=m_iNowLocal[1]+1;
                    break;
                }
           }

        if((m_iNowLocal[0]==destination[0])&&(m_iNowLocal[1]==destination[1]))
        {
            setPersonShap(' ');
            gotoyx(m_iPreviousLocal[1],m_iPreviousLocal[0]);
            _sleep(0.1*1000);//delay 0.1s
            setPersonShap(tempchPer);
            gotoyx(m_iNowLocal[1],m_iNowLocal[0]);
            _sleep(speed_num*1000);//delay 1s
            break;
        }
        m_Faceto = turn(m_Faceto);
        setPersonShap(' ');
        gotoyx(m_iPreviousLocal[1],m_iPreviousLocal[0]);
        _sleep(0.1*1000);//delay 1s
        setPersonShap(tempchPer);
        gotoyx(m_iNowLocal[1],m_iNowLocal[0]);
        _sleep(speed_num*1000);//delay 1s
    }
    setPersonShap(' ');
    gotoyx(0,m_mazeMap.getROw());
    cout<<endl;
    cout<<"Finally reach terminal point!"<<endl;
}

void mazePerson::gotoyx(int _row,int _col)
{
    COORD cd;
    cd.X = _row;
    cd.Y = _col;
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(handle,cd);
    cout<<m_chPerson;
}

代碼通過驗證,運行截圖以下blog

相關文章
相關標籤/搜索