一.扫雷游戏的思路
1)布置雷
2)排查雷
如果这个位置是雷,就炸死了,游戏结束。
不是雷,就告诉坐标周围有几个雷。
把所有不是雷的地方都找到,剩下的都是雷。游戏结束。
二.扫雷游戏的设计
设计三个文件:
test.c用来测试扫雷游戏,设计游戏菜单。
game.c game.h用来扫雷游戏的实现
1.布置雷
1)设计棋盘
由于要在棋盘上布置雷和排查雷,只在一个数组里面存储这么多的信息有些杂乱。
为了防止混淆雷和非雷的信息以及布置雷的信息
我们就需要创建两个二维数组
一个用来存放雷,一个存放排查出雷的信息。
第一个数组存放雷,开始全部初始化成0,设置十个雷,将雷设置成1。
第二个数组开始全部初始化为*,保持神秘,之后排查时调用,展示排查出雷的信息。
所以为了方便统一数组类型,都将数组设置为字符数组,0,1放入‘’中。
注意:
由于棋盘大小为9*9,防止越界访问,应创建11*11的数组。(遇到拐角的时候显示雷附近的雷数出错),如图

2) 设计四个函数
分别用于初始化,打印棋盘,布置雷和排查雷。
其中用set表示字符数组内存放的初始的字符内容。
#define TOTAL 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 rows, int cols);//布置雷
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int rows, int cols);//排查雷
2.排查雷
1)布置十个雷放在随机位置
如何产生随机数?
#include <stdlib.h>
#include <time.h>
int main()
{
//使⽤time函数的返回值设置种⼦
srand((unsigned int)time(NULL))
}
要生成a-b之间的随机数:a + rand()%(b-a+1)
eg.生成100-200:100+rand()%101
那么布置随机十个雷的位置就很容易了
void Setmine(char board[ROWS][COLS],int row,int col)
{
int count = TOTAL;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
2)统计雷个数的函数的设计
如下图 ,想要统计雷的个数,需要找到周围八个各自的坐标所存储的字符,并减去‘0’转为数字,相加计算就得到了个数。


三.扫雷游戏的实现
1.game.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once //让头文件只包含一次
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define TOTAL 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 rows, int cols);//布置雷
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int rows, int cols);//排查雷
2.test.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 <= row; 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 = TOTAL;
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 - 1] +mine[x][y - 1] +mine[x + 1][y - 1] +mine[x + 1][y]
+mine[x + 1][y + 1] +mine[x][y + 1] +mine[x - 1][y + 1] +mine[x - 1][y]) - (8 * '0');
}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int flag = 0;
while (flag < row * col - TOTAL)
{
printf("请输入需要排查的坐标\n");
scanf("%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);
flag++;
}
}
else
{
printf("坐标输入错误,请重新输入\n");
}
}
if (flag = row * col - TOTAL)
{
printf("恭喜你,排雷成功\n");
Displayboard(mine, row, col);
}
}
3.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;
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 <= row; 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 = TOTAL;
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 - 1] +mine[x][y - 1] +mine[x + 1][y - 1] +mine[x + 1][y]
+mine[x + 1][y + 1] +mine[x][y + 1] +mine[x - 1][y + 1] +mine[x - 1][y]) - (8 * '0');
}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int flag = 0;
while (flag < row * col - TOTAL)
{
printf("请输入需要排查的坐标\n");
scanf("%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);
flag++;
}
}
else
{
printf("坐标输入错误,请重新输入\n");
}
}
if (flag = row * col - TOTAL)
{
printf("恭喜你,排雷成功\n");
Displayboard(mine, row, col);
}
}
1万+

被折叠的 条评论
为什么被折叠?



