第一步:打印菜单
![](https://img-blog.csdnimg.cn/f7e506517cd64219a31f320f2316df5d.png)
第二步:写出主函数(是否玩扫雷)
![](https://img-blog.csdnimg.cn/c6af7d07cb664802bd527a43942ce120.png)
第三步:确定扫雷游戏需要的框架
![](https://img-blog.csdnimg.cn/925a5009e23c42b1b3eb1209951ae51a.png)
第四步:函数+数组实现扫雷游戏
第一个函数先实现:确定边界
因为打印9x9需要用11x11的边界来解决后面溢出的情况
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
第二个函数打印棋盘:
void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印棋盘
{
int i = 0;
int j = 0;
printf("-----------扫雷游戏-----------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i<=row; i++)
{
printf("%d ", i);
for (j = 1; j<=col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------扫雷游戏-----------\n");
}
第三个函数:布置雷
void setmine(char board[ROWS][COLS], int row, int col)
{
int count = CC;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
第四个函数:游戏判定
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return board[x - 1][y] +
board[x - 1][y - 1] +
board[x ][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0';
}
void FidMine(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 - CC)
{
printf("请输入一个坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{ //如果是雷
if (show[x][y] != '*')
{
printf("该坐标已经使用过了\n");
}
else
{
if (mine[x][y] == '1')
{
printf("踩雷了\n");
DisplayBoard(mine, ROW, COL);
break;
}
//如果不是雷
else
{//统计mine函数中x,y旁边有几个雷
win++;
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//转换成数字字符
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入错误\n");
}
}
if (win == row * col - CC)
{
printf("排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
完全代码的呈现:
头文件:
#pragma once
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define CC 10
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);//传了11x11的格子 用了9x9的格子
void setmine(char board[ROWS][COLS], int row, int col);
void FidMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
主函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("***********************************\n");
printf("********* 1.play **************\n");
printf("********* 0.exit **************\n");
printf("***********************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息
char show[ROWS][COLS] = { 0 };//存放排查出雷的信息
//mine数组在没放雷的时候都是‘0’
InitBoard(mine, ROWS, COLS, '0');
//show数组在没排查雷的时候都是‘*’
InitBoard(show, ROWS, COLS, '*');
//设置雷
setmine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);打印棋盘
//DisplayBoard(mine, ROW, COL);
//排查雷
FidMine(mine,show,ROW,COL);
}
int main()
{
int input = 0;
//设置随机数的生成
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始扫雷\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
}
} while (input);
return 0;
}
实现游戏的函数文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印棋盘
{
int i = 0;
int j = 0;
printf("-----------扫雷游戏-----------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i<=row; i++)
{
printf("%d ", i);
for (j = 1; j<=col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------扫雷游戏-----------\n");
}
void setmine(char board[ROWS][COLS], int row, int col)
{
int count = CC;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return board[x - 1][y] +
board[x - 1][y - 1] +
board[x ][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0';
}
void FidMine(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 - CC)
{
printf("请输入一个坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{ //如果是雷
if (show[x][y] != '*')
{
printf("该坐标已经使用过了\n");
}
else
{
if (mine[x][y] == '1')
{
printf("踩雷了\n");
DisplayBoard(mine, ROW, COL);
break;
}
//如果不是雷
else
{//统计mine函数中x,y旁边有几个雷
win++;
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//转换成数字字符
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入错误\n");
}
}
if (win == row * col - CC)
{
printf("排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}