可以指导毕设 — 加我qq 1961958205
推箱子这个游戏都很熟悉了,代码也熟悉了。正好我们下半学期学了栈,就尝试用栈做一个推箱子回退的功能。
返回上一步部分代码如下:
if (Pop(S, x) == 1)
{
//若上一步是向上
if (x == 'W' || x == 'w')
{
count--;
if (x == 'W')
{
a[i + 1][j] += 5;
a[i][j] -= 5;
}
else
{
a[i - 1][j] -= 4;
a[i + 1][j] += 5;
a[i][j] -= 1;
}
}
其它方向的撤回类似。
另外我的主页博客里面有添加了easyx库的推箱子,希望可以帮助大家。
简易版推箱子全部代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h >
#define maxlevel 3
/*墙1
目的地3
箱子4
人5
目的地上的箱子7
站在目的地的人8*/
//所有地图
int a1[maxlevel][13][13] = {
{
{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,1,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,4,0,1,0,1,1,1,0,0},
{0,0,1,0,4,0,1,0,1,3,1,0,0},
{0,0,1,1,1,5,1,1,1,3,1,0,0},
{0,0,0,1,1,4,0,0,0,3,1,0,0},
{0,0,0,1,0,0,0,1,0,0,1,0,0},
{0,0,0,1,0,0,0,1,1,1,1,0,0},
{0,0,0,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,1,1,1,1,1,1,1,0,0,0},
{1,1,1,1,0,0,0,0,0,1,0,0,0},
{1,0,0,0,3,1,1,1,0,1,0,0,0},
{1,0,1,0,1,0,0,0,0,1,1,0,0},
{1,0,1,0,4,0,4,1,3,0,1,0,0},
{1,0,1,0,0,7,0,0,1,0,1,0,0},
{1,0,3,1,4,0,4,0,1,0,1,0,0},
{1,1,0,0,0,0,1,0,1,0,1,1,1},
{0,1,0,1,1,1,3,0,0,0,0,5,1},
{0,1,0,0,0,0,0,1,1,0,0,0,1},
{0,1,1,1,1,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,1,1,1,1,1,1,1,0,0},
{0,0,0,1,1,0,0,1,0,0,1,0,0},
{0,0,0,1,0,0,0,1,0,5,1,0,0},
{0,0,0,1,4,0,4,0,4,0,1,0,0},
{0,0,0,1,0,4,1,1,0,0,1,0,0},
{0,1,1,1,0,4,0,1,0,1,1,0,0},
{0,1,3,3,3,3,3,0,0,1,0,0,0},
{0,1,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},
}
};
int a[13][13];//当前地图
//栈的操作,实现撤回功能
typedef struct
{
char str[1000];//最多支持1000步回退
int top;
} Stack;
//判断栈是否为空
int IsEmpty(Stack& S)
{
return (S.top == -1 ? 1 : 0);
}
//进栈
void Push(Stack& S, char ch)
{
S.top++;
S.str[S.top] = ch;
}
//出栈
int Pop(Stack& S, char& x)
{
if (IsEmpty(S) == 1)
return 0;
x = S.str[S.top];
S.top--;
return 1;
}
//以上是栈的操作
//目的地的坐标
typedef struct node
{
int x, y;
}Date;
typedef struct
{
Date d[10];//最多十个目的地。
int num;//个数。
}Destination;
//以上是目的地坐标的结构体
Destination D;
//找目的地
void des(int a[13][13])
{
int i, j, k = 0;
D.num = 0;
for (i = 0; i < 13; i++)
for (j = 0; j < 13; j++)
if (a[i][j] == 3)
{
D.num++;
D.d[k].x = i;
D.d[k++].y = j;
}
}
//以上找目的地坐标
//制造地图
void ditu(int a[13][13])
{
int i, j;
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
switch (a[i][j])
{
case 0:printf(" "); break;
case 1:printf("■"); break;
case 3:printf("△"); break;
case 4:printf("○"); break;
case 5:printf("♀"); break;
case 7:printf("●");break;
case 8:printf("♀");break;
}
}
printf("\n");
}
}
//动作制造
int action(int a[13][13], Stack& S)
{
char ch, x;
/*找出人5或者8()的位置*/
int i, j;
for (i = 0; i < 13; i++)
{
for (j = 0; j < 13; j++)
{
if (a[i][j] == 5 || a[i][j] == 8)
break;
}
if (a[i][j] == 5 || a[i][j] == 8)
break;
}
ch = getch();
switch (ch)
{
//撤回前一步
case 'R':
if (Pop(S, x) == 1)
{
//若上一步是向上
if (x == 'W' || x == 'w')
{
if (x == 'W')
{
a[i + 1][j] += 5;
a[i][j] -= 5;
}
else
{
a[i - 1][j] -= 4;
a[i + 1][j] += 5;
a[i][j] -= 1;
}
}
//若上一步是向下
if (x == 'S' || x == 's')
{
if (x == 'S')
{
a[i - 1][j] += 5;
a[i][j] -= 5;
}
else
{
a[i + 1][j] -= 4;
a[i - 1][j] += 5;
a[i][j] -= 1;
}
}
//若上一步是向左
if (x == 'A' || x == 'a')
{
if (x == 'A')
{
a[i][j + 1] += 5;
a[i][j] -= 5;
}
else
{
a[i][j - 1] -= 4;
a[i][j + 1] += 5;
a[i][j] -= 1;
}
}
//若上一步是向右
if (x == 'D' || x == 'd')
{
if (x == 'D')
{
a[i][j - 1] += 5;
a[i][j] -= 5;
}
else
{
a[i][j + 1] -= 4;
a[i][j - 1] += 5;
a[i][j] -= 1;
}
}
}break;
//以上是撤回功能
/*向前走*/
case 'W':
//人走
if (a[i - 1][j] == 0 || a[i - 1][j] == 3)
{
a[i - 1][j] += 5;
a[i][j] -= 5;
Push(S, 'W');
}
//人推箱子走
if ( (a[i - 1][j] == 4 || a[i - 1][j] == 7) && (a[i - 2][j] == 0 || a[i - 2][j] == 3))
{
//if (a[i - 2][j] == 0 || a[i - 2][j] == 3)
//{
a[i - 2][j] += 4;
a[i - 1][j] += 1;
a[i][j] -= 5;
Push(S, 'w');
//}
} break;
/*向下走*/
case 'S':
//人走
if (a[i + 1][j] == 0 || a[i + 1][j] == 3)
{
a[i + 1][j] += 5;
a[i][j] -= 5;
Push(S, 'S');
}
//人推箱子走
if (a[i + 1][j] == 4 || a[i + 1][j] == 7)
{
if (a[i + 2][j] == 0 || a[i + 2][j] == 3)
{
a[i + 2][j] += 4;
a[i + 1][j] += 1;
a[i][j] -= 5;
Push(S, 's');
}
} break;
/*向左走*/
case 'A':
//人走
if (a[i][j - 1] == 0 || a[i][j - 1] == 3)
{
a[i][j - 1] += 5;
a[i][j] -= 5;
Push(S, 'A');
}
//人推箱子走
if (a[i][j - 1] == 4 || a[i][j - 1] == 7)
{
if (a[i][j - 2] == 0 || a[i][j - 2] == 3)
{
a[i][j - 2] += 4;
a[i][j - 1] += 1;
a[i][j] -= 5;
Push(S, 'a');
}
} break;
/*向右走*/
case 'D':
//人走
if (a[i][j + 1] == 0 || a[i][j + 1] == 3)
{
a[i][j + 1] += 5;
a[i][j] -= 5;
Push(S, 'D');
}
//人推箱子走
if (a[i][j + 1] == 4 || a[i][j + 1] == 7)
{
if (a[i][j + 2] == 0 || a[i][j + 2] == 3)
{
a[i][j + 2] += 4;
a[i][j + 1] += 1;
a[i][j] -= 5;
Push(S, 'd');
}
} break;
}
//检测是否完成游戏
for (i = 0; i < D.num; i++)
{
if (a[D.d[i].x][D.d[i].y] != 7)
return 0;
}
return 1;
}
//开始菜单
void startmenu()
{
printf(" 欢迎来到董宏浩开发的推箱子小游戏 \n");
printf(" 更多新关卡正在开发主中... \n");
printf(" 按回车开始游戏 \n");
getchar();
system("cls");
}
//普通模式
void putong()
{
int level = 0;
//普通模式
while (level < maxlevel)
{
//重置栈
Stack S = { {0},0 };
//给当前地图赋值
for (int i = 0; i < 13; i++)
for (int j = 0; j < 13; j++)
a[i][j] = a1[level][i][j];
//寻找目的地
des(a);
while (1)
{
ditu(a);
if (action(a, S))
break;
system("cls");
}
system("cls");
printf("恭喜通过次关,太厉害了,继续请按回车!!");
getchar();
system("cls");
level++;
}
printf("恭喜你通过了所有关卡,真是太厉害了,作者都做不到!!");
getchar();
system("cls");
}
//主函数
int main()
{
//主菜单提示
startmenu();
putong();
}