📄 mazegame.cpp
字号:
#include "StdAfx.h"
#include ".\mazegame.h"
MazeGame::MazeGame(void)
{
column=0;
row=0;
}
MazeGame::~MazeGame(void)
{
for(int i=0;i<row+2;i++)
{
delete []MazeMap[i];
}
}
//////////////////////////////////////////////////////////////////
////////// First initialize some member viriable by th file input
void MazeGame::start()
{
ifstream in;
in.open("Maze.txt");
if(!in.is_open())
cout<<"Count open file."<<endl;
/////////////////////////////////////////////////
///////////// Find the row number of the maze
while(in)
{
string tag;
in>>tag;
if(tag=="column")
in>>column;
else if(tag=="row")
in>>row;
else if(tag=="Extrance")
{
char temp;
in>>temp;
in>>extrance.row;
in>>temp;
in>>extrance.col;
in>>temp;
}
else if(tag=="Exit")
{
char temp;
in>>temp;
in>>exit.row;
in>>temp;
in>>exit.col;
in>>temp;
}
else
break;
}
MazeMap=new int*[row+2];
for(int i=0;i<row+2;i++)
{
MazeMap[i]=new int[column+2];
}
initialMazeMap(in);
printPath(exit);
}
//////////////////////////////////////////////////////////////////////
////////// Input the Maze to a 2D array.
void MazeGame::initialMazeMap(ifstream& in)
{
for(int i=0;i<row+2;i++)
for(int j=0;j<column+2;j++)
{
if(i==0||i==row+1||j==0||j==column+1)
MazeMap[i][j]=1;
else
{
if(in)
in>>MazeMap[i][j];
}
}
in.close();
}
bool MazeGame::printPath(Point pCurrent)
{
if(pCurrent.col==extrance.col&&pCurrent.row==extrance.row)
{
cout<<"("<<pCurrent.row<<","<<pCurrent.col<<")"<<endl;
return true;
}
////////////////////////
/////////// Go north(up)
{
pCurrent.goNorth(); //Set the pCurrent to the north point
if(MazeMap[pCurrent.row][pCurrent.col]==0)
{
MazeMap[pCurrent.row+1][pCurrent.col]=1; // Set the current point to '1' to avoid turn back to this point;
if(printPath(pCurrent))
{
cout<<"("<<pCurrent.row+1<<","<<pCurrent.col<<")"<<endl; //Out put the point if there is a path to the exit.
return true;
}
pCurrent.goSouth();
MazeMap[pCurrent.row][pCurrent.col]=0; //Recover the point state from '1' to '0'
}
else
pCurrent.goSouth(); //Recover the pCurrent
}
////////////////////////
/////////// Go south(down)
{
pCurrent.goSouth();
if(MazeMap[pCurrent.row][pCurrent.col]==0)
{
MazeMap[pCurrent.row-1][pCurrent.col]=1;
if(printPath(pCurrent))
{
cout<<"("<<pCurrent.row-1<<","<<pCurrent.col<<")"<<endl;
return true;
}
pCurrent.goNorth();
MazeMap[pCurrent.row][pCurrent.col]=0;
}
else
pCurrent.goNorth();
}
////////////////////////
/////////// Go east(right)
{
pCurrent.goEast();
if(MazeMap[pCurrent.row][pCurrent.col]==0)
{
MazeMap[pCurrent.row][pCurrent.col-1]=1;
if(printPath(pCurrent))
{
cout<<"("<<pCurrent.row<<","<<pCurrent.col-1<<")"<<endl;
return true;
}
pCurrent.goWest();
MazeMap[pCurrent.row][pCurrent.col]=0;
}
else
pCurrent.goWest();
}
////////////////////////
/////////// Go west(left)
{
pCurrent.goWest();
if(MazeMap[pCurrent.row][pCurrent.col]==0)
{
MazeMap[pCurrent.row][pCurrent.col+1]=1;
if(printPath(pCurrent))
{
cout<<"("<<pCurrent.row<<","<<pCurrent.col+1<<")"<<endl;
return true;
}
pCurrent.goEast();
MazeMap[pCurrent.row][pCurrent.col]=0;
}
else
pCurrent.goEast();
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -