一、游戏的功能说明
- 游戏可以通过用户输入来游戏进行控制
- 扫雷的棋盘是矩形的,且每个坐标下是否有雷是不可见的
- 棋盘下隐藏了诺干颗雷,用户选择了含雷的坐标游戏就失败了
- 如果所选坐标无雷,将显示该坐标周围3*3矩阵内雷的个数
- 如果所选坐标以及周围一个雷都没有,将展开一篇无雷的坐标
二、游戏的分析和设计
1.数据结构的分析
在游戏过程中,我们需要把99棋盘中每个坐标的含雷情况和排查情况经行存储,
第一反应是使用二维数组来存储,即创建一个99的二维数组来存储游戏信息,存储数字0代表该坐标无雷,存储数字1代表该坐标有雷
因为在排查边缘坐标时会发生数组越界的问题,所以将存储数组初始化为11*11的大小,以避免数组越界的问题,而将原本越界的坐标设置为无雷
以上的数组用来存储棋盘含雷的信息
char mine[11][11] = {0};
而我们还需要再创建一个相同大小的数组来存储排雷情况(用来向玩家展示),在初始化棋盘时用’*'来隐藏每个坐标下的含雷情况以及周围雷的个数,若玩家排查该坐标再向玩家展示,如果该坐标含雷则展示游戏失败并且展示整个含雷数组,如果该坐标不含雷则展示该坐标周围雷的个数(如果周围一个雷也没有将会展开周围一片不含雷坐标),游戏继续,直至用户踩雷,或排查了所有不含雷的坐标.
char show[11][11] = {0};
三、文件设计结构
test.c //⽂件中写游戏的测试逻辑
game.c //⽂件中写游戏中函数的实现等
game.h //⽂件中写游戏需要的数据类型和函数声明等
四、扫雷游戏的代码实现
1.功能实现的相关函数
初始化棋盘 // 将棋盘初始化为传入的set字符
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 <= row; 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--;
}
}
}
统计周围雷的个数//循环遍历实现(周围是一个3*3的矩阵)
int GetMineCount(char mine[ROWS][COLS], int x, int y) {
int cnt = 0;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
cnt += mine[x+i][y+j] - '0';//因为需要得到的是数值1,所以需要用字符'1'-字符'0'
}
}
return cnt;
}
排查雷
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);
while (getchar() != '\n')
continue;
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
// break;
}
else {
//该位置不是雷,就统计这个坐标周围有几个雷
// int count = GetMineCount(mine, x, y);
// show[x][y] = count + '0';
if (show[x][y] == '*')//防止重复排查
{
test(mine,show,x,y);
DisplayBoard(show, ROW, COL);
win++;
}else{
printf("[%d,%d],已经被检查过了\n",x,y);
}
}
}
else {
printf("坐标不合规,重新输入\n");
}
}
if (win == ROW * COL - EASY_COUNT) {
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
展开一片(递归实现)
void test(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y){
if (x<1 || x>9 || y<1 || y > 9){//如果越界则结束递
return;
}
int count = GetMineCount(mine, x, y);
if (count != 0){//如果周为有雷就不再展开
show[x][y] = count + '0';
return;
} else if(count == 0){
show[x][y] = '0';
for (int i = -1; i <= 1; ++i) {//如果无雷就遍历排查周围的坐标
for (int j = -1; j <= 1; ++j) {
if (show[x+i][y+j] == '*'){//如果排查过就不再排查
test(mine,show,x+i,y+j);
}
}
}
}
}
2.源文件
main.c
//
// Created by 善浅_ on 2023/8/14.
//
#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];//存放布置好的雷
char show[ROWS][COLS];//存放排查出的雷的信息
//初始化棋盘
//1. mine数组最开始是全'0'
//2. show数组最开始是全'*'
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//1. 布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//2. 排查雷
FindMine(mine, show, ROW, COL);
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择:>");
scanf("%d", &input);
while (getchar() != '\n')
continue;
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
game.c
//
// Created by 善浅_ on 2023/8/14.
//
#define _CRT_SECURE_NO_WARNINGS 1
#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 <= row; 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) {
int cnt = 0;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
cnt += mine[x+i][y+j] - '0';
}
}
return cnt;
}
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);
while (getchar() != '\n')
continue;
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
// break;
}
else {
//该位置不是雷,就统计这个坐标周围有几个雷
// int count = GetMineCount(mine, x, y);
// show[x][y] = count + '0';
if (show[x][y] == '*')//防止重复排查
{
test(mine,show,x,y);
DisplayBoard(show, ROW, COL);
win++;
}else{
printf("[%d,%d],已经被检查过了\n",x,y);
}
}
}
else {
printf("坐标不合规,重新输入\n");
}
}
if (win == ROW * COL - EASY_COUNT) {
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
void test(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y){
if (x<1 || x>9 || y<1 || y > 9){//如果越界则结束递
return;
}
int count = GetMineCount(mine, x, y);
if (count != 0){
show[x][y] = count + '0';//如果周为有雷就不再展开
return;
} else if(count == 0){
show[x][y] = '0';
for (int i = -1; i <= 1; ++i) {//如果无雷就遍历排查周围的坐标
for (int j = -1; j <= 1; ++j) {
if (show[x+i][y+j] == '*'){
test(mine,show,x+i,y+j);
}
}
}
}
}
game.h
//
// Created by 善浅_ on 2023/8/14.
//
#pragma once
#ifndef MINE_GAME_H
#define MINE_GAME_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void test(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
#endif //MINE_GAME_H
五、实际运行
代码完善后咱们运行下看看实际效果
输入1 开始游戏
输入要排查的坐标
得到的结果
咱们把雷的个数设置为1再来测试一遍