推箱子5.1.1非文件版

#include <conio.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <windows.h>



//以32为基数,
//0==空, 1==墙,2==箱子,4==箱子到达的地点
char qp[9][12][12] = {//初始地图(作者习惯叫“初始棋盘”)
	{//1
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 34, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 36, 33},
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
	},
	{//2
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
		{33, 32, 32, 33, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 33, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 33, 33, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 33, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 32, 32, 32, 32, 34, 33, 32, 32, 32, 32, 33},
		{33, 32, 33, 32, 32, 32, 33, 32, 33, 33, 32, 33},
		{33, 32, 33, 32, 33, 33, 33, 32, 32, 33, 32, 33},
		{33, 32, 33, 32, 32, 32, 32, 32, 32, 33, 32, 33},
		{33, 32, 33, 32, 33, 33, 32, 32, 33, 33, 32, 33},
		{33, 32, 33, 32, 33, 32, 32, 32, 32, 36, 32, 33},
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
	},
	{//3
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
		{33, 32, 33, 33, 33, 32, 32, 32, 32, 33, 36, 33},
		{33, 32, 33, 32, 32, 32, 32, 33, 32, 33, 32, 33},
		{33, 32, 33, 32, 32, 33, 32, 33, 32, 33, 32, 33},
		{33, 32, 33, 33, 32, 33, 32, 33, 32, 33, 32, 33},
		{33, 32, 33, 33, 32, 33, 32, 33, 32, 32, 32, 33},
		{33, 32, 33, 33, 32, 33, 32, 33, 32, 32, 32, 33},
		{33, 32, 33, 33, 32, 32, 32, 32, 33, 32, 33, 33},
		{33, 32, 33, 33, 32, 32, 32, 32, 33, 32, 33, 33},
		{33, 32, 34, 32, 32, 33, 32, 32, 32, 32, 33, 33},
		{33, 33, 33, 32, 32, 33, 32, 33, 32, 32, 32, 33},
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
	},
	{//4
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
		{33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 33, 33},
		{33, 32, 34, 32, 32, 32, 32, 32, 32, 32, 33, 33},
		{33, 32, 32, 32, 32, 32, 32, 32, 33, 32, 33, 33},
		{33, 32, 32, 33, 33, 33, 33, 32, 32, 32, 33, 33},
		{33, 33, 32, 32, 32, 36, 33, 32, 32, 32, 33, 33},
		{33, 33, 32, 32, 32, 33, 33, 32, 32, 32, 33, 33},
		{33, 33, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33},
		{33, 33, 32, 33, 32, 32, 32, 32, 33, 32, 32, 33},
		{33, 33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 33, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33},
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
	},
	{//5
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33},
		{33, 33, 33, 33, 33, 33, 33, 32, 32, 32, 33, 33},
		{33, 33, 33, 32, 32, 33, 33, 32, 32, 32, 32, 33},
		{33, 33, 32, 32, 34, 32, 33, 33, 32, 32, 32, 33},
		{33, 33, 32, 32, 32, 33, 33, 33, 32, 33, 36, 33},
		{33, 32, 32, 33, 33, 32, 32, 33, 32, 32, 33, 33},
		{33, 32, 32, 32, 32, 32, 32, 33, 32, 33, 36, 33},
		{33, 33, 32, 32, 32, 33, 32, 33, 32, 33, 32, 33},
		{33, 33, 33, 33, 33, 32, 32, 33, 32, 32, 32, 33},
		{33, 32, 32, 34, 32, 32, 32, 32, 32, 32, 32, 33},
		{33, 33, 32, 32, 32, 32, 33, 33, 32, 32, 33, 33},
		{33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33}
	},
};
char nowqp[12][12];//目前地图(作者习惯叫“目前棋盘”)
int nowxy[9][3] = {
	//{横轴,纵轴,箱子数量(暂时没用)}
	{1, 1, 1},//1
	{1, 10, 1},//2
	{1, 1, 1},//3
	{1, 2, 1},//4
	{5, 3, 2},//5
};
int now = 0;//目前关卡
int nowx = nowxy[now][1], nowy = nowxy[now][2];//目前横轴、目前纵轴



void start(){//开始(载入地图)
	for (int i = 0; i < 12; i++){
		for (int j = 0; j < 12; j++){
			nowqp[i][j] = qp[now][i][j];
		}
	}
}
inline void restart(){//重新开始(载入地图)
	start();
}
void init1(){//随机地图
	printf("请输入有解概率与无解概率的比值:");
	int scan;
	scanf("%d", &scan);
	srand(time(0));
	int i = 0, j = 0;
	for (; i < 12; i++){
		for (; j < 12; j++){
			if (i == 0 || j == 0 || i == 11 || j == 11){ 
				nowqp[i][j] = 33;
			}
			else{
				nowqp[i][j] = 32;
			}
		}
	}
	for (i = 0; i < 12; i++){
		for (j = 0; j < 12; j++){
			if (i == 0 || j == 0 || i == 11 || j == 11){ 
				nowqp[i][j] = 33;
			}
			else{
				int x = rand() % (int)(scan * 2 + 2);
				if (x){
					nowqp[i][j] = 32;
				}
				else{
					nowqp[i][j] = 33;
				}
			}
		}
	}
	do{
		i = rand() % 10 + 1; j = rand() % 10 + 1;
	}while(nowqp[i][j] == 33);
	nowqp[i][j] = 34;
	do{
		i = rand() % 10 + 1; j = rand() % 10 + 1;
	}while(nowqp[i][j] == 33 || nowqp[i][j] == 34);
	nowqp[i][j] = 36;
	do{
		i = rand() % 10 + 1; j = rand() % 10 + 1;
	}while(nowqp[i][j] == 33 || nowqp[i][j] == 34 || nowqp[i][j] == 36);
	nowx = i;
	nowy = j;
}





void printqp(){//打印棋盘
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, 15/*0xf=0x1+0x2+0x4+0x8*/);
	system("cls");
	if (now < 9){
		printf("第 %d关\n", now + 1);
	}
	else{
		puts("随机关");
	}
	for (int i = 0; i < 12; i++){
		for (int j = 0; j < 12; j++){
			if (nowx == i && nowy == j){
				SetConsoleTextAttribute(handle, 15/*0xf=0x1+0x2+0x4+0x8*/);
				printf("♀");//你
			}
			else if (nowqp[i][j] == 32){
				printf("  ");//空格
			}
			else if (nowqp[i][j] == 33){
				SetConsoleTextAttribute(handle, 15/*0xf=0x1+0x2+0x4+0x8*/);
				printf("▇");//墙
			}
			else if (nowqp[i][j] == 34){
				SetConsoleTextAttribute(handle, 10/*0xa=0x2+0x8*/);
				printf("●");//箱子
			}
			else if (nowqp[i][j] == 36){
				SetConsoleTextAttribute(handle, 6/*0x6=0x2+0x4*/);
				printf("☆");//没有箱子的目的地
			}
			else if (nowqp[i][j] == 38){
				SetConsoleTextAttribute(handle, 14/*0xe=0x2+0x4+0x8*/);
				printf("★");//有箱子的目的地
			}
		}
		puts("");
	}
}
void scanqp(){//输入
	char inputchar;
	inscan: inputchar = getch();//读取用户输入字符
	if (inputchar == 'd'){//右
		if (nowqp[nowx][nowy + 1] != 33){
			if (nowqp[nowx][nowy + 1] == 34 ||
				nowqp[nowx][nowy + 1] == 38){
				if (nowqp[nowx][nowy + 2] != 33 &&
					nowqp[nowx][nowy + 2] != 34 &&
					nowqp[nowx][nowy + 2] != 38){
					nowqp[nowx][nowy + 2] = nowqp[nowx][nowy + 2] + 2;
					nowqp[nowx][nowy + 1] = nowqp[nowx][nowy + 1] - 2;
					nowy++;
				}
			}
			else{
				nowy++;
			}
		}
	}
	else if (inputchar == 's'){//下
		if (nowqp[nowx + 1][nowy] != 33){
			if (nowqp[nowx + 1][nowy] == 34 ||
				nowqp[nowx + 1][nowy] == 38){
				if (nowqp[nowx + 2][nowy] != 33 &&
					nowqp[nowx + 2][nowy] != 34 &&
					nowqp[nowx + 2][nowy] != 38){
					nowqp[nowx + 2][nowy] = nowqp[nowx + 2][nowy] + 2;
					nowqp[nowx + 1][nowy] = nowqp[nowx + 1][nowy] - 2;
					nowx++;
				}
			}
			else{
				nowx++;
			}
		}
	}
	else if (inputchar == 'a'){//左
		if (nowqp[nowx][nowy - 1] != 33){
			if (nowqp[nowx][nowy - 1] == 34 ||
				nowqp[nowx][nowy - 1] == 38){
				if (nowqp[nowx][nowy - 2] != 33 &&
					nowqp[nowx][nowy - 2] != 34 &&
					nowqp[nowx][nowy - 2] != 38){
					nowqp[nowx][nowy - 2] = nowqp[nowx][nowy - 2] + 2;
					nowqp[nowx][nowy - 1] = nowqp[nowx][nowy - 1] - 2;
					nowy--;
				}
			}
			else{
				nowy--;
			}
		}
	}
	else if (inputchar == 'w'){//上
		if (nowqp[nowx - 1][nowy] != 33){
			if (nowqp[nowx - 1][nowy] == 34 ||
				nowqp[nowx - 1][nowy] == 38){
				if (nowqp[nowx - 2][nowy] != 33 &&
					nowqp[nowx - 2][nowy] != 34 &&
					nowqp[nowx - 2][nowy] != 38){
					nowqp[nowx - 2][nowy] = nowqp[nowx - 2][nowy] + 2;
					nowqp[nowx - 1][nowy] = nowqp[nowx - 1][nowy] - 2;
					nowx--;
				}
			}
			else{
				nowx--;
			}
		}
	}
	else if (inputchar > 48 && inputchar < 54/*58*/){//'1'~'9',代表关卡
		if (now + 49 == inputchar){
			nowx = nowxy[now][1];
			nowy = nowxy[now][0];
			restart();
		}
		else if (now + 49 != inputchar){
			now = inputchar - 49;
			nowx = nowxy[now][1];
			nowy = nowxy[now][0];
			start();
		}
	}
	else if (inputchar == 48){//'0',代表随机关卡(可能无解)
		init1();
	}
	else if (inputchar == 'q'){//退出
		exit(0);
	}
	else{
		goto inscan;
	}
}



int main(){
	start();
	while (1){
		printqp();
		scanqp();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
推箱子是一种常有趣的益智游戏,下面是一个简单的 JavaScript 实现的网页推箱子游戏的代码: HTML 代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>推箱子游戏</title> <style> body{background-color: #eee;} #game{margin: 0 auto;width: 500px;} #game .row{clear: both;} #game .box{float: left;width: 50px;height: 50px;background-color: #fff;border: 1px solid #ccc;text-align: center;line-height: 50px;font-size: 30px;} #game .wall{background-color: #333;} #game .target{background-color: #0c0;} #game .player{background-color: #f00;} #game .box{background-color: #ff0;} </style> </head> <body> <div id="game"></div> <script src="game.js"></script> </body> </html> ``` JavaScript 代码: ``` var gameMap = [ [2,2,2,2,2,2,2,2,2,2], [2,0,0,0,0,2,0,0,0,2], [2,0,0,0,0,2,0,0,0,2], [2,0,0,0,0,0,0,0,0,2], [2,0,1,0,0,0,0,0,0,2], [2,0,0,0,0,0,0,0,0,2], [2,0,0,0,0,0,0,0,0,2], [2,0,0,0,0,0,0,0,0,2], [2,0,0,0,0,0,0,0,0,2], [2,2,2,2,2,2,2,2,2,2] ]; var game = document.getElementById("game"); for(var i=0;i<gameMap.length;i++){ var row = document.createElement("div"); row.className = "row"; for(var j=0;j<gameMap[i].length;j++){ var box = document.createElement("div"); box.className = "box"; if(gameMap[i][j] == 2){ box.className += " wall"; }else if(gameMap[i][j] == 1){ box.className += " player"; }else if(gameMap[i][j] == 3){ box.className += " target"; }else if(gameMap[i][j] == 4){ box.className += " box"; } row.appendChild(box); } game.appendChild(row); } var playerPos = {x:4, y:1}; document.onkeydown = function(event){ var e = event || window.event || arguments.callee.caller.arguments[0]; if(e && e.keyCode == 38){ // 上 move(0,-1); }else if(e && e.keyCode == 40){ // 下 move(0,1); }else if(e && e.keyCode == 37){ // 左 move(-1,0); }else if(e && e.keyCode == 39){ // 右 move(1,0); } } function move(x,y){ var nextPos = {x:playerPos.x+x, y:playerPos.y+y}; if(gameMap[nextPos.y][nextPos.x] == 0 || gameMap[nextPos.y][nextPos.x] == 3){ gameMap[playerPos.y][playerPos.x] = 0; gameMap[nextPos.y][nextPos.x] = 1; render(); playerPos = nextPos; }else if(gameMap[nextPos.y][nextPos.x] == 4){ var nextBoxPos = {x:nextPos.x+x, y:nextPos.y+y}; if(gameMap[nextBoxPos.y][nextBoxPos.x] == 0 || gameMap[nextBoxPos.y][nextBoxPos.x] == 3){ gameMap[playerPos.y][playerPos.x] = 0; gameMap[nextPos.y][nextPos.x] = 1; gameMap[nextBoxPos.y][nextBoxPos.x] = 4; render(); playerPos = nextPos; } } } function render(){ for(var i=0;i<gameMap.length;i++){ for(var j=0;j<gameMap[i].length;j++){ var box = game.childNodes[i].childNodes[j]; box.className = "box"; if(gameMap[i][j] == 2){ box.className += " wall"; }else if(gameMap[i][j] == 1){ box.className += " player"; }else if(gameMap[i][j] == 3){ box.className += " target"; }else if(gameMap[i][j] == 4){ box.className += " box"; } } } } ``` 这个游戏使用了一个二维数组来表示游戏地图,其中数字 0 表示空地,数字 1 表示玩家,数字 2 表示墙,数字 3 表示目标,数字 4 表示箱子。游戏中通过键盘控制玩家移动,当玩家推到所有箱子都放到目标位置时,游戏胜利。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值