日本益智游戏77推箱子26关怎么过三七关怎么过

77577女生小游戏
77577男生小游戏
77577经典小游戏
男生游戏 >
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
女生游戏 >
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
热门游戏 >
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
热门专题 >
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
友情链接(友情链接交换要求:PR&4,百度权重&5,Alexa排名10万以内,日均IP达到20万以上)
(window.slotbydup=window.slotbydup || []).push({
id: '2363059',
container: s,
size: '300,250',
display: 'inlay-fix'  经过四次的修改和优化,终于将推箱子这个游戏完整的写出来了,今天就像大家分享一下这个游戏的编写。
  这个游戏界面的编写总的来说不困难,主要是推动箱子的算法。
  (1)利用数组和windows api 即可写出界面
1 #define N 15
2 #define M 15
3 int map[N][M] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },//0-&空白
{ 0, 0, 0, 0, 1, 0, 2, 1, 1, 1, 0, 0, 0, 0, 0 },//1-&墙
{ 0, 0, 0, 0, 1, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0 },//2-&人
{ 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0 },//3-&箱子
{ 0, 0, 0, 1, 4, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0 },//4-&位置
{ 0, 0, 0, 1, 4, 3, 0, 0, 1, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 4, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
20 void PushBox::Color(int m)//封装到PushBox类里
HANDLE//创建句柄,详细句柄知识,请百度一下或查MSDN
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);//实例化句柄
SetConsoleTextAttribute(consolehwnd, m);
29 void PushBox::Drop(int map[N][M])
for (i = 0; i & N; i++)
for (j = 0; j & M; j++)
switch (map[i][j])
std::cout && "
std::cout && "■"; break;
case 2: Color(10); std::cout && "△"; break;
case 3: Color(5);
std::cout && "□"; break;
case 4: Color(1);
std::cout && "☆"; break;
case 5: Color(7);
std::cout && "◆"; break;//箱子到达目标位置
case 6: Color(10); std::cout && "△"; break;//表示人与位置重叠
std::cout && "\n";
  (2)推箱子算法:本人比较笨,没有找到捷径,所以就穷举了推箱子步骤,分析如下:
     &以人为中心,出现两种可能:①人在空位&②人在目标位置上
       ①有六种可能:(注:x1,y1, x2, y2为坐标的偏移量,i ,为人所在的坐标 )
       
      
&    ②人在目标位置上 同样也有六种可能:
        
    用if语句进行对这12中可能进行判断,除了处理这几种能够移动的外,其他没有可能移动,分析清楚,则很容写出移动算法:
        
        
1 int PushBox::push(int map[N][M],int x1,int x2,int y1,int y2)
Postion(map, &i, &j);
/*******************人在空格处*/
if (map[i][j] == 2)
//人前是箱子,箱子在空格处
if (map[i + x1][j + y1] == 3)
//箱子前面为空格S
if (map[i + x2][j + y2] == 0)
map[i][j] = 0;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 3;
//箱子前面为位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 0;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 5;
//人前为箱子,箱子在位置上
if (map[i + x1][j + y1] == 5)
//箱子前面为空
if (map[i + x2][j + y2] == 0)
map[i + x2][j + y2] = 3;
map[i + x1][j + y1] = 6;
map[i][j] = 0;
//箱子前面为位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 0;
map[i + x1][j + y1] = 6;
map[i + x2][j + y2] = 5;
/*--------------------*/
//人前为空格
if (map[i + x1][j + y1] == 0)
map[i + x1][j + y1] = 2;
map[i][j] = 0;
//人前为位置
if (map[i + x1][j + y1] == 4)
map[i + x1][j + y1] = 6;
map[i][j] = 0;
/*******************人在位置上*/
if (map[i][j] == 6)
//位置前面是箱子,箱子在空格
if (map[i + x1][j + y1] == 3)
//箱子前面为空格
if (map[i + x2][j + y2] == 0)
map[i][j] = 4;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 3;
//箱子前面为位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 4;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 5;
//位置前面是箱子,箱子在位置
if (map[i + x1][j + y1] == 5)
//箱子前面是空格
if (map[i + x2][j + y2] == 0)
map[i][j] = 4;
map[i + x1][j + y1] = 6;
map[i + x2][j + y2] = 3;
//箱子前面是位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 4;
map[i + x1][j + y1] = 6;
map[i + x2][j + y2] = 5;
/*-----------------*/
//人前为位置
if (map[i + x1][j + y1] == 4)
map[i + x1][j + y1] = 6;
map[i][j] = 4;
//人前为空格
if (map[i + x1][j + y1] == 0)
map[i + x1][j + y1] = 2;
map[i][j] = 4;
}return 0;
        
    这里写返回1值既可以减少系统的判断,还可以判断是否执行了移动操作,方便统计移动的步数
    (3)编写获取人的位置函数、判断是否获胜
      获取人的位置,只需要判断得到地图中6或者2其中一个坐标,由于需要横坐标和纵坐标,所以利用指针得到位置
      
void PushBox::Postion(int map[N][M], int *cl, int *cow)
for (i = 0; i & N; i++)
for (j = 0; j & M; j++)
if (map[i][j] == 2 || map[i][j] == 6)goto ML;
    判断是否获胜:即地图中没有目标位置,就获胜,若胜利返回1值,否则返回0;
int PushBox::juide(int map[N][M])
for (i = 0; i & N; i++)
for (j = 0; j & M; j++)
if (map[i][j] == 6)return 0;
if (map[i][j] == 4)return 0;
if (i == N - 1 && j == M - 1)return 1;
&   (4)编写移动方向算法,并统计执行步数
int PushBox::move(int map[N][M], char ch)
static int step = 0;
int x1, x2, y1, y2;
switch (ch)
case 'S': x1 = 1; x2 = 2; y1 = 0; y2 = 0;
if (push(map, x1, x2, y1, y2)) step++; return
case 'W': x1 = -1; x2 = -2; y1 = 0; y2 = 0;
if (push(map,x1,x2,y1,y2)) step++; return
case 'a': x1 = 0; x2 = 0; y1 = -1; y2 = -2;
if (push(map,x1,x2,y1,y2)) step++; return
case 'd': x1 = 0; x2 = 0; y1 = 1; y2 = 2;
if (push(map,x1,x2,y1,y2)) step++; return
  (5)Push类的封装,将以上的几个方法封装到类里,建立头文件
1 #include &iostream&
2 using namespace
3 #include &windows.h&
4 #include &string.h&
5 #include &conio.h&
6 #include &fstream&
7 #pragma warning(disable:4996)
8 #define N 15
9 #define M 15
11 //建立一个推箱子相关操作的类
12 /*--------------------------PushBox类编写--------------------------------------*/
13 /****************************************************************************/
14 class PushBox{
15 public:
int move(int map[N][M], char ch);//移动箱子
void Drop(int map[N][M]);//箱子界面编写
juide(int map[N][M]);//判断是否全部移入位置,成功返回1,失败返回0
19 private:
int push(int map[N][M],int x1,int x2,int y1,int y2);
void Color(int m);
void Postion(int map[N][M], int *i, int *j);
  (6)主函数的编写:这里我将地图放入外部txt文件中,方便以后添加地图
#include &iostream&
using namespace
#include &windows.h&
#include &string.h&
#include &conio.h&
#include "Push.h"
#pragma warning(disable:4996)
#define N 15
#define M 15
int read_map(int *p);
void change_map(int *p, char *temp);
int main()
int map[N][M] = { 0 };
int *p = &map[0][0];
int select = read_map(p);
int step = 0;
cout && "你选择的关卡是:" && select &&
cout && "你走了:" && step && "步";
box.Drop(map);
cout && "W-----向上
S------向下" &&
cout && "A-----向左
S------向右" &&
ch = _getch();
step = box.move(map, ch);
system("cls");
if (box.juide(map))break;
std::cout && "你赢了!";
std::cout && "共走:" && step && "步";
getchar();
getchar();
/*选择关卡*/
int read_map(int *p)
cout && "请输入关卡:";
char temp[15];
switch (ch)
case 1:strcpy(temp, "map/map_1.txt"); change_map(p, temp); system("cls"); return 1;
case 2:strcpy(temp, "map/map_2.txt"); change_map(p, temp); system("cls"); return 1;
/*打开关卡*/
void change_map(int *p, char *temp)
infile.open(temp);
while (!infile.eof())
infile && *p;
infile.close();
  程序分析图:
      
  经过调试运行,暂时还没有发现BUG,一下是源代码,希望大家发现了以后给我留言,写得不好的地方希望大家能够指出
1 /**************************************************
3 * FileName
: PushBox.cpp
4 * Author
5 * Version
7 *Description
: 制作一个简单的推箱子
9 *Function List
: (1)void read_map(int *p);
(2)void change_map(int *p, char *temp);
11 --------------
12 History:
13 &author&
&reviseInf&
把类封装到头文件中,重写推函数,添加地图
15 ****************************************************/
18 #include &iostream&
19 using namespace
20 #include &windows.h&
21 #include &string.h&
22 #include &conio.h&
23 #include "Push.h"
24 #pragma warning(disable:4996)
25 #define N 15
26 #define M 15
28 int read_map(int *p);
29 void change_map(int *p, char *temp);
30 //主函数
32 int main()
int map[N][M] = { 0 };
int *p = &map[0][0];
int select = read_map(p);
int step = 0;
cout && "你选择的关卡是:" && select &&
cout && "你走了:" && step && "步";
box.Drop(map);
cout && "W-----向上
S------向下" &&
cout && "A-----向左
S------向右" &&
ch = _getch();
step = box.move(map, ch);
system("cls");
if (box.juide(map))break;
std::cout && "你赢了!";
std::cout && "共走:" && step && "步";
getchar();
getchar();
60 /*选择关卡*/
61 int read_map(int *p)
cout && "请输入关卡:";
char temp[15];
switch (ch)
case 1:strcpy(temp, "map/map_1.txt"); change_map(p, temp); system("cls"); return 1;
case 2:strcpy(temp, "map/map_2.txt"); change_map(p, temp); system("cls"); return 1;
74 /*打开关卡*/
75 void change_map(int *p, char *temp)
infile.open(temp);
while (!infile.eof())
infile && *p;
infile.close();
 头文件   
1 #include &iostream&
2 using namespace
3 #include &windows.h&
4 #include &string.h&
5 #include &conio.h&
6 #include &fstream&
7 #pragma warning(disable:4996)
8 #define N 15
9 #define M 15
11 //建立一个推箱子相关操作的类
12 /*--------------------------PushBox类编写--------------------------------------*/
13 /****************************************************************************/
14 class PushBox{
15 public:
int move(int map[N][M], char ch);//移动箱子
void Drop(int map[N][M]);//箱子界面编写
juide(int map[N][M]);//判断是否全部移入位置,成功返回1,失败返回0
19 private:
int push(int map[N][M],int x1,int x2,int y1,int y2);
void Color(int m);
void Postion(int map[N][M], int *i, int *j);
25 int PushBox::move(int map[N][M], char ch)
static int step = 0;
int x1, x2, y1, y2;
switch (ch)
case 'S': x1 = 1; x2 = 2; y1 = 0; y2 = 0;
if (push(map, x1, x2, y1, y2)) step++; return
case 'W': x1 = -1; x2 = -2; y1 = 0; y2 = 0;
if (push(map,x1,x2,y1,y2)) step++; return
case 'a': x1 = 0; x2 = 0; y1 = -1; y2 = -2;
if (push(map,x1,x2,y1,y2)) step++; return
case 'd': x1 = 0; x2 = 0; y1 = 1; y2 = 2;
if (push(map,x1,x2,y1,y2)) step++; return
49 void PushBox::Drop(int map[N][M])
for (i = 0; i & N; i++)
for (j = 0; j & M; j++)
switch (map[i][j])
std::cout && "
std::cout && "■"; break;
case 2: Color(10); std::cout && "△"; break;
case 3: Color(5);
std::cout && "□"; break;
case 4: Color(1);
std::cout && "☆"; break;
case 5: Color(7);
std::cout && "◆"; break;
case 6: Color(10); std::cout && "△"; break;
std::cout && "\n";
70 int PushBox::juide(int map[N][M])
for (i = 0; i & N; i++)
for (j = 0; j & M; j++)
if (map[i][j] == 6)return 0;
if (map[i][j] == 4)return 0;
if (i == N - 1 && j == M - 1)return 1;
85 int PushBox::push(int map[N][M],int x1,int x2,int y1,int y2)
Postion(map, &i, &j);
/*******************人在空格处*/
if (map[i][j] == 2)
//人前是箱子,箱子在空格处
if (map[i + x1][j + y1] == 3)
//箱子前面为空格S
if (map[i + x2][j + y2] == 0)
map[i][j] = 0;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 3;
//箱子前面为位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 0;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 5;
//人前为箱子,箱子在位置上
if (map[i + x1][j + y1] == 5)
//箱子前面为空
if (map[i + x2][j + y2] == 0)
map[i + x2][j + y2] = 3;
map[i + x1][j + y1] = 6;
map[i][j] = 0;
//箱子前面为位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 0;
map[i + x1][j + y1] = 6;
map[i + x2][j + y2] = 5;
/*--------------------*/
//人前为空格
if (map[i + x1][j + y1] == 0)
map[i + x1][j + y1] = 2;
map[i][j] = 0;
//人前为位置
if (map[i + x1][j + y1] == 4)
map[i + x1][j + y1] = 6;
map[i][j] = 0;
/*******************人在位置上*/
if (map[i][j] == 6)
//位置前面是箱子,箱子在空格
if (map[i + x1][j + y1] == 3)
//箱子前面为空格
if (map[i + x2][j + y2] == 0)
map[i][j] = 4;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 3;
//箱子前面为位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 4;
map[i + x1][j + y1] = 2;
map[i + x2][j + y2] = 5;
//位置前面是箱子,箱子在位置
if (map[i + x1][j + y1] == 5)
//箱子前面是空格
if (map[i + x2][j + y2] == 0)
map[i][j] = 4;
map[i + x1][j + y1] = 6;
map[i + x2][j + y2] = 3;
//箱子前面是位置
if (map[i + x2][j + y2] == 4)
map[i][j] = 4;
map[i + x1][j + y1] = 6;
map[i + x2][j + y2] = 5;
/*-----------------*/
//人前为位置
if (map[i + x1][j + y1] == 4)
map[i + x1][j + y1] = 6;
map[i][j] = 4;
//人前为空格
if (map[i + x1][j + y1] == 0)
map[i + x1][j + y1] = 2;
map[i][j] = 4;
}return 0;
213 void PushBox::Postion(int map[N][M], int *cl, int *cow)
for (i = 0; i & N; i++)
for (j = 0; j & M; j++)
if (map[i][j] == 2 || map[i][j] == 6)goto ML;
228 void PushBox::Color(int m)
HANDLE//创建句柄,详细句柄知识,请百度一下或查MSDN
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);//实例化句柄
SetConsoleTextAttribute(consolehwnd, m);
两个地图张
&文件放置如图:
阅读(...) 评论()分享给朋友:推箱子 第77关下载至电脑扫码用手机看用或微信扫码在手机上继续观看二维码2小时内有效推箱子 第77关扫码用手机继续看用或微信扫码在手机上继续观看二维码2小时内有效,扫码后可分享给好友没有优酷APP?立即下载请根据您的设备选择下载版本
药品服务许可证(京)-经营- 请使用者仔细阅读优酷、、、Copyright(C)2017 优酷
版权所有不良信息举报电话:}

我要回帖

更多关于 推箱子14关怎么过 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信