推箱子(C语言版)

简介: 推箱子(C语言版)

 推箱子这个小游戏,当初自己也写了很久

这个游戏:就是人将所有箱子推到目的地,便算是成功

主要思路:

1.用二维数组和枚举绘制地图(如果要增加关卡,就用三维数组(里面存放的元素是多个二维数组),这里我写了三关)

2.用数组的位置来写人和箱子的移动,再配合switch语句,就可以实现用键盘操控人的移动。

3.游戏结束的标志:空地上没有箱子

源码在这,如有需要,请自行领取。

#include<stdio.h>                                       // 推箱子的几个元素;
#include<stdlib.h>                                     //  0 空地   1 墙  2 目的地   3 箱子 4 玩 5 箱子在目的地  6 玩家在目的地 
#include<conio.h>//_getch()函数的头文件   //用_getch函数,如果用getchar(),每次移动要回车键  // 上 72  下 80  左75   右77
enum Mine
{
  space,
  wall,
  destination,
  box,
  player,
};
int level = 0;
int map[3][10][10] =
{
  {
    {0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
    {0, 0, 0, 1, 0, 0 ,2 ,1, 0, 0},
    {0, 0, 0, 1, 0, 3 ,0 ,1, 0, 0},
    {0, 1, 1, 1, 0, 0 ,0 ,1, 1, 1},
    {0, 1, 0, 2, 3 ,4 ,3, 0, 2, 1},
    {0, 1, 1, 1, 0, 3 ,0 ,1, 1, 1},
    {0, 0, 0, 1, 0, 2 ,0 ,1, 0, 0},
    {0, 0, 0, 1, 0, 0 ,0 ,1, 0, 0},
    {0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
    {0, 0, 0, 0, 0, 0 ,0 ,0, 0, 0},
  },
  {
    {1, 1, 1, 1, 1, 1 ,1 ,1, 1, 1},
    {1, 2, 0, 0, 0, 0 ,0 ,0, 0, 1},
    {1, 0, 0, 0, 0, 0 ,3 ,0, 1, 0},
    {1, 0, 0, 3, 0, 0 ,0 ,0, 0, 1},
    {1, 0, 0, 3, 0 ,4 ,0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0 ,1 ,0, 0, 1},
    {1, 2, 1, 1, 0, 0 ,1 ,0, 2, 1},
    {1, 1, 1, 0, 1, 0 ,1 ,0, 1, 0},
    {0, 0, 0, 0, 1, 0 ,1 ,0, 1, 0},
    {0, 0, 0, 0, 1, 1 ,1 ,1, 0, 0},
  },
  {
    {0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
    {0, 0, 1, 0, 0, 0 ,0 ,1, 0, 0},
    {0, 1, 0, 3, 0, 3 ,0 ,0, 1, 0},
    {1, 2, 0, 0, 0, 0 ,1 ,0, 0, 1},
    {1, 1, 1, 1, 2 ,0 ,1, 0, 0, 1},
    {1, 0, 0, 1, 1, 1 ,1 ,0, 0, 1},
    {1, 2, 0, 0, 0, 4 ,0 ,0, 0, 1},
    {0, 1, 0, 0, 3, 0 ,3 ,0, 1, 0},
    {0, 0, 1, 0, 0, 0 ,2 ,1, 0, 0},
    {0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
  }
};
void drawmap()
{
  for (int i = 0; i < 10; ++i)
  {
    for (int j = 0; j < 10; ++j)
    {
      switch (map[level][i][j])
      {
      case space:
        printf("  ");
        break;
      case wall:
        printf("▓ ");
        break;
      case destination:
        printf("☆");
        break;
      case box:
        printf("□");
        break;
      case player:
        printf("♀");
        break;
      case box + destination:
        printf("★");
        break;
      case player + destination:
        printf("♂");
        break;
      }
    }
    printf("\n");
  }
}
void playgame()
{
  int i = 0, j = 0;
  for (i = 0; i < 10; i++)
  {
    for (j = 0; j < 10; j++)
    {
      if (map[level][i][j] == player || map[level][i][j] == player + destination)
      {
        goto end;       //break跳出一层循环,用  goto end; end:; 跳出所有的循环
      }
    }
  }
end:;
  int  key = _getch();
  switch (key)
  {
    case 'w':      // 上
    case 'W':
    case  72:
      if (map[level][i - 1][j] ==  space || map[level][i - 1][j] ==  destination)
      {
        map[level][i - 1][j] += player;
        map[level][i][j] -= player;
      }
      else
      {
        if (map[level][i - 1][j] == box || map[level][i - 1][j] == box + destination)
        {
          if (map[level][i - 2][j] == space || map[level][i - 2][j] == destination)
          {
            map[level][i - 2][j] += box;
            map[level][i - 1][j] = map[level][i - 1][j] - box + player;
            map[level][i - 1][j] -=  player;
          }
        }
      }
      break;
    case 's':     //  下
    case 'S':
    case 80:
      if (map[level][i + 1][j] == space || map[level][i + 1][j] == destination)
      {
        map[level][i + 1][j] = map[level][i + 1][j] + player;
        map[level][i][j] = map[level][i][j] - player;
      }
      else
      {
        if (map[level][i + 1][j] == box || map[level][i + 1][j] == box + destination)
        {
          if (map[level][i + 2][j] == space || map[level][i + 2][j] == destination)
          {
            map[level][i + 2][j] = map[level][i + 2][j] + box;
            map[level][i + 1][j] = map[level][i + 1][j] - box + player;
            map[level][i + 1][j] = map[level][i + 1][j] - player;
          }
        }
      }
      break;
    case 'a':     //  左
    case 'A':
    case 75:
      if (map[level][i][j - 1] == space || map[level][i][j - 1] == destination)
      {
        map[level][i][j - 1] += player;
        map[level][i][j] -= player;
      }
      else
      {
        if (map[level][i][j - 1] == box || map[level][i][j - 1] == box + destination)
        {
          if (map[level][i][j - 2] == space || map[level][i][j - 2 ] == destination)
          {
            map[level][i][j - 2] += box;
            map[level][i][j - 1] = map[level][i][j - 1] - box + player;
            map[level][i][j - 1] -= player;
          }
        }
      }     
      break;
    case 'd':      // 右
    case 'D':
    case 77:
      if (map[level][i][j + 1] == space || map[level][i][j + 1] == destination)
      {
        map[level][i][j + 1] += player;
        map[level][i][j] -= player;
      }
      else
      {
        if (map[level][i][j + 1] == box || map[level][i][j + 1] == box + destination)
        {
          if (map[level][i][j + 2] == space || map[level][i][j + 2] == destination)
          {
            map[level][i][j + 2] += box;
            map[level][i][j + 1] = map[level][i][j + 1] - box + player;
            map[level][i][j + 1] -= player;
          }
        }
      }
      break;
  }
}
bool deduce_success()
{
  for (int a = 0; a < 10; a++)
  {
    for (int b = 0; b < 10; b++)
    {
      if (map[level][a][b] == box)
      {
        return false;
      }
    }
  }
  return true;
}
int main()
{
  //cols 长  lines宽
  system("mode con cols=25 lines=15");
  while (1)
  {
    system("cls");
    drawmap();
    if (deduce_success())
    {
      level++;
      if (level > 2)
      {
        printf("你赢了");
        printf("不愧是地表最强的人!!!!!!");
        printf("恭喜通关!!!!!!");
        break;
      }
    }
    playgame();
  }
  getchar();
  return 0;
} 
相关文章
|
12月前
|
缓存 Prometheus 监控
Elasticsearch集群JVM调优设置合适的堆内存大小
Elasticsearch集群JVM调优设置合适的堆内存大小
2010 1
|
人工智能 自然语言处理 供应链
生成式AI如何改变供应链和采购角色
生成式AI如何改变供应链和采购角色
|
存储 SQL 关系型数据库
校园二手商品交易系统的设计与实现(论文+源码)_kaic
校园二手商品交易系统的设计与实现(论文+源码)_kaic
|
存储 Unix 数据挖掘
【Netapp数据恢复】Netapp存储误删除lun的数据恢复案例
某单位一台Netapp存储,该Netapp存储内共有数十块SAS硬盘。 工作人员误操作删除了Netapp存储中12个lun,删除的数据包括客户信息和其他重要数据。
【Netapp数据恢复】Netapp存储误删除lun的数据恢复案例
|
移动开发 API
万网域名注册促销活动_阿里云域名优惠口令免费获取
万网域名注册促销活动_阿里云域名优惠口令免费获取,2023年阿里云域名优惠口令,com域名续费优惠口令“com批量注册更享优惠”,cn域名续费优惠口令“cn注册多个价格更优”,com域名注册优惠口令“梦想从域名开始”,cn域名注册优惠口令“互联网上的中国标识”
411 1
|
XML Java 数据格式
SpringBoot+Mybatis+application.yml(仅供入门参考)
SpringBoot+Mybatis+application.yml(仅供入门参考)
959 0
SpringBoot+Mybatis+application.yml(仅供入门参考)
|
边缘计算 运维 Kubernetes
k3d+k3s+kubecm 本地快速搭建与管理集群
使用 k3d 在本地快速搭建轻量级 k8s 集群 - k3s,并使用 kubecm 管理所有集群。
6058 0
k3d+k3s+kubecm 本地快速搭建与管理集群
|
3天前
|
云安全 人工智能 自然语言处理
AI说的每一句话,都靠谱吗?
阿里云提供AI全栈安全能力,其中针对AI输入与输出环节的安全合规挑战,我们构建了“开箱即用”与“按需增强”相结合的多层次、可配置的内容安全机制。