扫雷是需要俩三百行代码才能实现的,对我们初学者来说还是有一定困难的。
首先为了使代码逻辑更清晰,我们需要分为3个文件来去实现分别为game.h;game.c;text.c;
准备工作:头文件:game.h
主要还是写一些预处理信息、一些需要用到的现有的函数的头文件和自定义函数的声明。
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
#define 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 mine[ROWS][COLS], int row, int col, int count);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
源文件game.c
主要是写出函数中的定义
#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 (i = 0; i <= row; i++)
printf("%d ", i);
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 mine[ROWS][COLS], int row, int col, int count)
{
while (count)
{
int x = rand() % ROW + 1;
int y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//计算该坐标周围8个坐标存在的总雷数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int i = 0;
int sum = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
sum += mine[i][j];
}
}
return sum - 9 * '0';
}
//判断周围雷的数量为0的坐标周围雷的存在情况
void JudgeAround(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] != ' ' && i != 0 && i != ROWS - 1 && j != 0 && j != COLS - 1)
{
int count = GetMineCount(mine, i, j);
show[i][j] = count + '0';
if (show[i][j] == '0')
{
show[i][j] = ' ';
JudgeAround(mine, show, i, j);
}
}
}
}
}
//计算剩下未知坐标的数量,当其等于雷的总数时表示扫雷成功
int Remain(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
count++;
}
}
return count;
}
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int ret = 0;
while (ret != COUNT)
{
printf("请输入要排查的坐标:>");
int x = 0;
int y = 0;
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';
if (show[x][y] == '0')
{
show[x][y] = ' ';
JudgeAround(mine, show, x, y);
}
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
ret = Remain(show, ROW, COL);
}
if (ret == COUNT)
{
printf("恭喜你,排雷成功!\n");
DisplayBoard(mine, ROW, COL);
}
}
源文件text.c
最后是text中的代码,使其进行测试运行
#include"game.h"
void menu()
{
printf("|----------------------|\n");
printf("| 1.play |\n");
printf("| 0.exit |\n");
printf("|----------------------|\n");
}
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL, COUNT);
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:
system("cls");
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (input);
return 0;
}