注: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++,编译运行即可。