推箱子

推箱子小游戏

游戏规则:
经典的推箱子是一个来自日本的古老游戏,目的是在训练你的逻辑思考能力。在一个狭小的仓库中,要求把木箱放到指定的位置,稍不小心就会出现箱子无法移动或者通道被堵住的情况,所以需要巧妙的利用有限的空间和通道,合理安排移动的次序和位置,才能顺利的完成任务。

1. c语言代码实现原理

头文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

#define row 10       //行
#define column 10		//列

void interface1();  //界面

int game();		//游戏

void map(char arr[][column]);	//绘制地图

int player(char arr[][column],char* p);  //玩家

int rule1(char* p,int i,int j);			//判断上规则
int rule2(char* p, int i, int j);			//判断下规则
int rule3(char* p, int i, int j);			//判断左规则
int rule4(char* p, int i, int j);			//判断右规则

int victory(char* p,int* x,int* y,int count); //胜利条件

选择界面:

#include"tast.h"

int main()
{
	int input = 1;  //输入
	
	interface1(); //菜单界面
	printf("选择->");

	while (input)
	{
		scanf("%d", &input);
		switch (input)
		{
			case 1:
			{
				system("cls");	//清屏
				game();    //游戏
				system("cls");  //清屏
				interface1();		//界面
				printf("选择->");
			}break;
			case 0:
			{
				printf("退出\n");
			}break;
			default:
			{
				printf("重新输入->");
			}break;
		}
	}
	system("pause");
	return 0;
}
#include"tast.h"

void interface1()   //界面
{
	printf("********************\n");
	printf("*      推箱子      *\n");
	printf("********************\n");
	printf("*    1.开始游戏    *\n");
	printf("********************\n");
	printf("*    0.退出游戏    *\n");
	printf("********************\n");
}

int game()	//游戏
{
		//‘+’是墙体■,‘O’是箱子▓,‘X’是目标处○,‘*’是人♀,
		//可跟自己需求改下面数组实现不一样的地图
	char arr[row][column] =
	{
		'+', '+', '+', '+', '+', '+', '+', '+', '+', '+',
		'+', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' ', '+',
		'+', 'X', '+', ' ', ' ', ' ', ' ', 'O', ' ', '+',
		'+', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '+',
		'+', ' ', '+', ' ', ' ', ' ', ' ', ' ', ' ', '+',
		'+', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '+',
		'+', ' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', '+',
		'+', ' ', ' ', ' ', '+', 'O', '+', 'O', ' ', '+',
		'+', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '+',
		'+', '+', '+', '+', '+', '+', '+', '+', '+', '+'
	};

	char arr1[row][column]; //复制1份
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			arr1[i][j] = arr[i][j];
		}
	}

	int ch1[20];	//存储X目标点20份
	int ch2[20];

	for (i = 0; i < 20; i++) //初始化
	{
		ch1[i] = 0;
		ch2[i] = 0;
	}

	int k = 0;

	int count = 0; //计‘X’有多少个
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			if (arr1[i][j] == 'X')
			{
				ch1[k] = i;
				ch2[k] = j;
				count++;
				k++;
			}
		}
	}

	map(arr);	//绘制地图

	int input = 1;

	while (input)
	{
		input = player(arr, arr);	//玩家
		if (input == 0)
		{
			break;
		}
		system("cls");
		input = victory(arr,ch1,ch2,count);  //胜利条件
		map(arr);
						
	}
	printf("\n返回主页面,");
	system("pause");
}

void map(char arr[][column])		//绘制地图
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			if (arr[i][j] == '+')
			{
				printf("■");
			}
			else if (arr[i][j] == ' ')
			{
				printf("  ");
			}
			else if (arr[i][j] == 'O')
			{
				printf("▓");
			}
			else if (arr[i][j] == '*')
			{
				printf("♀");
			}
			else if (arr[i][j] == 'X')
			{
				printf("○");
			}
		}
		printf("\n");
	}
}

int player(char arr[][column],char* p) //找到玩家位置
{
	int count = 1;

	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < column; j++)
		{
			if (arr[i][j] == '*')
			{
				break;
			}
		}
		if (arr[i][j] == '*')
		{
			break;
		}
	}


	while (count)
	{
//		_getch; //上下左右需要读取2次
		char k = _getch(); 
		switch (k)
		{
			case 27:		//输入esc
			{	
				return 0;	//跳出函数
			}break;
			case 72:
			{		
				if ((*(p + (((i - 1) * column) + j))) != '+')//判断上方墙体
				{
					if ((*(p + (((i - 1) * column) + j))) != 'O') //判断上方有没有箱子
					{
						*(p + (((i - 1) * column) + j)) = '*';		//向上走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
					else if (rule1(p, i, j) == 1)
					{

						*(p + (((i - 1) * column) + j)) = '*';		//向上走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
				}
			}break;
		case 80:
			{
				if ((*(p + (((i + 1) * column) + j))) != '+')//判断下方墙体
				{
					if ((*(p + (((i + 1) * column) + j))) != 'O') //判断下方有没有箱子
					{
						*(p + (((i + 1) * column) + j)) = '*';		//向下走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
					else if (rule2(p, i, j) == 1)
					{

						*(p + (((i + 1) * column) + j)) = '*';		//向下走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
				}
		}break;
			case 75:
			{
				if ((*(p + ((i * column) + (j - 1))) != '+'))//不能走向墙体
				{
					if ((*(p + ((i * column) + (j - 1))) != 'O'))
					{
						*(p + ((i * column) + (j - 1))) = '*';		//向左走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
					else if (rule3(p, i, j) == 1)
					{
						*(p + ((i * column) + (j - 1))) = '*';		//向左走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
				}
			}break;
		case 77:
			{
				if ((*(p + ((i * column) + (j + 1)))) != '+')//不能走向墙体
				{
					if ((*(p + ((i * column) + (j + 1))) != 'O'))
					{
						*(p + ((i * column) + (j + 1))) = '*';		//向右走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
					else if (rule4(p, i, j) == 1)
					{
						*(p + ((i * column) + (j + 1))) = '*';		//向右走
						*(p + ((i * column) + j)) = ' '; //清除
						count = 0;	//跳出循环
					}
				}
			}break;
		default:
			{
				   count = 1;//继续循环
			}
		}
	}


	printf("%d\n",count);
	
}

int rule1(char* p,int i,int j)	//上规则
{
	int count = 1;
	
	if ((*(p + (((i - 1) * column) + j))) == 'O' && (*(p + (((i - 2) * column) + j))) != 'O')//判断有没有箱子
	{
		if (((*(p + (((i - 2) * column) + j))) != '+'))//判断有没有墙
		{
			(*(p + (((i - 2) * column) + j))) = 'O';
			return 1;
		}
	}

}

int rule2(char* p, int i, int j)	//下规则
{
	int count = 1;

	if ((*(p + (((i + 1) * column) + j))) == 'O' && (*(p + (((i + 2) * column) + j))) != 'O')
	{
		if ((*(p + (((i + 2) * column) + j))) != '+')//判断有没有墙
		{
			(*(p + (((i + 2) * column) + j))) = 'O';
			return 1;
		}
	}
}

int rule3(char* p, int i, int j)	//左规则
{
	int count = 1;

	if ((*(p + ((i * column) + (j - 1)))) == 'O' && (*(p + ((i * column) + (j - 2)))) != 'O')
	{
		if ((*(p + ((i * column) + (j - 2)))) != '+')
		{
			(*(p + ((i * column) + (j - 2)))) = 'O';
			return 1;
		}
	}
}

int rule4(char* p, int i, int j)	//右规则
{
	int count = 1;

	if ((*(p + ((i * column) + (j + 1)))) == 'O' && (*(p + ((i * column) + (j + 2)))) != 'O')
	{
		if ((*(p + ((i * column) + (j + 2)))) != '+')
		{
			(*(p + ((i * column) + (j + 2)))) = 'O';
			return 1;
		}
	}
}

int victory(char* p,int* x,int* y,int count)
{
	int a = 0;	//次数
	int i = 0;
	for (i = 0; i < count; i++)
	{
		if ((*(p + ((*(x+i)) * column) + (*(y+i)))) == ' ') //检查X是否为空格
		{
			(*(p + ((*(x + i)) * column) + (*(y+i)))) = 'X';
		}
	}

	for (i = 0; i < count; i++)
	{
		if ((*(p + ((*(x + i)) * column) + (*(y + i)))) == ' ') //检查X是否为空格
		{
			(*(p + ((*(x + i)) * column) + (*(y + i)))) = 'X';
		}
	}

	for (i = 0; i < count; i++)
	{
		if ((*(p + ((*(x + i)) * column) + (*(y + i)))) == 'O') //检查X是否为空格
		{
			a++;
		}
	}
	if (a == count)
	{
		printf("\n\t\t胜利\n\n");
		return 0;
	}
return 1;
}

完成

在这里插入图片描述

上下左右键控制
在这里插入图片描述
不怎么会写博客,以后尽量写具体点,多多包涵。不懂的地方可以问我。

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值