推箱子c++实现代码

本文详细介绍了如何使用C++编程语言实现经典的推箱子游戏。通过构建游戏地图、定义游戏规则和玩家操作,展示了一套完整的推箱子游戏逻辑。通过对代码的解析,读者可以理解游戏的核心算法和数据结构设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<p>c++  实验课的作业,话说这次算是我花的时间最长的实验课作业了,不过的确感觉自己对搜索的理解和编码能力提高了,作为一名初学者,我要学的东西还很多。。。</p><p>这次的实验是推箱子,自己YY了一个自动生成推箱子地图的算法,两重BFS,这大概也是我第一次把ACM的算法放到了实际应用中吧。下面上代码。。。。</p>
class Box
	{
	public :
		Box();
		void RandomMaze(int n);
	public :
		int x,y;
	};


class game
	{
	public:
		int WaitKey( double sec );
		void SetPos( int x, int y );
	};

typedef int Status;
typedef int ElemType;
//using namespace std;

class map
    {
    public:
        Status RandomMaze(int n,int m);
        Status PrintMaze(int n) ;
    public:
        int mazemap[20][20];
    };



//using namespace std;

class Vehicle
    {
    public:
        Vehicle();
        void show();
        void LoadMap(const char *a);
    public:
        int x,y;
    };



#include "Windows.h"
#include "game.h"
#include "ctime"
#include "conio.h"
using namespace std;

int game::WaitKey( double sec )
{
    clock_t tickStart = clock();
    clock_t tickEnd = tickStart + (clock_t)(sec * CLOCKS_PER_SEC);
    while(!kbhit())
    {
        // time out
        if( clock() >= tickEnd)
            return 0;
        Sleep(sec / 100.0); // wait
    }
    return getch();
}


void game::SetPos( int x, int y )
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD cursorPos = {x, y};
    SetConsoleCursorPosition(hConsole, cursorPos);
}



#include "box.h"
#include "iostream"
#include "map.h"
#include "vehicle.h"
#include "ctime"
#include "Windows.h"
typedef int Status;
typedef int ElemType;

#define OK        1
#define ERROR     0
#define TRUE      1
#define FALSE     0
#define OVERFLOW -2


Status map::RandomMaze(int n,int m)
{
    int i,j,k;
    srand(time(NULL));
    for(i=0;i<n;i++)
        mazemap[0][i]=mazemap[n-1][i]=1;
    for(j=0;j<n;j++)
        mazemap[j][0]=mazemap[j][n-1]=1;
    for(i=1;i<n-1;i++)
        for(j=1;j<n-1;j++)
        {
            k=rand()%6;    //随机生成整张的地图
            if(k==0)
                mazemap[i][j]=1;
            else
                mazemap[i][j]=0;
        }
	/*
	for(i=1;i<=m;i++)
		{
			int rand_x=rand()%19;
			int rand_y=rand()%19;
			if(rand_x>=1&&rand_y>=1)
				{
					mazemap[rand_x][rand_y]=-1;    //随机生成箱子的位置
				}
			else
				i--;
		}*/
	/*
	for(i=1;i<=m;i++)
		{
			int rand_x=rand()%19;
			int rand_y=rand()%19;
			if(rand_x>=1&&rand_y>=1)
				{
					mazemap[rand_x][rand_y]=-3;   //随机生成箱子的目标位置
				}
			else
				i--;
		}*/
    mazemap[1][0]=mazemap[1][1]=0;//标记入口出口
    return OK;
}

Status map::PrintMaze(int n)                //打印最后的足迹
{
    //printf("路径如下:\n");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值