#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义雷区大小(可修改)
#define ROWS 9
#define COLS 9
#define ROWS_ALL ROWS + 2 // 实际数组开更大,方便边界判断
#define COLS_ALL COLS + 2
#define MINE_COUNT 10 // 雷的数量
// 函数声明
void menu();
void game();
void InitBoard(char board[ROWS_ALL][COLS_ALL], int rows, int cols, char set);
void SetMine(char mine[ROWS_ALL][COLS_ALL], int rows, int cols);
void ShowBoard(char board[ROWS_ALL][COLS_ALL], int rows, int cols);
int CountMine(char mine[ROWS_ALL][COLS_ALL], int x, int y);
void FindMine(char mine[ROWS_ALL][COLS_ALL], char show[ROWS_ALL][COLS_ALL], int rows, int cols);
// 菜单函数
void menu() {
printf("*************************\n");
printf("***** 1. play *****\n");
printf("***** 0. exit *****\n");
printf("*************************\n");
}
// 初始化棋盘(mine数组初始化为'0',show数组初始化为'*')
void InitBoard(char board[ROWS_ALL][COLS_ALL], int rows, int cols, char set) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
board[i][j] = set;
}
}
}
// 布置雷(随机生成MINE_COUNT个雷,存放在mine数组中)
void SetMine(char mine[ROWS_ALL][COLS_ALL], int rows, int cols) {
int count = MINE_COUNT;
srand((unsigned int)time(NULL)); // 随机数种子
while (count > 0) {
int x = rand() % rows + 1; // 生成1~rows的随机行
int y = rand() % cols + 1; // 生成1~cols的随机列
if (mine[x][y] == '0') { // 确保当前位置没有雷
mine[x][y] = '1'; // '1'代表雷
count--;
}
}
}
// 打印棋盘(只打印有效区域:1~ROWS行、1~COLS列)
void ShowBoard(char board[ROWS_ALL][COLS_ALL], int rows, int cols) {
int i, j;
// 打印列号
printf(" ");
for (i = 1; i <= cols; i++) {
printf("%d ", i);
}
printf("\n");
// 打印棋盘内容
for (i = 1; i <= rows; i++) {
printf("%d ", i); // 打印行号
for (j = 1; j <= cols; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 统计坐标(x,y)周围的雷数
int CountMine(char mine[ROWS_ALL][COLS_ALL], int x, int y) {
// 周围8个格子的和('1'-'0'=1,'0'-'0'=0)
return (mine[x-1][y-1] + mine[x-1][y] + mine[x-1][y+1]
+ mine[x][y-1] + mine[x][y+1]
+ mine[x+1][y-1] + mine[x+1][y] + mine[x+1][y+1]) - 8 * '0';
}
// 扫雷逻辑(玩家输入坐标,判断是否踩雷/统计周围雷数)
void FindMine(char mine[ROWS_ALL][COLS_ALL], char show[ROWS_ALL][COLS_ALL], int rows, int cols) {
int x, y;
int win = 0; // 已排查的非雷格子数
while (win < rows * cols - MINE_COUNT) { // 非雷格子全部排查完则胜利
printf("请输入要排查的坐标(行 列): ");
scanf("%d %d", &x, &y);
// 坐标合法性判断
if (x >= 1 && x <= rows && y >= 1 && y <= cols) {
if (mine[x][y] == '1') { // 踩雷
printf("很遗憾,你被炸死了!\n");
ShowBoard(mine, rows, cols); // 显示所有雷的位置
break;
} else if (show[x][y] == '*') { // 该位置未被排查过
int count = CountMine(mine, x, y); // 统计周围雷数
show[x][y] = count + '0'; // 转换为字符存入show数组
ShowBoard(show, rows, cols);
win++;
} else {
printf("该坐标已被排查,请重新输入!\n");
}
} else {
printf("坐标非法,请重新输入!\n");
}
}
if (win == rows * cols - MINE_COUNT) {
printf("恭喜你,扫雷成功!\n");
ShowBoard(mine, rows, cols);
}
}
// 游戏核心逻辑
void game() {
// 定义两个二维数组:mine存雷信息('1'是雷,'0'是非雷);show存玩家看到的信息(初始为'*')
char mine[ROWS_ALL][COLS_ALL] = {0};
char show[ROWS_ALL][COLS_ALL] = {0};
// 初始化棋盘
InitBoard(mine, ROWS_ALL, COLS_ALL, '0');
InitBoard(show, ROWS_ALL, COLS_ALL, '*');
// 布置雷
SetMine(mine, ROWS, COLS);
// 测试:打印雷的位置(实际游戏可注释)
// ShowBoard(mine, ROWS, COLS);
// 显示初始棋盘
ShowBoard(show, ROWS, COLS);
// 扫雷
FindMine(mine, show, ROWS, COLS);
}
// 主函数
int main() {
int input = 0;
do {
menu(); // 打印菜单
printf("请选择: ");
scanf("%d", &input);
switch (input) {
case 1:
printf("扫雷游戏开始!\n");
game(); // 进入游戏
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,请重新输入!\n");
break;
}
} while (input != 0); // 选择0则退出循环
return 0;
}
澎湖县网站建设_网站建设公司_原型设计_seo优化