1. 设计思路
要实现一个扫雷小游戏。
- 设计一个
ROW*COL
的棋盘,用二维数组存放棋盘信息。 - 棋盘中元素要么是雷,要么不是雷。我们令有雷的位置存放1,没有雷的位置存放0。
-
- 排查雷时,如果要排查位置不是雷,就在此位置显示其周围雷的个数,可能是1,这就和前一步有雷的位置存放的1产生了冲突,这个“1”就有了歧义。
- 所以,我们设计两个大小一样的棋盘,一个布置雷,一个排查雷(放置周围雷的个数),显示给玩家的是排查雷的那个棋盘。
- 而显示给玩家的棋盘,玩家不知道有没有雷,所以最初都存放
*
,由于*
是字符,所以布置雷的那个棋盘中存放的1和0最好是'1'
和'0'
。 - 显示某个位置(不是雷)周围雷的个数前,要统计周围雷的个数。这就是前面为什么存放
'1'
和‘0’
的原因了。只要将这个位置周围存放的所有字符1或0都转换为数字1或0,再相加,得到的数字就是周围雷的个数。 - 由于我们是将这个位置周围所有的8个元素都相加,所以可能存在越界(如下图位置2和位置3),因此,棋盘最好再扩增两行两列。
2. 具体实现
2.1 game.h
,存放相关的函数声明。
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10 //难度系数 雷的个数
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch);
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col);//这里依然是ROWS COLS 11x11
//布置雷
void SetThunder(char board[ROWS][COLS], int row, int col);
//排查雷
void FindThunder(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col);
2.2 game.c
,存放相关函数的实现
2.2.1 棋盘初始化
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = ch;
}
}
}
2.2.2 打印棋盘
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("--------扫雷--------\n");
int i = 0;
int j = 0;
for (j = 0; j <= col; 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");
}
2.2.3 布置雷
//布置雷
void SetThunder(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--;
}
}
}
2.2.4 排雷
//统计周围雷的个数
int get_mine_thunder_count(char board[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
int ret = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
ret += board[i][j] - '0';
}
}
return ret;
}
//排查雷
void FindThunder(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int not_thunder = 0;//找到的非雷的个数
while (not_thunder < ROW * COL - EASY_COUNT)
{
printf("请输入要排查的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//判断所输入的坐标是否已被排查过
if (show_board[x][y] != '*')
{
printf("该坐标已被排查过,请勿重复排查\n");
continue;
}
//是雷
if (mine_board[x][y] == '1')
{
printf("很遗憾,你踩到雷了\n");
DisplayBoard(mine_board, ROW, COL);
break;
}
else//不是雷
{
not_thunder++;
//统计mine_board数组中(x,y)坐标周围雷的个数
int count = get_mine_thunder_count(mine_board, x, y);
show_board[x][y] = count + '0';//转换成数字字符
DisplayBoard(show_board, ROW, COL);
}
}
else
{
printf("输入坐标不合法,请重新输入\n");
}
}
if (not_thunder == ROW * COL - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine_board, ROW, COL);
}
}
3. 源代码
//game.h
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10 //难度系数 雷的个数
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch);
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col);//这里依然是ROWS COLS 11x11
//布置雷
void SetThunder(char board[ROWS][COLS], int row, int col);
//排查雷
void FindThunder(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col);
//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_thunder[ROWS][COLS] = { 0 };//存放布置好的雷的信息
char show_thunder[ROWS][COLS] = { 0 };//存放排查出的雷的信息
//初始化棋盘
//没有布置雷 存放'0'
InitBoard(mine_thunder,ROWS,COLS,'0');
//不显示雷
InitBoard(show_thunder, ROWS, COLS, '*');
//布置雷 存放'1'
SetThunder(mine_thunder,ROW,COL);
//DisplayBoard(&mine_thunder, ROW, COL);
DisplayBoard(show_thunder, ROW, COL);
//排查雷
FindThunder(mine_thunder, show_thunder, 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;
}
//game.h
#include "game.h"
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = ch;
}
}
}
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("--------扫雷--------\n");
int i = 0;
int j = 0;
for (j = 0; j <= col; 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 SetThunder(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 get_mine_thunder_count(char board[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
int ret = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
ret += board[i][j] - '0';
}
}
return ret;
}
//排查雷
void FindThunder(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int not_thunder = 0;//找到的非雷的个数
while (not_thunder < ROW * COL - EASY_COUNT)
{
printf("请输入要排查的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//判断所输入的坐标是否已被排查过
if (show_board[x][y] != '*')
{
printf("该坐标已被排查过,请勿重复排查\n");
continue;
}
//是雷
if (mine_board[x][y] == '1')
{
printf("很遗憾,你踩到雷了\n");
DisplayBoard(mine_board, ROW, COL);
break;
}
else//不是雷
{
not_thunder++;
//统计mine_board数组中(x,y)坐标周围雷的个数
int count = get_mine_thunder_count(mine_board, x, y);
show_board[x][y] = count + '0';//转换成数字字符
DisplayBoard(show_board, ROW, COL);
}
}
else
{
printf("输入坐标不合法,请重新输入\n");
}
}
if (not_thunder == ROW * COL - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine_board, ROW, COL);
}
}