“扫雷”想必80 90 年代的都不陌生吧,是当代青年人机必备的益智游戏,今天,我们就用c/c++还原它,记得点赞支持一下,老乔谢谢你们~
使用演示设备:
1.开发环境 visual studio 2022 或 dev c++
2.演示系统 Windows 10
3.演示课目 扫雷游戏
效果展示(演示图):
上代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 3
#define COLS 3
#define MINES 1
char minefield[ROWS][COLS];
char displayed_field[ROWS][COLS];
void initialize_game() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
minefield[i][j] = '0';
displayed_field[i][j] = '*';
}
}
srand(time(0));
int mines_placed = 0;
while (mines_placed < MINES) {
int row = rand() % ROWS;
int col = rand() % COLS;
if (minefield[row][col] == '1') continue;
minefield[row][col] = '1';
mines_placed++;
}
}
void print_field(char field[ROWS][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%c ", field[i][j]);
}
printf("\n");
}
}
void update_displayed_field(int row, int col) {
if (minefield[row][col] == '1') {
printf("你死了,这是一个雷!\n");
print_field(minefield);
exit(1);
}
int neighbors = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && minefield[i][j] == '1') {
neighbors++;
}
}
}
displayed_field[row][col] = '0' + neighbors;
}
void play_game() {
initialize_game();
print_field(displayed_field);
while (1) {
printf("请输入你的位置 (row col): ");
int row, col;
scanf("%d %d", &row, &col);
if (row < 0 || col < 0 || row >= ROWS || col >= COLS) {
printf("非法位置,请输入有效坐标!\n");
continue;
}
if (displayed_field[row][col] == '*') {
update_displayed_field(row, col);
print_field(displayed_field);
} else {
printf("你已经访问过这里了!\n");
}
int won = 1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (displayed_field[i][j] == '*') {
won = 0;
break;
}
}
if (!won) break;
}
if (won) {
printf("恭喜你,你赢了!\n");
print_field(minefield);
break;
}
}
}
int main() {
play_game();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 3
#define COLS 3
#define MINES 1
char minefield[ROWS][COLS];
char displayed_field[ROWS][COLS];
void initialize_game() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
minefield[i][j] = '0';
displayed_field[i][j] = '*';
}
}
srand(time(0));
int mines_placed = 0;
while (mines_placed < MINES) {
int row = rand() % ROWS;
int col = rand() % COLS;
if (minefield[row][col] == '1') continue;
minefield[row][col] = '1';
mines_placed++;
}
}
void print_field(char field[ROWS][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%c ", field[i][j]);
}
printf("\n");
}
}
void update_displayed_field(int row, int col) {
if (minefield[row][col] == '1') {
printf("你死了,这是一个雷!\n");
print_field(minefield);
exit(1);
}
int neighbors = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < ROWS && j >= 0 && j < COLS && minefield[i][j] == '1') {
neighbors++;
}
}
}
displayed_field[row][col] = '0' + neighbors;
}
void play_game() {
initialize_game();
print_field(displayed_field);
while (1) {
printf("请输入你的位置 (row col): ");
int row, col;
scanf("%d %d", &row, &col);
if (row < 0 || col < 0 || row >= ROWS || col >= COLS) {
printf("非法位置,请输入有效坐标!\n");
continue;
}
if (displayed_field[row][col] == '*') {
update_displayed_field(row, col);
print_field(displayed_field);
} else {
printf("你已经访问过这里了!\n");
}
int won = 1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (displayed_field[i][j] == '*') {
won = 0;
break;
}
}
if (!won) break;
}
if (won) {
printf("恭喜你,你赢了!\n");
print_field(minefield);
break;
}
}
}
int main() {
play_game();
return 0;
}
感谢大家的鼓励,我会致力发更多免费,可复制的程序代码记得关注支持喔~
标签:ROWS,游戏,int,COLS,c++,field,扫雷,col,row From: https://blog.csdn.net/qiaoqiaohonghu/article/details/141637440