首页 > 编程资源分享区 > C/C++源代码共享 > [C语言]实现走迷宫地图!
2006
10-09

[C语言]实现走迷宫地图!

#include <iostream>
#include <conio.h>

using namespace std;

void SelectSort(int * iArray,int iSize);


//record the Position
typedef struct _tagPoint
{
int x;
int y;
}Point;

//reloaded the != operator
bool operator !=(const Point & pt0,const Point & pt1)
{
return !((pt0.x == pt1.x) && pt0.y == pt1.y);
}

//define the Direction
enum enumDirect{dirUp = 8,dirDown = 2,dirLeft = 4,dirRight = 6};

//Show your Position
void ShowPosition(const Point & ptPos)
{
cout << “your position is row:”
<< ptPos.x + 1
<< ” col:”
<< ptPos.y + 1
<< endl;
}

//check the Road can move
bool CanMove(Point ptTmp,char * cMap,int iRow,int iCol)
{
return *(cMap + ptTmp.x * iCol + ptTmp.y) == 0;
}

//display the Map
void ShowMap(Point ptTmp,char * cMap,int iRow,int iCol)
{
for(int i = 0;i < iRow;i++)
{
for(int j = 0;j < iCol;j++)
{
if((i == ptTmp.x) && (j == ptTmp.y))
cout << “^-^”;
else
cout << ” ” << *(cMap + i * iCol + j) << ” “;
}
cout << endl;
}
}

void main(void)
{

char cMap[9][10] = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,0,1},
{1,0,1,0,1,1,0,1,0,1},
{1,0,0,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1}};
Point ptStart = {8,1};
Point ptEnd = {8,6};
Point ptPos = {-1,-1};
int iDirect = dirDown;
bool bStart = true;

while(ptPos != ptEnd && ptPos != ptStart)
{

//clear the screen
system(“cls”);

//show the map
ShowMap(ptPos,(char *)cMap,9,10);

//give the tip
cout << “Are you Move to?” << endl;
cout << “8– UP, 2– Down, 4– Left, 6– Right!” << endl;

//show the current position
ShowPosition(ptPos);

//get user input for direction
iDirect = getch() – ’0′;

if(bStart)
{//when start the motion set position in start
if(iDirect == dirDown)
{
cout << “\07″;
cout << “Can’t Move!” << endl;
continue;
}
//set position at entry
ptPos = ptStart;
bStart = false;
}
Point ptTmp = ptPos;
switch(iDirect)
{//process the direction
case dirUp:
ptTmp.x–;
break;
case dirDown:
ptTmp.x++;
break;
case dirLeft:
ptTmp.y–;
break;
case dirRight:
ptTmp.y++;
break;
}
if(CanMove(ptTmp,(char *)cMap,9,10))
{//the direction can move
ptPos = ptTmp;
ShowPosition(ptPos);
cout << “\07″;
}
else
{
cout << “\07\07\07\07\07″;
cout << “Can’t Move!” << endl;
}
}
cout << “Greeting! you out!” << endl;


}


[C语言]实现走迷宫地图!》有 10 条评论

  1. 雪 狼 狐 说:

    一直都没有可以直接运行的程序

    希望你的可以

    哎!我去试试

    要行  我好好顶!!!

  2. 雪 狼 狐 说:

    怒 !!

    怎么还是不能运行啊

    我不过是想研究代码 

    学习一下  然后自己就多懂一些

    为什么啊?!

  3. jinfan1009 说:

    楼主能告诉我一下你的邮箱吗?

  4. lnhaing 说:

    我的 可以运行的 啊

  5. amen 说:

    都是高手啊!!!

  6. 120992162 说:

    高手,学习中“`挺长的啊.

  7. weike9786 说:

    可以运行的

  8. wxjeacen 说:

    写的太垃圾了

     

    如果用回溯递归,比这个简单不知道多少倍了.

     

    基本的算法。

  9. leixu21 说:

    是可以运行的

  10. chengzi12130 说:

    为毛没注释,看着好费劲!!!加注释啊,楼主。。。

留下一个回复