Java老鼠走迷宫流程图_老鼠走迷宫(队列实现)

1.[文件]

Game.cpp ~ 2KB

下载(134)

// Game.cpp: implementation of the Game class.

//

//

#include "Game.h"

#include

#include

using namespace std;

char map[WIDTH][HEIGHT] ;

const char WAY = ' ';//路

const char WALL= '#';//墙

const char MOUSE = '&';//老鼠

const char DOOR = '>';//门

const char PASS = '.';//走过的路

const char DEAD_PASS='X';//死路

const int LEFT = 0;

const int RIGHT=1;

const int DOWN = 2;

const int UP =3;

//

// Construction/Destruction

//

Game::Game():mouse(0,2)

{

}

Game::~Game()

{

}

//启动游戏,初始化地图

void Game::init_map()

{

srand(time(NULL));

for(int i = 0 ; i

{

for(int j= 0 ; j

{

if(i==0 || i==WIDTH-1 || j==0 || j==HEIGHT-1)

map[i][j] = WALL;

else

map[i][j] = rand()%4==0?WALL:WAY;

}

}

map[0][2]=MOUSE;

map[WIDTH-1][HEIGHT-2]=DOOR;

}

//显示地图

void Game::show_map()

{

system("cls");

for(int i = 0 ; i

{

for(int j= 0 ; j

{

cout<< map[i][j];

}

cout<

}

}

//开始游戏

void Game::start()

{

int x,y;

while(1)//mouse.getX()!=WIDTH-1 &&mouse.getY()!=HEIGHT-2

{

x=mouse.getX();

y=mouse.getY();

map[x][y]=MOUSE;

show_map();

if((map[x][y+1]!=WALL && map[x][y+1]!=DEAD_PASS &&map[x][y+1]!=PASS)&& test_bound(x,y+1)==true)

{

map[x][y]=PASS;

mouse.goRight();

}

else if((map[x+1][y]!=WALL && map[x+1][y]!=DEAD_PASS&&map[x+1][y]!=PASS)&& test_bound(x+1,y)==true)

{

map[x][y]=PASS;

mouse.goDown();

}

else if((map[x][y-1]!=WALL && map[x][y-1]!=DEAD_PASS&&map[x][y-1]!=PASS)&& test_bound(x,y-1)==true)

{

map[x][y]=PASS;

mouse.goLeft();

}

else if((map[x-1][y]!=WALL && map[x-1][y]!=DEAD_PASS && map[x-1][y]!=PASS)&& test_bound(x-1,y)==true)

{

map[x][y]=PASS;

mouse.goUp();

}

else

{

map[x][y]=DEAD_PASS;

int rb=mouse.reBack();

if(rb==-1)

break;

}

if(mouse.getX()==WIDTH-1 && mouse.getY()==HEIGHT-2)

break;

}

if(mouse.getX()==WIDTH-1 && mouse.getY()==HEIGHT-2)

cout<

else

cout<

}

//测试坐标是否合法

bool Game::test_bound(int x , int y)

{

if(x>0 && x0 && y

return true;

return false;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值