迷宫(初探,为后面做个准备)

本文介绍了一个基于C++的迷宫游戏实现,玩家可以通过WASD键盘指令控制角色在迷宫中移动,避免墙壁并探索迷宫。游戏使用二维数组表示迷宫地图,通过交换数组元素来更新角色位置,同时提供了实时的迷宫地图显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

放弃这个吧:因为打印的时候  0,0是在左上角!

 

-------------------------------

 

 

 

#include <iostream>
#include <string>
#include <queue>
#include <cstdio>
using namespace std;



//0-9 * 0-9   : 100个元素
// 1代表自己  2代表墙 不能通过
int a[10][10] = {
	{1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 2, 0, 0, 0, 0, 0, 0, 0},
	{2, 0, 2, 0, 0, 0, 0, 0, 0, 0},
	{2, 0, 2, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 2, 0, 0, 2, 2, 0, 0, 0},
	{0, 0, 2, 0, 0, 2, 0, 0, 0, 0},
	{0, 0, 2, 2, 2, 2, 0, 2, 2, 0},
	{0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
	{0, 0, 0, 0, 2, 2, 2, 2, 0, 0},
	{0, 0, 0, 0, 2, 0, 0, 0, 0, 0},
} ;

int x, y; //当前坐标


//打印出来
void show(int a[10][10] = a, int minIndex = 0, int maxIndex = 9) {
	for (int i = minIndex; i <= maxIndex; i++) {
		for (int j = minIndex; j <= maxIndex; j++) {
			printf("%2d", a[i][j]);
		}
		cout << endl;
	}

	cout << endl;

}

//W A S D
void go(char ch) {
	switch (ch) {
		case 'S':// 0 0  -> 1 0
			if (y + 1 <= 9 && a[y + 1][x] != 2) {
//				cout << "now pos is " << y << "," << x << endl;
				swap(a[y][x], a[y + 1][x]);
				y++;
				cout << "下"  << endl;
//				cout << "now pos is " << y << "," << x << endl;
			} else {
				cout << "越界||墙!" << endl;
			}
			break;
		case 'W':
			if (y - 1 >= 0 && a[y - 1][x] != 2) {
				swap(a[y][x], a[y - 1][x]);
				y--;
				cout << "上"  << endl;
			} else {
				cout << "墙!" << endl;
			}
			break;
		case 'A'://左, 1,1 到 1,0
			if (x - 1 >= 0 && a[y][x - 1] != 2) {
//				cout << "now POS is " << y << "," << x << endl;
				swap(a[y][x], a[y][x - 1]);
				x--;
				cout << "左"  << endl;
//				cout << "now pos is " << y << "," << x << endl;
			} else {
				cout << "墙!" << endl;
			}
			break;
		case 'D':
			if (x + 1 <= 9 && a[y][x + 1] != 2) {
//				cout << "now x,y is " << x << "," << y << endl;
				swap(a[y][x], a[y][x + 1]);
				x++;
				cout << "右"  << endl;
//				cout << "now x,y is " << x << "," << y << endl;
			} else {
				cout << "墙!" << endl;
			}
			break;
	}

	show();
}




int main() {
	char ch;
	while (cin >> ch && ch != 'Q') {
		go(ch);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值