推箱子

//头文件
#define w 10
#define h 10
#define s (w*h)

#define KD 0 //□
#define ZA 1 //■
#define KOU 2//口
#define MU 3 //木
#define KUN 4//困

void Draw();
bool Run(int dir);

//-------------------------cpp其一
#include "txz.h"
#include <iostream>
int map[s] = 
{
ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,MU,KD,KD,MU,KD,KD,KD,ZA,
ZA,KD,KOU,KD,KD,KOU,KD,KD,KD,ZA,
ZA,KD,KUN,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KUN,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,
};

//英雄的坐标
int px = 3;
int py = 3;

void Draw()
{
	char* ss[5] = {"□","■","口","木","困"};
	for (int y = 0; y <h; ++y)
	{
		for (int x = 0; x < w; ++x)
		{
			if (x == px && y == py)
			{
				if (map[x+y*w] == KD)
					std::cout<<"人";
				else if (map[x+y*w] == KOU)
					std::cout<<"囚";
			}
			else 
				std::cout<< ss[map[x+y*w]];
		}
		std::cout<<"\n";
	}
}

//true胜利 false 没胜利
bool Run(int dir)
{
	//胜负判断
	bool win = true;
	for (int i = 0;i < s; ++i)
	{
		if (map[i] == KOU || map[i] == MU)
		{
			win = false;
			break;
		}
	}
	if (win)
		return true;

	if (dir == -1)
		return false;

	int xx[4] = {0,0,-1,1};
	int yy[4] = {-1,1,0,0};
	//和英雄相邻的位置
	int nearx = px + xx[dir];
	int neary = py + yy[dir];
	int near = nearx + neary * w;

	//和英雄离得较远的位置
	int farx = px + xx[dir] * 2;
	int fary = py + yy[dir] * 2;
	int far = farx + fary * w;

	switch (map[near])
	{
	case KD ://□
	case KOU://口
		{
			px = nearx; py = neary;
			break;
		}
	case MU ://木
		{
			switch (map[far])
			{
			case KD:
				{
					map[far] = MU;
					map[near] = KD;
					px = nearx; py = neary;
					break;				
				}
			case KOU:
				{
					map[far] = KUN;
					map[near] = KD;
					px = nearx; py = neary;
					break;	
				}
			}
			break;
		}
	case KUN://困 
		{
			switch (map[far])
			{
			case KD:
				{
					map[far] = MU;
					map[near] = KOU;
					px = nearx; py = neary;
					break;				
				}
			case KOU:
				{
					map[far] = KUN;
					map[near] = KOU;
					px = nearx; py = neary;
					break;	
				}
			}
			break;
		}
	}

	return false;
}
//--------------------cpp  main
#include <iostream>
#include <conio.h>
#include "txz.h"


void main()
{
	while (1)
	{
		system("cls");
		Draw();

		
		int dir = -1;
		int a = _getch();
		if (a == 'w' || a == 'W')
			dir = 0;
		if (a == 's' || a == 'S')
			dir = 1;
		if (a == 'a' || a == 'A')
			dir = 2;
		if (a == 'd' || a == 'D')
			dir = 3;

		if (Run(dir))
		{
			std::cout<<"胜利!";
			system("pause");
			break;
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值