test.c
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include "game001.h"
void menu()
{
printf("*****************\n");
printf("****1.开始游戏****\n");
printf("****0.结束游戏****\n");
printf("*****************\n");
}
void game()
{
//mine数组是用来存放布置好雷的信息
char mine[ROWS][COLS] = { 0 };//'0'
//show数组是用来排查出的雷的信息
char show[ROWS][COLS] = { 0 };//'*'
//分别初始化棋盘
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
//布置雷‘1’
set_mine(mine, ROW, COL);
//打印棋盘
show_board(mine, ROW, COL);//屏蔽掉玩家就可以玩了
show_board(show, ROW, COL);//屏蔽掉可以去程序内测
//排查雷
find_mine(mine, show, ROW, COL);
}
int main()
{
menu();
srand((unsigned int)time(NULL));
int a = 0;
do
{
printf("请选择——>");
scanf("%d", &a);
switch (a)
{
case 0:
printf("结束游戏\n");
break;
case 1:
game();//进入游戏
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (a);//0or非0
return 0;
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
//定义头文件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//函数定义
#define ROW 9//排
#define COL 9//列
#define ROWS ROW+2
#define COLS COL+2
#define EASY 80
//初始化
void init_board(char arr[ROWS][COLS], int rows, int cols, char set);
//打印
void show_board(char arr[ROWS][COLS], int row, int col);
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);
//排查雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include "game001.h"
//初始化
void init_board(char arr[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++)
{
arr[i][j] = set;
}
}
}
//打印
void show_board(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("-------扫雷-------------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("-------扫雷-------------\n");
}
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY;
int x = 0;
int y = 0;
while (count)
{
//雷随即坐标
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//统计坐标周围有几个雷
int get_mine_count(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 find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<col*row-EASY)
{
printf("请输入坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,被炸死了\n");
show_board(mine, ROW, COL);//死的情况把雷显示出来
break;
}
else
{
int count = get_mine_count(mine, x, y);//统计坐标周围有几个雷‘0’的值48
show[x][y] = count + '0';//例如count=1,1+‘0’=‘1’转化成字符放入*棋盘代表一个雷
show_board(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == col * row - EASY)
{
printf("恭喜你,排雷成功\n");
}
}