写在开始:这个博客建于大二下学期。2年多的学习,从网上借鉴的大牛经验,代码,指导数不胜数,而其中大部分来自别人的博客,于是期待有一天也能把自己在学习过程中的一些经验拿出来与大家分享。后来大概觉得离大牛太远,就没敢再奢望指导别人,反而希望有大牛能偶尔看到,给予指导,也算不负初衷。
其实我凝望了C++已经快2年了,也没见它回望于我。标题叫新手村的复活点,大概是因为在开始的地方就一直被小问题打倒吧。
下面是最近两天写的一个小程序,算是对C++的一个小小的复习,这个小游戏已经拖了半年,已经忘记了当时写它的初衷。
以下是main.cpp
#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.h
#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.cpp
#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; }
代码经过验证,运行截图如下