1 数据结构的分析
在扫雷过程中,布置雷和排查雷的同时需要储存,所以需要一定的数据结构来存储这些信息。
在9*9的棋盘上排雷时,我们可以在布置有雷的地方存放1,没有的地方存放0。
假设我们在排查(3,5)这个坐标时,我们访问周围的一圈8个黄色位置,统计周围个数是1
假设我们在排查(8,3)这个坐标时,我们访问周围的一圈8个黄色位置,统计周围个数是过程中,最下面的三个坐标会发生越界,为了防止越界我们需要在设计时,给数组扩大一圈。雷还是布置在9*9的 坐标上,周围增加多一圈不需要布雷,这样就很好解决了越界问题。
为了在排雷过程中,不容易混杂,能够更方便排查出。我们专门给一个棋盘(对应一个数组mine)存放布置好的雷的信息,在给另一个棋盘(对应另外一个数组show)存放排查出雷的信息。这样就做到了互不干扰。把雷布置到mine数组,在mine数组中排查雷,排查出的数据存放在show数组,并且打印show数组的信息后期排查参考。
对应的数组:
1 char mine[11][11] = {0}; //用来存放布置好的雷的信息
2 char show[11][11] = {0}; //用来存放排查出雷的个数信息
2 文件结构设计
把复杂的代码,按照功能分不同模块实现。这样的好处是:逻辑清晰,方便协作,方便代码隐藏。
所以我们把代码分为三个模块:
1 test.c //文件写游戏的测试逻辑
2 game.c //文件写游戏中函数的实现
3 game.h //文件写游戏需要的数据类型和函数声明
3 扫雷游戏代码的实现
(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_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
(2)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 <= 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)
{
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 + 1] + mine[x][y + 1] - 8 * '0');
}
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_s("%d %d", &x, &y);
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';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
(3)game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.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);