前言:通过前面学的分支结构,循环结构,二维数组,函数,今天来做应该简单版的扫雷
1. 扫雷游戏分析和设计
1.1 扫雷游戏的功能说明
• 使⽤控制台实现经典的扫雷游戏
• 游戏可以通过菜单实现继续玩或者退出游戏
• 扫雷的棋盘是9*9的格⼦ • 默认随机布置10个雷
• 可以排查雷 ◦ 如果位置不是雷,就显⽰周围有⼏个雷
◦ 如果位置是雷,就炸死游戏结束
◦ 把除10个雷之外的所有⾮雷都找出来,排雷成功,游戏结束
1.2 游戏的分析和设计
1.扫雷的过程中,布置的雷和排查出的雷的信息都需要存储,又因为我们需要在9*9的棋盘上布置雷的信息和排查雷,我们⾸先想到的就是创建⼀个9*9的数组来存放信息。
2.为了区分坐标处有没有雷,我们把没有雷的地方设置成0,有雷的地方设置成1。
问题分析:
我们怎么得到坐标周围雷的个数?我们可以遍历统计周围8个坐标,那有的超出格子怎么办,超出格子就是数组的越界访问了,这时候可以扩展多2行和2列,但是玩家还是操作和看到9x9的格子,这样即使是四角处都能正常遍历周围8格。
1.3 游戏的实现
我们要创建3个文件:game.h存放函数声明;game.c存放游戏函数定义;test.c 写游戏的测试逻辑。
1.3.1 game.h头文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
//重置棋盘
void InitBoard(char board[ROWS][COLS], int row, int col, 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);
//求周围雷的个数
int CountMine(char board[ROWS][COLS], int row, int col);
1.3.2 game.c
#define _CRT_SECURE_NO_WARNINGS
#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");
for (j = 0; j < 10; 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 = 10;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if ('1' != board[x][y])
{
board[x][y] = '1';
count--;
}
}
}
int CountMine(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 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 - 10)
{
printf("请输入你要排查的坐标x y:>");
scanf("%d%d", &x, &y);
if (x > 0 && x <= 9 && y > 0 && y <= 9)
{
if (mine[x][y] == '1')
{
printf("踩到雷了,你死亡\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
win++;
printf("还要排%d个雷\n", row * col - 10 - win);
int c = CountMine(mine, x, y);
show[x][y] = c + '0';
DisplayBoard(show, ROW, COL);
}
}
else
printf("输入错误,重新输入\n");
}
if (win == row * col - 10)
{
printf("恭喜你,游戏胜利\n");
DisplayBoard(mine, ROW, COL);
}
}
1.3.3 test.c主函数
#define _CRT_SECURE_NO_WARNINGS
#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 };
//重置
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//放置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
//扫雷游戏的主函数
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请输入:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("扫雷游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("输入错误,重新输入\n");
break;
}
} while (input);
return 0;
}