c++迷宫小游戏3.0

注:1、1.0和2.0忘记传了

       2、由于作者的Dev-c++不识中文,所以游戏中的文字都是英文!!

简单介绍一下:

1.0:最初的款式,使用'W', 'A', 'S', 'D'来移动。

2.0:设立了第一个设置选项(用于选择是否让玩家看到完整的地图),当然是放在外面的,并且增加了计步功能。

3.0:也就是现在的版本,新造了2个地图(现在共有3个),还会输出玩家'a'所在的坐标以及坐标上的字符。

废话不多说,上代码:

#include <bits/stdc++.h>
#include <windows.h>
#include <bitset>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int x, y, f, flag, vis[10][10];
int a[3] = {24, 25, 24};
string mp[3][10] = {{"S##..##...",
					 ".###..###.",
					 "....#.....",
					 ".###..####",
					 ".####.....",
					 "...##.###.",
					 ".#....###.",
					 "#..###...#",
					 "#.##...#.#",
					 "#....###.F"},
					{"S....##...",
					 "##.#.##.#.",
					 "#..#.###..",
					 "#.##.....#",
					 "..###.##.#",
					 "##....#..#",
					 "...####.##",
					 "####....##",
					 ".....###.#",
					 "F###.....#",},
					{"S....###.#",
					 "#.##......",
					 "....##.##.",
					 "###...#.#.",
					 "#...#....#",
					 "#.#####.##",
					 "#..#######",
					 "#.#.....#.",
					 "#...###...",
					 "..#....##F",}};
void print()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (vis[i][j] || flag)
			{
				if (i == x && j == y) printf("a");
				else printf("%c", mp[f][i][j]);
			}
			else printf(" ");
		}
		printf("\n");
	}
	printf("(%d, %d) %c\n", x, y, mp[f][x][y]);
}
void Vis(int x, int y)
{
	vis[x][y] = 1;
	if (x != 0) vis[x - 1][y] = 1;
	if (y != 0) vis[y - 1][x] = 1;
	if (x != 9) vis[x + 1][y] = 1;
	if (y != 9) vis[x][y + 1] = 1;
	if (x != 0 && y != 0) vis[x - 1][y - 1] = 1;
	if (x != 0 && y != 9) vis[x - 1][y + 1] = 1;
	if (x != 9 && y != 0) vis[x + 1][y - 1] = 1;
	if (x != 9 && y != 9) vis[x + 1][y + 1] = 1;
}
void set_up()
{
	if (!flag) puts("Press '1' to look at the complete map");
	else puts("Press '0' to have the normal perspective");
}

int main()
{
	srand(time(NULL));
	int cnt = 0;
	f = rand() % 3;
	vis[0][0] = 1, vis[1][0] = 1, vis[0][1] = 1, vis[1][1] = 1;
	print(), set_up();
	while (1)
	{
		if (KEY_DOWN('W'))
		{
			if (x - 1 < 0 || mp[f][x - 1][y] == '#') continue;
			system("cls"), x--, cnt++, Vis(x, y), print(), set_up();
		}
		if (KEY_DOWN('A'))
		{
			if (y - 1 < 0 || mp[f][x][y - 1] == '#') continue;
			system("cls"), y--, cnt++, Vis(x, y), print(), set_up();
		}
		if (KEY_DOWN('S'))
		{
			if (x + 1 >= 10 || mp[f][x + 1][y] == '#') continue;
			system("cls"), x++, cnt++, Vis(x, y), print(), set_up();
		}
		if (KEY_DOWN('D'))
		{
			if (y + 1 >= 10 || mp[f][x][y + 1] == '#') continue;
			system("cls"), y++, cnt++, Vis(x, y), print(), set_up();
		}
		if (KEY_DOWN('1')) flag = 1;
		if (KEY_DOWN('0')) flag = 0;
		if (mp[f][x][y] == 'F') { printf("You use %d steps, the optimal solution is %d steps.\n", cnt, a[f]); break; }
	}
    return 0;
}

复制粘贴到dev-c++,编译运行即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值