java迷宫队列实现_老鼠走迷宫(队列实现)

1.[文件] Game.cpp ~ 2KB     下载(133)

// 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;

}

2.[文件] Game.h ~ 695B     下载(89)

// Game.h: interface for the Game class.

//

//

#include "Mouse.h"

#if !defined(AFX_GAME_H__18374A6D_B86D_41F9_A7AD_006E4240E462__INCLUDED_)

#define AFX_GAME_H__18374A6D_B86D_41F9_A7AD_006E4240E462__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

class Game

{

private:

Mouse mouse;

public:

Game();

virtual ~Game();

void init_map();//初始化地图

void show_map();//显示地图

void start();//开始游戏

bool test_bound(int x , int y);//测试坐标是否合法

};

#endif // !defined(AFX_GAME_H__18374A6D_B86D_41F9_A7AD_006E4240E462__INCLUDED_)

3.[文件] main.cpp ~ 282B     下载(89)

#include

#include "Game.h"

#include "My_Stack.h"

using namespace std;

int main()

{

Game game;

game.init_map();

game.start();

/*My_Stack m;

for(int i= 0 ; i<23 ; i++)

m.push(i);

for(int j=0 ; j<23 ; j++)

cout<

return 0;

}

4.[文件] Mouse.cpp ~ 1KB     下载(90)

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

//

//

#include "Mouse.h"

//

// Construction/Destruction

//

extern const int LEFT = 0;

extern const int RIGHT=1;

extern const int DOWN = 2;

extern const int UP =3;

Mouse::Mouse(int x, int y)

{

this->x=x;

this->y=y;

}

Mouse::~Mouse()

{

}

//往左走

void Mouse::goLeft()

{

y--;

remember(LEFT);

}

//往右走

void Mouse::goRight()

{

y++;

remember(RIGHT);

}

//往上走

void Mouse::goUp()

{

x--;

remember(UP);

}

//往下走

void Mouse::goDown()

{

x++;

remember(DOWN);

}

//执行大脑记忆

void Mouse::remember(int x)

{

brain.push(x);

}

//执行回想

int Mouse::reBack()

{

int t= brain.pop();

switch(t)

{

case 0:

y++;

break;

case 1:

y--;

break;

case 2:

x--;

break;

case 3:

x++;

break;

}

return t;

}

5.[文件] Mouse.h ~ 839B     下载(89)

// Mouse.h: interface for the Mouse class.

//

//

#include "My_Stack.h"

#if !defined(AFX_MOUSE_H__30C35BDF_E6F1_4860_AB56_9B958FBCC740__INCLUDED_)

#define AFX_MOUSE_H__30C35BDF_E6F1_4860_AB56_9B958FBCC740__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#define WIDTH16

#define HEIGHT 26

class Mouse

{

int x;

int y;

My_Stack brain;

public:

Mouse(int x , int y);

virtual ~Mouse();

void goLeft();//往左走

void goRight();//往右走

void goUp();//往上走

void goDown();//往下走

void remember(int x);//执行大脑记忆

int reBack();//执行回想

int getX(){return x;};

int getY(){return y;};

};

#endif // !defined(AFX_MOUSE_H__30C35BDF_E6F1_4860_AB56_9B958FBCC740__INCLUDED_)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值