本篇博客介绍一下:棋盘格大小为9*9的扫雷小游戏
1.完成扫雷游戏:首先就是要知道扫雷游戏的规则,它的运行逻辑
2.编写代码之前你需要考虑如何布置雷和如何排雷。
这里我们就是要设置两个数组:数组1放置我们布置的雷,数组2放置排查周围8个坐标的个数,为了方便代码的调用,用字符'1' 表示雷,字符'0'表示不是雷。
2.该扫雷是用数组来表示的,就要考虑到数组访问是否会越界,因为设计的扫雷为9*9的大小,为了防止访问越界,就将上述两个数组进行扩大一周变为11*11,因为设计的扫雷为9*9的大小,我们在9*9的范围之外肯定不会设置雷,雷只会随机分布在9*9的棋盘格的范围内。我们这样设计的好处是我们在计算9*9边界坐标周围含雷数量的时候,不会发生数组越界。其他的在打印棋盘我们传入的数组参数大小都是11*11,只是访问的范围是9*9。
3.排查雷我们需要考虑三点
(1)看我们排查的坐标是否越界
(2)排查的坐标如果是雷,游戏结束。
(3)排查的坐标如果不是雷,统计该坐标周围8个坐标雷的个数,放在数组2中。(其中一个小细节,就是整形向字符的转化,就是将整形-'0'=该整形的字符形式)打印出排雷信息,游戏继续,要排查多少次呢,这要根据你设置的雷的个数。比如9*9的棋盘,设置10个雷,那就要排查9*9-10=71次。若进行71次排雷后,排雷胜利✌。
4.代码实现
在编译环境中建议分成三个文件,会使代码阅读更加清晰(头文件.h、游戏文件.c和游戏主函数.c)。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define Easy_count 10 //设置雷的个数
#define Easy_num 71 //设置排雷最多次数
//初始化棋盘 声明
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘 声明
void Displayboard(char board[ROW][COL], 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 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)
{
printf("--------扫雷-------\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);//打印棋盘坐标的列
}
printf("\n");
for (int 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 x = 0;
int y = 0;
int count = Easy_count;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
//统计周围雷的个数
//int Getminenum(char mine[ROWS][COLS], int x, int y)//统计周围雷的个数方法1
//{
//
// return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
// mine[x][y - 1] + mine[x][y + 1] +
// mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]-8*'0';
//}
int Getminenum(char mine[ROWS][COLS], int x, int y)//统计周围雷的个数方法2
{
int count = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
count += mine[x + i][y + j] - '0';
}
}
return count;
}
//排查雷
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int num = Easy_num;
while (num)
{
printf("请选择:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (mine[x][y] == '1')
{
printf("游戏结束\n");
Displayboard(mine, ROW, COL);
break;
}
else
{
show[x][y] = Getminenum(mine, x, y) + '0';
num--;
Displayboard(show, ROW, COL);
if (num == 0)
{
printf("恭喜您排除全部雷,扫雷胜利!\n");
Displayboard(mine, ROW, COL);
break;
}
else
{
printf("您还没有排除全部雷,请继续扫雷!");
}
}
}
else
{
printf("输入错误x(1~9),y(1~9),请重新输入\n");
}
}
}
//**************************************************************
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(show, ROW, COL);//打印排查雷的信息
//Displayboard(mine, ROW, COL);//打印初始化雷的信息
//设置雷
Setmine(mine, ROW, COL);
//Displayboard(mine, ROW, COL);//打印设置雷的信息
//排雷
Findmine(mine, show, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入选项:> ");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请再次选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}