不足:
1.缺少统计步数的功能,因为会跟光标跳转函数一起用会出现BUG
2.未使用文件知识,无法重载地图,没有撤回功能,无法返回已完成的关卡
优点:
1.不会闪屏
2.解决了两个箱子堆到一起的BUG
4.解决了箱子进入目的地之后再退一个箱子进去的BUG
3.可以选择关卡
4.解决了直接跳到第八关通关的BUG
5.可以统计已通关数
注意:该版本是对3.0版本的代码优化,调整了难度顺序,增加了界面颜色功能
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <stdbool.h>
# include <windows.h>
//三维数组可以想象成魔方,第一个二维数组的地图就是第一层,以此类推
//注意每一个地图都只需要一个人
int map[8][10][10] =
{
1,1,1,1,1,1,1,1,1,1,
1,3,4,0,0,0,0,4,3,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,5,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,3,4,0,0,0,0,4,3,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,3,4,0,0,0,0,4,3,1,
1,0,0,3,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,4,0,0,0,0,0,1,
1,0,1,1,0,1,1,0,0,1,
1,0,5,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,3,4,0,0,0,0,4,3,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,3,4,0,0,0,0,0,3,1,
1,0,0,0,0,0,0,4,1,1,
1,0,1,0,0,0,0,0,0,1,
1,0,0,0,1,1,0,0,0,1,
1,0,3,1,1,1,1,0,0,1,
1,0,0,4,0,0,4,0,0,1,
1,0,0,0,0,0,0,3,0,1,
1,0,5,0,0,0,0,4,3,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,3,4,0,0,0,0,4,3,1,
1,0,0,0,0,0,0,0,0,1,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inishCount[8] = { 0 };//记录对应下标的地图是否通关,以便地图跳转
int finishSum;//统计已通关数
//注意:此游戏无法使用光标跳转函数,会出现步数错误的BUG
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() {
HideCursor();
role_i = 6;
role_j = 2;
count = 0;//初始化地图下标为0
finishSum = 0;//初始化通关总数为0
}
void show() {
system("color f9");
int i, j;
gotoxy(0, 0);
printf("请切换英文输出,操作为 wasd 或者 ↑↓←→\n");
printf("主要目标:把★推到☆即可进入下一关\n");
printf("您可以通过按键选择关卡(1-8)\n");
printf("您现在正处于第 %d 关\n\n", count + 1);
printf("已通关数:%d\n", finishSum);
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
switch (map[count][i][j])
{
case 0://空地
printf(" ");//注意下面的符号是占两个输出位置,所以为了对齐,需要显示两个空格
break;
case 1://墙
printf("■");
break;
case 3://目的地
printf("☆");
break;
case 4://箱子
printf("★");
break;
case 7://箱子推进目的地
printf("●");
break;
case 5://人
case 8://人
printf("♀");
break;
}
}
printf("\n");
}
}
void updateWithoutInput() {
//判断是否过关,如果过关,则进入下一个地图
//注意:需要先加载地图,再查找位置
bool isMapFinish = true;
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (map[count][i][j] == 4)
{
isMapFinish = false;
goto NEXT;
}
}
}
NEXT:
//统计已通关数
finishSum = 0;
for (i = 0; i < 8; i++)
{
if (isFinishCount[i] == 1)
{
finishSum++;
}
}
//如果箱子都到达目的地,则加载下一个地图
if (isMapFinish)
{
printf("\a");
isFinishCount[count] = 1;
//查找未通关地图下标
for (i = 0; i < 8; i++)
{
if (isFinishCount[i] == 0)
{
break;
}
}
if (i == 8)//如果通关,则显示提示信息
{
printf("OK! You Win!\n");
system("pause");
exit(-1);
}
//限制加载地图下标,最后一关通关后不再继续加载地图,如果未通关,则进入未通关的地图
if (count < 8)
{
count++;
}
else
{
count = i;
}
}
//搜索角色当前所在位置
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (map[count][i][j] == 5 || map[count][i][j] == 8)
{
role_i = i;
role_j = j;
goto NEXT_1;
}
}
}
NEXT_1:
;
}
void updateWithInput() {
//用户按键输入
char hit;
if (_kbhit())
{
hit = _getch();
//根据按键输入更新关卡下标
if (hit >= '1' && hit <= '8')
{
count = hit - 1 - '0';
}
switch (hit)
{
case 'w':
case 'W':
case 72:
if (map[count][role_i - 1][role_j] == 0 || map[count][role_i - 1][role_j] == 3)//当角色前面一个位置是空地或者目的地
{
map[count][role_i - 1][role_j] += 5;//人来了,加5
map[count][role_i][role_j] -= 5;//人走了,减5
}
if (map[count][role_i - 1][role_j] == 4 || map[count][role_i - 1][role_j] == 7)//角色前面一个位置是箱子或者是推进目的地的箱子
{
if (map[count][role_i - 2][role_j] != 1 && map[count][role_i - 2][role_j] != 4 && map[count][role_i - 2][role_j] != 7)//当箱子可以推动时
{
map[count][role_i - 2][role_j] += 4;//箱子来了加4
map[count][role_i - 1][role_j] += 1;//箱子走了减4,人来了加5
map[count][role_i][role_j] -= 5;//人走了减5
}
}
break;
case 's':
case 'S':
case 80:
if (map[count][role_i + 1][role_j] == 0 || map[count][role_i + 1][role_j] == 3)
{
map[count][role_i + 1][role_j] += 5;
map[count][role_i][role_j] -= 5;
}
if (map[count][role_i + 1][role_j] == 4 || map[count][role_i + 1][role_j] == 7)
{
if (map[count][role_i + 2][role_j] != 1 && map[count][role_i + 2][role_j] != 4 && map[count][role_i + 2][role_j] != 7)
{
map[count][role_i + 2][role_j] += 4;
map[count][role_i + 1][role_j] += 1;
map[count][role_i][role_j] -= 5;
}
}
break;
case 'a':
case 'A':
case 75:
if (map[count][role_i][role_j - 1] == 0 || map[count][role_i][role_j - 1] == 3)
{
map[count][role_i][role_j - 1] += 5;
map[count][role_i][role_j] -= 5;
}
if (map[count][role_i][role_j - 1] == 4 || map[count][role_i][role_j - 1] == 7)
{
if (map[count][role_i][role_j - 2] != 1 && map[count][role_i][role_j - 2] != 4 && map[count][role_i][role_j - 2] != 7)
{
map[count][role_i][role_j - 2] += 4;
map[count][role_i][role_j - 1] += 1;
map[count][role_i][role_j] -= 5;
}
}
break;
case'd':
case'D':
case 77:
if (map[count][role_i][role_j + 1] == 0 || map[count][role_i][role_j + 1] == 3)
{
map[count][role_i][role_j + 1] += 5;
map[count][role_i][role_j] -= 5;
}
if (map[count][role_i][role_j + 1] == 4 || map[count][role_i][role_j + 1] == 7)
{
if (map[count][role_i][role_j + 2] != 1 && map[count][role_i][role_j + 2] != 4 && map[count][role_i][role_j + 2] != 7)
{
map[count][role_i][role_j + 2] += 4;
map[count][role_i][role_j + 1] += 1;
map[count][role_i][role_j] -= 5;
}
}
break;
}
}
}
//主函数,游戏主体框架
int main()
{
startup();
while (1) {
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}