1. 游戏规则
输入1,开始游戏。输入0,结束游戏。
输入坐标,如果该位置有雷,则游戏失败,可选择重新游戏或者结束。
如果该位置无雷,则显示该位置周围雷的个数,如果该位置周围都没有雷,则展开周围雷的信息。
2. 游戏演示
输入1,开始游戏。首先会显示一些信息。
有雷的信息,打印的界面,设置的雷的位置。
***************** 扫雷 *****************
1 2 3 4 5 6 7 8 9
---------------------------
1 | 0 0 0 0 0 0 0 0 0
2 | 0 0 0 0 0 0 0 0 0
3 | 0 0 0 0 0 0 0 0 0
4 | 0 0 0 0 0 0 0 0 0
5 | 0 0 0 0 0 0 0 0 0
6 | 0 0 0 0 0 0 0 0 0
7 | 0 0 0 0 0 0 0 0 0
8 | 0 0 0 0 0 0 0 0 0
9 | 0 0 0 0 0 0 0 0 0
***************** 扫雷 *****************
***************** 扫雷 *****************
1 2 3 4 5 6 7 8 9
---------------------------
1 | * * * * * * * * *
2 | * * * * * * * * *
3 | * * * * * * * * *
4 | * * * * * * * * *
5 | * * * * * * * * *
6 | * * * * * * * * *
7 | * * * * * * * * *
8 | * * * * * * * * *
9 | * * * * * * * * *
***************** 扫雷 *****************
***************** 扫雷 *****************
1 2 3 4 5 6 7 8 9
---------------------------
1 | 0 0 0 0 0 0 0 0 1
2 | 0 0 0 0 0 1 0 0 1
3 | 1 0 0 0 0 0 1 0 0
4 | 0 0 0 0 0 0 0 0 0
5 | 0 0 0 0 0 0 0 0 0
6 | 0 0 0 0 0 1 0 1 1
7 | 1 0 0 0 0 0 0 0 0
8 | 0 0 1 0 0 0 0 0 0
9 | 0 0 0 0 0 0 0 0 0
***************** 扫雷 *****************
然后开始扫雷,输入坐标 1,1
***************** 扫雷 *****************
1 2 3 4 5 6 7 8 9
---------------------------
1 | 0 0 0 0 1 * * * *
2 | 1 1 0 0 1 * * * *
3 | * 1 0 0 1 2 * * *
4 | 1 1 0 0 0 1 * * *
5 | 0 0 0 0 1 1 * * *
6 | 1 1 0 0 1 * * * *
7 | * 2 1 1 1 * * * *
8 | * * * * * * * * *
9 | * * * * * * * * *
***************** 扫雷 *****************
由于该位置周围都没有雷,就会展开周围雷的信息。
3. 完整代码
完整的代码分为3个文件。
首先是头文件 game.h
这里面是一些宏定义和函数的声明。
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
// 支持维数 1×1 - 99×99
#define ROW 9 // 棋盘维数 9×9
#define COL 9
#define ROWS ROW+2 // 棋盘维数多2,方便判断周围是否有雷
#define COLS COL+2
#define BombNumber 10 // 地雷个数
// 棋盘初始化函数
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set);
// 打印棋盘函数
void DisplayBoard(char Board[ROWS][COLS], int row, int col);
// 设置雷
void SetBomb(char Board[ROWS][COLS], int row, int col);
// 玩游戏
void PlayGame(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
然后是源文件 game.c
函数就是在这个源文件定义的。
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (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");
// 打印列号
printf(" ");
for (i = 1; i <= col; i++)
{
printf("%-2d ", i);
}
printf("\n");
// 打印行线
printf(" ");
for (i = 1; i <= col; i++)
{
printf("---");
}
printf("\n");
// 打印其余行
for (i = 1; i <= row; i++)
{
// 每一行先打印行号和竖线,然后打印数组
printf("%-2d | ", i);
for (j = 1; j <= col; j++)
{
printf("%-2c ", Board[i][j]);
}
printf("\n");
}
printf("***************** 扫雷 *****************\n");
}
void SetBomb(char Board[ROWS][COLS], int row, int col)
{
int count = BombNumber;
while (count)
{
// 随机获取地雷的坐标
int x = rand() % row + 1;
int y = rand() % col + 1;
// 先判断该位置是否有雷,避免重复放置
if (Board[x][y] == '0')
{
Board[x][y] = '1';
count--;
}
}
}
char GetNumber(char mine[ROWS][COLS], int x, int y)
{
char count = '0';
int i = 0;
int j = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
{
count++;
}
}
}
return count;
}
void judge(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int* pwin)
{
// 判断该位置周围雷的个数
char count = '0';
count = GetNumber(mine,x,y);
// 把个数赋给数组,后面打印显示
show[x][y] = count;
// 赋一个新的字符给这个位置,表示这个位置已经递归过了,不要重复递归,不然可能栈溢出
mine[x][y] = 'N';
// 该位置无雷,win的个数要减1
*pwin = *pwin - 1;
// 判断周围是否都没有雷
int i = 0;
int j = 0;
int flag = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
{
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
// 如果都没有雷,则递归
if (flag == 0) // 无雷,则展开
{
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
// 这个判断很重要,不然可能会栈溢出
if (i >= 1 && i <= row && j >= 1 && j <= col && mine[i][j] != 'N')
{
// 递归
judge(mine, show, ROW, COL, i, j, pwin);
}
}
}
}
}
void PlayGame(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
// 规则:
// 输入坐标
// 如果有雷,则失败;无雷,检查周围雷的个数
// 如果该位置没有雷,且周围都没有雷,则递归判断周围雷的信息
int x = 0;
int y = 0;
// 通过判断无雷位置的个数,判断是否赢
int win = ROW * COL - BombNumber;
while (win)
{
// 输入坐标之前,先打印一下雷的位置,避免炸到自己人
//DisplayBoard(mine, ROW, COL);
printf("请输入:>\n");
scanf("%d%d", &x, &y);
// 判断输入坐标是否合法,不合法则重新输入
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
// 如果有雷
if (mine[x][y] == '1')
{
printf("被炸,游戏失败\n");
break;
}
else
{
// 无雷则判断周围雷的个数,并判断是否递归
judge(mine, show, ROW, COL, x, y, &win);
// 打印棋盘,显示周围雷的个数
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("下标错误,请重新输入:>\n");
}
}
// 判断是否赢
if (win == 0)
{
printf("恭喜你赢了\n");
}
}
最后是源文件 test.c
main函数在这个源文件。
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("********************************\n");
printf("*********** 1. play ************\n");
printf("*********** 2. exit ************\n");
printf("********************************\n");
}
void game()
{
// 创建两个二维数组
char mine[ROWS][COLS] = { 0 }; // 存放地雷,地雷用 '1' 表示,无地雷用 '0' 表示
char show[ROWS][COLS] = { 0 }; // 用来显示该位置周围地雷的个数
// 棋盘初始化
InitBoard(mine, ROWS, COLS, '0'); // 初始化为 '0'
InitBoard(show, ROWS, COLS, '*'); // 初始化为 '*'
// 打印棋盘
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
// 设置雷
SetBomb(mine, ROW, COL);
DisplayBoard(mine, ROW, COL); // 先打印一下哪儿有雷,方便调试
// 扫雷
PlayGame(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入:>\n");
scanf("%d", &input);
// 根据input的值,选择是进行游戏还是退出
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
OpenAI可以写程序,很好用!!