扫雷4.0
YangShaoQi_1e8 · · 科技·工程
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int N = 10;
int M = 10;
int MINE = 10;
const int dx[10] = {
1,
-1,
0,
0,
1,
1,
-1,
-1
};
const int dy[10] = {
0,
0,
1,
-1,
-1,
1,
-1,
1
};
bool Fail;
bool play = true;
int X = 1, Y = 1;
int cnt_open;
int cnt_mark;
struct node {
int cnt;
bool mark;
bool mine;
bool open;
};
node board[105][105];
void start() {
printf("\n\n\t\t\t\t\t\t欢迎来到扫雷小游戏\n\n\n\n\n");
printf("Tip:记得调成英文输入法\n\n\n");
system("pause");
}
void make_mines() {
int tmp_cnt = 0;
while (tmp_cnt < MINE) {
int x = rand() % N + 1;
int y = rand() % M + 1;
if (!board[x][y].mine) {
board[x][y].mine = true;
board[x][y].cnt = -1e9;
for (int i = 0; i < 8; i++) {
int tmp_x = x + dx[i];
int tmp_y = y + dy[i];
if (tmp_x < 1 || tmp_y < 1 || tmp_x > N || tmp_y > M) continue;
board[tmp_x][tmp_y].cnt++;
}
tmp_cnt++;
}
}
}
void new_game() {
system("cls");
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
board[i][j].mine = false;
board[i][j].mark = false;
board[i][j].open = false;
board[i][j].cnt = 0;
}
}
cnt_open = 0;
cnt_mark = 0;
Fail = false;
make_mines();
}
void dfs_open(int x, int y) {
if (board[x][y].cnt) return;
for (int i = 0; i < 8; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 1 || ty < 1 || tx > N || ty > M) continue;
if (board[tx][ty].open == true) continue;
if (board[tx][ty].mark == true) continue;
if (board[tx][ty].mine == true) continue;
board[tx][ty].open = true;
cnt_open++;
dfs_open(tx, ty);
}
}
void clear() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo = {
1,
FALSE
};
SetConsoleCursorInfo(hOut, & cursorInfo);
COORD coord = {
(SHORT) 0,
(SHORT) 0
};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void mask() {
system("cls");
cout << "Hello,World!\n\n";
cout << "--------------------------------\n";
cout << "Process exited after 0.2733 seconds with return value 0\n";
system("pause");
system("cls");
}
void setting() {
N = M = MINE = 0;
system("cls");
while (N < 3 || N > 12) {
system("cls");
cout << "行数(3 ~ 12):";
cin >> N;
}
while (M < 3 || M > 12) {
system("cls");
cout << "行数(3 ~ 12):" << N << "\n\n列数(3 ~ 12):";
cin >> M;
}
while (MINE < 1 || MINE >= N * M) {
system("cls");
cout << "行数(3 ~ 12):" << N << "\n\n列数(3 ~ 12):" << M;
cout << "\n\n地雷数量(1 ~ 行数*列数/2):";
cin >> MINE;
}
system("cls");
new_game();
}
void move() {
if (_kbhit()) {
char op = getch();
if (op == 'w' && X > 1) X--;
if (op == 's' && X < N) X++;
if (op == 'a' && Y > 1) Y--;
if (op == 'd' && Y < M) Y++;
if (op == ' ') {
if (!board[X][Y].mine) {
if (!board[X][Y].open && !board[X][Y].mark) {
board[X][Y].open = true;
cnt_open++;
dfs_open(X, Y);
}
} else {
if (cnt_open) {
Fail = true;
return;
} else {
while (board[X][Y].mine) new_game();
board[X][Y].open = true;
cnt_open++;
dfs_open(X, Y);
}
}
}
if (op == 'm' && !board[X][Y].open) {
if (!board[X][Y].mark) cnt_mark++;
else cnt_mark--;
board[X][Y].mark = !board[X][Y].mark;
}
if (op == 'i') {
system("cls");
printf("\n\n操作说明:\n\n [WSAD] 键控制上下左右\t\t\t雷 1 0\n [空格] 键打开方格\t\t\t2 2 1\n [M] 键标记地雷(再按一次取消)\t1 雷 1\n\n\n");
system("pause");
system("cls");
}
if (op == 'n') mask();
if (op == 'o') play = false;
if (op == 'u') setting();
return;
}
}
void color(string op) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (op == "red") SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
if (op == "blue") SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
if (op == "green") SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
if (op == "white") SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
}
void output() {
printf(" 标记数量:%d 地雷数量:%d\n 打开数量:%d 棋盘规格:%d*%d\n\n", cnt_mark, MINE, cnt_open, N, M);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (!Fail) {
if (!board[i][j].open) {
if (!board[i][j].mark) {
if (i == X && j == Y) color("red");
cout << " [] ";
color("white");
} else {
if (i == X && j == Y) {
color("red");
cout << " [!]";
color("white");
} else {
color("red");
cout << " ! ";
color("white");
}
}
} else {
if (i == X && j == Y) {
color("red");
cout << " [";
color("green");
cout << board[i][j].cnt;
color("red");
cout << "]";
color("white");
} else {
color("green");
cout << " " << board[i][j].cnt << " ";
color("white");
}
}
} else {
if (!board[i][j].mine) {
if (!board[i][j].open) {
if (!board[i][j].mark) {
if (i == X && j == Y) color("red");
cout << " [] ";
color("white");
} else {
if (i == X && j == Y) {
color("red");
cout << " [!]";
color("white");
} else {
color("red");
cout << " ! ";
color("white");
}
}
} else {
if (i == X && j == Y) {
color("red");
cout << " [";
color("green");
cout << board[i][j].cnt;
color("red");
cout << "]";
color("white");
} else {
color("green");
cout << " " << board[i][j].cnt << " ";
color("white");
}
}
} else {
color("red");
cout << " 雷 ";
color("white");
}
}
}
cout << "\n\n";
}
if (!Fail && cnt_open != N * M - MINE) cout << "说明(I) 设置(U) 伪装(N) 关闭(O)\n\n";
}
int main() {
srand(time(0));
start();
while (play) {
new_game();
while (!Fail && cnt_open < N * M - MINE) {
clear();
output();
move();
if (!play) {
system("cls");
return 0;
}
}
system("cls");
output();
if (!Fail) cout << "YOU WIN!\n\n";
else cout << "YOU LOSE!\n\n";
system("pause");
}
return 0;
}