一.思路分析
打印菜单
存放雷的信息
初始化棋盘
打印棋盘
布置雷
排查雷
二.代码总结
下面我们直接开始:
1.(test.c)打印菜单:这里面有个time函数一会后面会说明
2.(game.h)建立数组来保存棋子
这里要说明一下,ROW,COL这俩个是我们需要呈现出来的棋盘,而ROWS,COLS是我们程序员防止越界时的棋盘
3.初始化棋盘
‘0’表示没有雷的区域,‘1’则是有雷的区域,‘*’是玩家玩时未被刮开的棋盘
4.棋盘展示
我们在打印棋盘时防止玩家数错需要给没行,每列打印数字,这样就避免了数错的现象
5.布置雷
还记得我们前面那个time函数吧,这里就是rand函数所用到的,随机值的设置需要time函数,这里面EASY_COUNT是设置了10个雷
6.排查雷
我们这里面有个GetMineCount函数是为了计算出一个没有雷的区域旁边有多少个雷的总值
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] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0');
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死\n");
}
else
{
//如果该坐标不是雷,就要统计这个坐标周围有几个雷
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("该位置已经被排查\n");
}
}
else
{
printf("排查的坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
代码总结
game.h
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.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);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i, 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, j = 0;
printf("******** 扫雷 ********\n");
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");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[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] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0');
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死\n");
}
else
{
//如果该坐标不是雷,就要统计这个坐标周围有几个雷
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("该位置已经被排查\n");
}
}
else
{
printf("排查的坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "game.h"
void menu()
{
printf("****************************\n");
printf("******** 1. play *********\n");
printf("******** 2. back *********\n");
}
void game()
{
//mine数组是专门存放布置好的雷的信息
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
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));
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
break;
default:
printf("选择错误请重新选择");
break;
}
return 0;
}
好了以上就是本次的扫雷初级版,这个还有计数器啊等许多都没有做,如果有兴趣可以自己探究一下哦,喜欢的话请三连哦,感谢您的观看