头文件Head.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#define COW 9
#define COL 9
#define COWS COW+2
#define COLS COL+2
#define MINE 10
void InitialBoard(char arr[COWS][COLS], int cows, int cols, char set);
void DisplayBoard(char arr[COWS][COLS], int cows, int cols);
void SetMine(char mine[COWS][COLS], int cow, int col, int min);
int MineNumber(char mine[][COLS], int x, int y);
void ExtendCell(char mine[COWS][COLS], char show[COWS][COLS], int x, int y);
game.c
#include"game.h"
void InitialBoard(char arr[COWS][COLS], int cows, int cols, char set)
{
for (int i = 0; i < cows; i++)
{
for (int j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void DisplayBoard(char arr[COWS][COLS], int cow, int col)
{
for (int i = 0; i <= cow; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= cow; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[COWS][COLS], int cow, int col,int min)
{
int count = min;
while (count > 0)
{
int x, y;
x = rand() % col + 1;
y = rand() % cow + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int MineNumber(char mine[COWS][COLS],int x,int y)
{
return mine[x][y - 1] +
mine[x][y + 1] +
mine[x - 1][y] +
mine[x + 1][y] +
mine[x - 1][y - 1] +
mine[x + 1][y - 1] +
mine[x - 1][y + 1] +
mine[x + 1][y + 1]-8*'0';
}
void ExtendCell(char mine[COWS][COLS],char show[COWS][COLS], int x, int y)
{
if (show[x][y] == '0')
{
if (x - 1 > 0 && x - 1 < COWS&&y-1>0&&y-1<COLS&& show[x - 1][y - 1]=='*')
{
show[x - 1][y - 1] = MineNumber(mine, x - 1, y - 1) + '0';
ExtendCell(mine, show, x - 1, y - 1);
//DisplayBoard(show, COW, COL);
}
if (x > 0 && x < COWS && y - 1 > 0 && y - 1 < COLS&&show[x][y - 1] == '*')
{
show[x][y - 1] = MineNumber(mine, x, y - 1) + '0';
ExtendCell(mine, show, x, y - 1);
//DisplayBoard(show, COW, COL);
}
if (x + 1 > 0 && x + 1 < COWS && y - 1 > 0 && y - 1 < COLS && show[x + 1][y - 1] == '*')
{
show[x + 1][y - 1] = MineNumber(mine, x + 1, y - 1) + '0';
ExtendCell(mine, show, x + 1, y - 1);
//DisplayBoard(show, COW, COL);
}
if (x - 1 > 0 && x - 1 < COWS && y > 0 && y < COLS && show[x - 1][y] == '*')
{
show[x - 1][y] = MineNumber(mine, x - 1, y) + '0';
ExtendCell(mine, show, x - 1, y);
//DisplayBoard(show, COW, COL);
}
if (x + 1 > 0 && x + 1 < COWS && y > 0 && y < COLS && show[x + 1][y] == '*')
{
show[x + 1][y] = MineNumber(mine, x + 1, y) + '0';
ExtendCell(mine, show, x + 1, y);
//DisplayBoard(show, COW, COL);
}
if (x - 1 > 0 && x - 1 < COWS && y + 1 > 0 && y + 1 < COLS && show[x - 1][y + 1] == '*')
{
show[x - 1][y + 1] = MineNumber(mine, x - 1, y + 1) + '0';
ExtendCell(mine, show, x - 1, y + 1);
//DisplayBoard(show, COW, COL);
}
if (x > 0 && x < COWS && y + 1 > 0 && y + 1 < COLS && show[x][y + 1] == '*')
{
show[x][y + 1] = MineNumber(mine, x, y + 1) + '0';
ExtendCell(mine, show, x, y + 1);
//DisplayBoard(show, COW, COL);
}
if (x + 1 > 0 && x + 1 < COWS && y + 1 > 0 && y + 1 < COLS && show[x + 1][y + 1] == '*')
{
show[x + 1][y + 1] = MineNumber(mine, x + 1, y + 1) + '0';
ExtendCell(mine, show, x + 1, y + 1);
//DisplayBoard(show, COW, COL);
}
else
return;
}
}
测试运行文件 测试.c
#include"game.h"
void meun()
{
printf("*************************\n");
printf("***** Play:1 No:0 *****\n");
printf("*************************\n");
}
void game()
{
srand((unsigned)time(NULL));
//创建雷区数组
char mine[COWS][COLS];
//创建显示数组
char show[COWS][COLS];
//初始化数组
InitialBoard(mine, COWS, COLS, '0');
InitialBoard(show, COWS, COLS, '*');
//展示游戏脚本
//DisplayBoard(mine, COW, COL);
//展示游戏操作页面
DisplayBoard(show, COW, COL);
//放置雷区
SetMine(mine,COW,COL,MINE);
DisplayBoard(mine, COW, COL);
//排雷
//int easy_count = COW * COL - MINE;
int x, y;
do
{
printf("排雷:输入坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= COL && y >= 1 && y <= COW)
{
if (mine[x][y] == '1')
{
printf("你被炸死\n");
DisplayBoard(mine, COW, COL);
break;
}
else
if (show[x][y] == '*')
{
show[x][y] = MineNumber(mine, x, y) + '0';
//如果周围没有雷,则显示周围一切格子
ExtendCell(mine,show, x, y);
DisplayBoard(show, COW, COL);
//easy_count--;//满了要用函数进行检索,所有格子都不是'*'则赢了
}
else
printf("格子被占,请重新输入:");
}
else
printf("输入范围不对,重新输入");
} while (1);// (easy_count);
//if (easy_count == 0)
//printf("恭喜,胜利!\n");
}
void test()
{
int input;
//是否进入游戏
do
{
//显示菜单
meun();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;//此处的break是跳出switch函数,而不是do while,while循环是由(input)控制
case 0:
break;
default:
printf("输入错误,重新输入:");
}
} while (input);
}
int main()
{
test();
return 0;
}