①game.h
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);//初始化数组
void DisplayBoard(char board[ROWS][COLS], int rows, int cols);//打印数组
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
#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 rows, int cols)
{
int i = 0;
int j = 0;
printf("------扫雷游戏-----\n");
for (i = 0; i < cols - 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i < rows - 1; i++)
{
printf("%d ", i);
for (j = 1; j < cols - 1; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;//1~9
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';//设置为雷
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return (board[x - 1][y] +
board[x - 1][y - 1] +
board[x][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0');
}
void spreadmine(char board[ROWS][COLS], int x, int y)
{
board[x][y] = '_';
board[x - 1][y] = '_';
board[x - 1][y - 1] = '_';
board[x][y - 1] = '_';
board[x + 1][y - 1] = '_';
board[x + 1][y] = '_';
board[x + 1][y + 1] = '_';
board[x][y + 1] = '_';
board[x - 1][y + 1] = '_';
}
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("请输入要排查的坐标->");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
// 是雷
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
DisplayBoard(mine, ROWS, COLS);
break;
}
//排查重复输入
if (show[x][y] == '0'&&show[x][y]=='1'&&show[x][y]=='_')
{
printf("您已经输入过该坐标,请重新输入!\n");
}
//不是雷
else
{
win++;
int count = get_mine_count(mine, x, y);//统计mine数组中x,y周围有几个雷
show[x][y] = count + '0'; //转换为数字字符
if (strcmp(count+'0', '0') == 0)
{
spreadmine(show, x, y);
}
DisplayBoard(show, ROWS, COLS);
}
}
else
{
printf("输入坐标非法,请重新输入\n");
break;
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你排雷成功\n");
DisplayBoard(mine, ROWS, COLS);
}
}
③test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("****************************\n");
printf("***** 1.play 0.exit *****\n");
printf("****************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
//初始化数组内容为指定的内容
//mine数组在没有布置雷的时候初始化为'0'
InitBoard(mine, ROWS, COLS, '0');
//show数组在没有排查出雷的时候初始化为'*'
InitBoard(show, ROWS, COLS, '*');
//设置雷
SetMine(mine, ROW, COL);
DisplayBoard(show, ROWS, COLS);//打印show数组
//DisplayBoard(mine, ROWS, COLS);//打印mine数组
//排查雷
FindMine(mine, show, ROW, COL);
//展开一片
//spreadmine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
//设置随机数生成起点
srand((unsigned)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);
return 0;
}