1.头文件的定义
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//game.h
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化棋盘
void InitBoard(char board[ROWS][COLS],int rows,int cols);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS],int row,int col);
//布置雷
void SetMine(char board[ROWS][COLS],int row,int col);
//展开空白部分
void ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
//标记雷
void MarkMine(char show[ROWS][COLS]);
//排查雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
2.函数部分定义
#define _CRT_SECURE_NO_WARNINGS 1
//game.c
#include "game.h"
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set) {//初始化棋盘
int i = 0;
for (i = 0; i < rows; i++) {
int j = 0;
for (j = 0; j < cols; j++) {
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS],int row,int col) {//打印棋盘
int i = 0;
printf("--------扫雷游戏--------\n");
for (i = 0; i <= col; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= col; i++) {
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS],int row,int col) {
//随机生成10个雷
//生成随机的坐标,布置雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0') {
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y) {//得到一个坐标九宫格内雷的数量
return(mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] +
mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0');
}
//如果查找的九宫格坐标内无雷,则以递归展开空白无雷部分
void ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) {
int count = GetMineCount(mine, x, y);
if (count == 0) {
show[x][y] = ' ';
if (x - 1 >= 0 && y - 1 >= 0 && show[x - 1][y - 1] == '*')
ExpandBoard(mine, show, x - 1, y - 1);
if (x - 1 >= 0 && show[x - 1][y])
ExpandBoard(mine, show, x - 1, y);
if (x - 1 >= 0 && y + 1 <= COL && show[x - 1][y + 1] == '*')
ExpandBoard(mine, show, x - 1, y + 1);
if (y - 1 >= 0 && show[x][y - 1] == '*')
ExpandBoard(mine, show, x, y - 1);
if (y + 1 <= COL && show[x][y + 1] == '*')
ExpandBoard(mine, show, x, y + 1);
if (x + 1 <= ROW && y - 1 >= 0 && show[x + 1][y - 1] == '*')
ExpandBoard(mine, show, x + 1, y - 1);
if (x + 1 <= ROW && show[x + 1][y] == '*')
ExpandBoard(mine, show, x + 1, y);
if (x + 1 <= ROW && y + 1 <=COL && show[x + 1][y + 1] == '*')
ExpandBoard(mine, show, x + 1, y + 1);
}
else {
show[x][y] = count + '0';
}
}
//判断胜利条件
int IsWin(char show[ROWS][COLS]) {
int count = 0;
for (int i = 1; i <= ROW; i++) {
for (int j = 1; j <= COL; j++) {
if (show[i][j]=='#') {
count++;
}
}
}
if (count == EASY_COUNT) {
return 1;
}
else {
return 0;
}
}
//将自己认为是雷的坐标标记为#
void MarkMine(char show[ROWS][COLS]) {
int input = 0;
int x =0, y = 0;
printf("1.标记 0.不标记\n");
do {
printf("是否进行标记:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入要标记的坐标:>");
scanf("%d%d", &x, &y);
show[x][y] = '#';
DisplayBoard(show, ROW, COL);
break;
case 0:
printf("退出标记\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
}
//排查是否为雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col) {
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT) {
printf("请输入要查找的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("很遗憾,你被炸死了\n");
DisplayBoard(show, ROW, COL);
printf("\n");
break;
}
else {
ExpandBoard(mine, show, x, y);
DisplayBoard(show, ROW, COL);
int Winner = IsWin(show);
if (Winner == 1) {
printf("恭喜你,排雷成功\n");
printf("\n");
return;
}
MarkMine(show);
Winner = IsWin(show);
if (Winner == 1) {
printf("恭喜你,排雷成功\n");
printf("\n");
return;
}
}
}
else
{
printf("坐标违法,重新输入\n");
}
}
}
3.主函数定义部分
#define _CRT_SECURE_NO_WARNINGS 1
//test.c
#include "game.h"
void menu() {
printf("******************************\n");
printf("*********** 1. play **********\n");
printf("*********** 0. exit **********\n");
printf("******************************\n");
}
void game() {
char mine[ROWS][COLS];//存放雷
char show[ROWS][COLS];//存放排查出雷的信息
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
}
4.运行部分展示