1.引言 学习了多文件函数的声明和定义,于是此游戏将使用多文件的形式进行书写
main.c //游戏的逻辑
game.h //函数的声明
game.c //函数的实现
一.对扫雷的分析和设计
对于扫雷的功能
1.我们可以建立一个9*9的的格子,在设计10个雷
2.通过坐标进行排查雷
3.找出所有雷,游戏胜利
二.游戏的界面
可以通过do{}while实现一个菜单,输入对应数字进行下一步,如下可以运用switch选择游戏。需要注意的是我们可以将退出设计成0,当输入0是即可退出程序。
三.游戏的实现
要实现这个游戏,我们需要建立两个二维数组一个打印出来,玩家进行扫雷,另一个二维数组我们设置10个雷。所以建立两个数组一个mine雷和show显示出来。
//建立两个二维数组mine show
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
在数组的排查中
难点-1,设置随机雷我们可以用rand函数进行实现,需要注意的是我们需要设置一个起始种子才行srand((unsigned int)time(NULL))设置一个随机的种子,rand的随机数过大我们只需要一个1-9的数字,只需要rand%9+1即可。
srand((unsigned int)time(NULL));
int x = rand() % row+1;
int y = rand() % col+1;
难点-2,当输入坐标,我们需要排查周围的雷的个数但是对于处在边上的数据如何计算他的周围雷的个数
难点-3在排查完多少雷时,其是字符'1'/'0'我们要将其转化成数字只需要
将'0'-'0'=0. '1'-'0'=1所以排查完四周的雷只需要将其-8'0'即可
难点-3
如何胜利,当找出所有的雷是只需要每次输入没雷时count=0,count++即可,当count的值等于9*9-10是获得胜利。
if (win == ROW * COL - easy)
{
printf("you are the winner!!!!\n");
}
四.实际代码实现
//main.c文件
#include<stdio.h>
#include"game.h"
#include<time.h>
void game()
{
//建立两个二维数组mine show
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化
initboard(mine, ROWS, COLS, '0');
initboard(show, ROWS, COLS, '*');
//打印数组就打印9行
print(show, ROW, COL);
//print(mine, ROW, COL);
//放雷
set(mine, ROW, COL);
print(mine, ROW, COL);
//排查雷
find(mine, show, ROW, COL);
}
void mun()
{
printf("1.game\n");
printf("0.exit\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
mun();
scanf_s("%d",&input);
switch (input)
{
case(1):
printf("please play your game!!!\n");
game();
break;
case(0):
printf("exit the game\n");
break;
default:
printf("mistake\n");
break;
}
} while (input);
return 0;
}
//game.c文件
#include"game.h"
void initboard(char board[ROWS][COLS], int row, int col, char set)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
void print(char board[ROWS][COLS], int row, int col)
{
int i, j;
printf("-------minesweeper-------\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("-------minesweeper-------\n");
}
void set(char board[ROWS][COLS], int row, int col)
{
int count = easy;
while (count)
{
int x = rand() % row+1;
int y = rand() % col+1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int my_count(char board[ROWS][COLS], int x, int y)
{
int i, j;
char ret=0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
ret += board[x + i][y + j];
}
}
int a = ret - board[x][y] - 8 * '0';
return a;
}
void find(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
while (ROW * COL - easy)
{
printf("please enter the check coordinates---> ");
int x, y;
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row && y > 0 && y <= col)
{
if (show[x][y] == '*')
{
//是地雷
if (mine[x][y] == '1')
{
printf("oh,you failed\n");
print(mine, ROW, COL);
break;
}
//不是雷
else
{
//数地雷个数
win++;
int ret = my_count(mine, x, y);
show[x][y] = ret + '0';
print(show, ROW, COL);
}
}
else
{
printf("there are already chess here\n");
}
}
else
{
printf("mistake,please enter again\n");
}
if (win == ROW * COL - easy)
{
printf("you are the winner!!!!\n");
print(mine, ROW, COL);
break;
}
}
}
//game.h文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define easy 80
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
void initboard(char board[ROWS][COLS], int row, int col, char set);
void print(char board[ROWS][COLS], int row, int col );
void set(char board[ROWS][COLS], int row, int col );
void find(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col);