老版推箱子

/********************************************
项    目: 推箱子
作    者: 零起点
编译环境:VS 2015
最后修改:2017年3月8日
*********************************************/
#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
#include<dos.h>
int chart[8][8] = {
{0,1,1,1,1,1,0,0},
{0,1,0,5,1,1,1,0},
{0,1,0,4,0,0,1,0},
{1,1,1,0,1,0,1,1},
{1,3,1,0,1,0,0,1},
{1,3,4,0,0,1,0,1},
{1,3,0,0,0,4,0,1},
{1,1,1,1,1,1,1,1}
};
int  pox_x,pox_y;
void  outlet(void); //输出图案
void The_key(void); //向上走的功能
void Left_key(void); //向左走的功能
void hand_button(void); //向下走的功能
void next_key(void); //向右走的功能
int judge(void); //判断箱子是否在指定位置


int main(void)
{
char ch;
int sum;
for (pox_x = 0; pox_x < 8; ++pox_x)
{
for (pox_y = 0; pox_y < 8; ++pox_y)
if (chart[pox_x][pox_y] == 5)
break;
if (chart[pox_x][pox_y] == 5)
break;
}//找出  人 的 位置   必须用两个  if  不然会 出错
while (1)
{
outlet();
fflush(stdin);//更新窗口
switch (getch())//接受 键盘的值
{
case 72://上键
The_key();
break;
case 80://下键
hand_button();
break;
case 75://左键
Left_key();
break;
case 77:
next_key();
break;
}
if ((sum = judge()) == 3)
{
printf("你赢了!!");
break;
}
}
system("pause");
return 0;
}
void  outlet(void)
{
system("cls");//清屏
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 8; ++j)
switch (chart[i][j])
{
case 0:printf("  "); break;
case 1:printf("※"); break;
case 3:printf("☆"); break;
case 4:printf("□"); break;
case 8:
case 5:printf("♂"); break;
case 7:printf("★"); break;
}
putchar('\n');
}
return;
}


void The_key(void)
{
if ((chart[pox_x - 1][pox_y] == 0 || chart[pox_x - 1][pox_y] == 3) && pox_x - 1 != -1)
{
if(chart[pox_x-1 ][pox_y] == 3)
chart[pox_x - 1][pox_y] = 8;
else
chart[pox_x - 1][pox_y] = 5;
if(chart[pox_x ][pox_y] == 8)
chart[pox_x][pox_y] = 3;
else
chart[pox_x][pox_y] = 0;
pox_x -= 1;
}
else if (chart[pox_x - 1][pox_y] == 4 && (pox_x - 2 != -1 && chart[pox_x - 2][pox_y]  != 1))
{
if(chart[pox_x - 2][pox_y] != 3)
chart[pox_x - 2][pox_y] = 4;
else
{
chart[pox_x - 2][pox_y] = 7;
chart[pox_x - 1][pox_y] = 5;
chart[pox_x][pox_y] = 3;
}
pox_x -= 1;

else if (chart[pox_x - 1][pox_y] == 7 && (pox_x - 2 != -1 && chart[pox_x - 2][pox_y] != 1))
{
if (chart[pox_x - 2][pox_y] == 3)
{
chart[pox_x - 2][pox_y] = 7;
chart[pox_x - 1][pox_y] = 8;
}
if (chart[pox_x][pox_y] == 8)
chart[pox_x][pox_y] = 3;
else
chart[pox_x][pox_y] = 0;
pox_x -= 1;
}

return;
}
void hand_button(void)
{
if ((chart[pox_x + 1][pox_y] == 0 || chart[pox_x + 1][pox_y] == 3) && pox_x + 1 != 8)
{
if (chart[pox_x+1][pox_y] == 3)
chart[pox_x + 1][pox_y] = 8;
else
chart[pox_x + 1][pox_y] = 5;
if (chart[pox_x][pox_y] == 8)
chart[pox_x][pox_y] = 3;
else
chart[pox_x][pox_y] = 0;
pox_x += 1;
}
else if (chart[pox_x + 1][pox_y] == 4 && (pox_x + 2 != 8 && chart[pox_x + 2][pox_y] != 1))
{
if (chart[pox_x + 2][pox_y] != 3)
{
chart[pox_x + 2][pox_y] = 4;
chart[pox_x + 1][pox_y] = 5;
chart[pox_x][pox_y] = 0;
}
else
{
chart[pox_x + 2][pox_y] = 7;
chart[pox_x + 1][pox_y] = 5;
chart[pox_x][pox_y] = 3;
}
pox_x += 1;
}


return;
}
void next_key(void)
{
if ((chart[pox_x][pox_y + 1] == 0 || chart[pox_x][pox_y + 1] == 3) && pox_y + 1 != 8)
{
if (chart[pox_x][pox_y + 1] == 3)
chart[pox_x][pox_y +1] = 8;
else
chart[pox_x][pox_y + 1] = 5;
if (chart[pox_x][pox_y] == 8)
chart[pox_x][pox_y] = 3;
else
chart[pox_x][pox_y] = 0;
pox_y += 1;
}
else if (chart[pox_x][pox_y + 1] == 4 && (pox_y + 2 != 8 && chart[pox_x][pox_y + 2] != 1))
{
if (chart[pox_x][pox_y + 2] != 3)
{
chart[pox_x][pox_y + 2] = 4;
chart[pox_x][pox_y + 1] = 5;
chart[pox_x][pox_y] = 0;


}
else
{
chart[pox_x][pox_y +2] = 7;
chart[pox_x][pox_y + 1] = 5;
chart[pox_x][pox_y] = 0;
}
pox_y += 1;
}
return;
}
void Left_key(void)
{

if ((chart[pox_x][pox_y - 1] == 0 || chart[pox_x][pox_y - 1] == 3) && pox_y - 1 != -1)
{
if (chart[pox_x][pox_y - 1] == 3)
chart[pox_x][pox_y - 1] = 8;
else
chart[pox_x][pox_y - 1] = 5;
if (chart[pox_x][pox_y] == 8)
chart[pox_x][pox_y] = 3;
else
chart[pox_x][pox_y] = 0;
pox_y -= 1;
}
else if (chart[pox_x][pox_y - 1] == 4 && (pox_y - 2 != -1 && chart[pox_x][pox_y - 2] != 1))
{
if (chart[pox_x][pox_y - 2] != 3)
{
chart[pox_x][pox_y - 2] = 4;
chart[pox_x][pox_y - 1] = 5;
chart[pox_x][pox_y ] = 0;
}
else 
{
chart[pox_x][pox_y -2] = 7;
chart[pox_x][pox_y - 1] = 5;
chart[pox_x][pox_y] = 0;
}
pox_y -= 1;
}


return;
}




int  judge(void)
{
int sum = 0;
for(int i=0;i<8;i++)
for (int j = 0; j < 8; ++j)
{
if (chart[i][j] == 7)
++sum;
}
return sum;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值