欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 【C++/控制台】扫雷

【C++/控制台】扫雷

2025/2/25 23:19:52 来源:https://blog.csdn.net/Ub4959d11430/article/details/144867402  浏览:    关键词:【C++/控制台】扫雷

源代码:

#include <windows.h>
#include <conio.h>
#include <stdio.h>
int S, W = 9, H = 9, B = 10, s, p = 0, c = 1, i, *m, *M, (*f)(int, int), *O;
int edge(int x, int y) { return x < 0 || W <= x || y < 0 || H <= y; }
void tmp(int x, int y) { edge(x, y) || m[x += W * y] - 9 && ++m[x]; }
void dig(int v, int l) {for (l = edge(v, l) || M[v += W * l] || (++M[v], --s, m[v]) ? 0 : 9; l;)--l - 4 || --l, f(v % W + l % 3 - 1, v / W + l / 3 - 1);
}
void set(int x, int y) {for (f = tmp; c++ < B; m[i] = 0, dig(i % W, i / W), m[i] = 9, M[i]--)while (m[i = rand() % S] > 8 || (abs(x - i % W) | abs(y - i / W)) < 2);f = dig, s += B, dig(x, y);
}
int main(){f = set, s = S = W * H, m = calloc(S * 2, 4), O = GetStdHandle((DWORD)-11);SetConsoleCursorInfo(O, &(CONSOLE_CURSOR_INFO){1}), M = m + S, srand(m);for (; B % s; c = _getch() & 95, c - 83 || (p += W), c - 87 || (p -= W)) {SetConsoleCursorPosition(O, (COORD){0}), c - 68 || ++p, c - 65 || --p;p = (p + S) % S, c || (f(p % W, p / W), m[p] < 9 || (B = 0)), i = 0;for (c - 27 || (B = 0); c = B | m[i] < 9, i < S; ++i % W || puts(""))SetConsoleTextAttribute(O, (M[i] ? m[i] : 9) | (p - i ? 240 : 64)),c || ++M[i], printf(M[i] ? " %c" : "■", " 12345678@"[m[i]]);}_cputs(B ? "You win!" : "Game over!"), _getch();
}

操作方式:WASD移动光标,空格点击

运行结果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词