函数实现的功能:
1.布置雷:用rand函数生成随机坐标再雷阵里随机布雷
2.排雷:踩到雷炸死,不是雷,统计周围一圈有⼏个雷,并记录信息放到另一个数组
实现原理:
1.雷的信息存储到一个二维数组mine里
2.排出来的雷存到另一个二维数组show里
实现⼀个 9*9 的雷阵,要创建⼀个 11*11的数组 ,目的是⽅便统计最外边一周
的雷的个数。
代码:
测试部分test.c
#include<stdio.h>
#include<windows.h>
#include "game.h"
void menu()
{
printf("###########################\n");
printf("#####欢迎来到扫雷游戏######\n");
printf("#### 1.play 0.exit ######\n");
printf("###########################\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//雷的信息
char show[ROWS][COLS] = { 0 };//排查出的雷的信息
Init(mine, ROWS, COLS,'0' );
Init(show, ROWS, COLS, '*');//初始化
//Print(mine, ROW, COL);
Print(show, ROW, COL);//打印
//布置雷
Setmine(mine, ROW, COL);
Print(mine, ROW, COL);
//排雷
Sweepmine(mine, show, ROW, COL);
}
void test()
{
srand((unsigned)time(NULL));
menu();
int input = 0;
do
{
printf("请选择:>");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入有误\n");
break;
}
}
while (input);
}
int main()
{
test();
system("pause");
return 0;
}
游戏头文件game.h
#ifndef __GAME_H__
#define __GAME_H__
#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_Count 10
//#define MIDDLE_Count 30
//#define DIFFICULT_Count 60
void Init(char board[ROWS][COLS], int rows, int cols, char set);
void Print(char board[ROWS][COLS], int row, int col);
void Setmine(char board[ROWS][COLS], int row, int col);
void Sweepmine(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetmineCount(char mine[ROWS][COLS], int x, int y);
#endif
游戏实现部分game.c
#include"game.h"
void Init(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 Print(char board[ROWS][COLS], int row, int col)//不打印外圈,从1开始
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; 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 ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
void Setmine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_Count;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % row + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int GetmineCount(char mine[ROWS][COLS], int x, int y)//获得周围一圈雷的个数
{
return 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] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}
void Sweepmine(char board[ROWS][COLS], char show[ROWS][COLS],int row, int col)
{
int x = 0;
int y = 0;
int ret = 0;
while (ret != ROW*COL - EASY_Count)
{
printf("请输入排查的坐标:>");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= row)
{
if (board[x][y] == '0')
{
int count = GetmineCount(board, x, y);
show[x][y] = count + '0';
ret++;
Print(show, ROW, COL);
}
else
{
break;
}
}
else
printf("输入错误");
}
if (ret != ROW*COL - EASY_Count)
{
printf("很遗憾,你被炸死了\n");
}
else
{
printf("恭喜你扫雷成功\n");
}
}
}
效果展示:
游戏部分做的不太完整,只是初步的一个实现,希望多多指点!