代码的头文件如下-->
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define COLS 12
#define ROWS 12
#define COL 10
#define ROW 10
#define MINES 10
void init_mines(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col);
void display(char mine[ROWS][COLS], int row, int col);
int checkwin(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
#endif//__GAME_H__
下面是game.c源文件
#include"game.h"
void init_mines(char mine[ROWS][COLS],char show[ROWS][COLS],int rows,int cols,int row,int col)//初始化棋盘
{
int i, x, y;
memset(mine, '0', rows*cols*sizeof(char));//给棋盘赋值--0为雷
memset(show, '.', rows*cols*sizeof(char));
for (i = 0; i < MINES; i++)
{
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
break;
}
}
}
}
void display(char mine[ROWS][COLS],int row,int col)
{
int i, j;
for (i = 1; i <= row; i++)
{
printf("%4d", i);
}
printf("\n");
for (i = 1; i <= col; i++)
{
printf("%2d", i);
for (j = 1; j <= col; j++)
{
printf(" %c ", mine[i][j]);
}
printf("\n");
}
}
static char checkmine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//判断雷的数量
{
int m, n;
char mine_count = '0';
for (m = x - 1; m <= x + 1 ; m++)
{
for (n = y - 1; n <= y + 1; n++)//双重循环检查输入坐标周围
{
if (mine[m][n] == '1')
mine_count++;
}
}
return mine_count;
}
int checkwin(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col,int x,int y)
{
int count = 0;
if (mine[x][y] == '0')
show[x][y] = checkmine(mine, show, x, y);
else return '*';
for (x = 1; x <= row; x++)
{
for (y = 1; y <= col; y++)
{
if (show[x][y] == '.')
count++;
}
}
if (count == MINES)
return 'w';
return 0;
}
在初始化棋盘中,用到了memset函数。其定义如下:
void *memset(void *s, int ch, size_t n);
函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。
此外,还用到了rand函数.
rand 返回 0~RAND_MAX 之间的随机数。要得到一个 0~1.0 之间的随机浮点数.
rand函数在产生随机数前,需要系统提供的生成伪随机数序列的种子,rand根据这个种子的值产生一系列随机数。如果系统提供的种子没有变化,每次调用rand函数生成的伪随机数序列都是一样的。
srand(unsigned seed)通过参数seed改变系统提供的种子值,从而可以使得每次调用rand函数生成的伪随机数序列不同,从而实现真正意义上的"随机"。通常可以利用系统时间来改变系统的种子值,即srand(time(NULL)),可以为rand函数提供不同的种子值,进而产生不同的随机数序列
在此代码中
x = rand() % row + 1;
y = rand() % col + 1;作用是随机产生坐标,方便于雷的随机性!最后则是test.c源文件
#include"game.h"
void menu()
{
printf("-------------------------\n");
printf("------1 . play -------\n");
printf("------2 . exit -------\n");
printf("-------------------------\n");
}
void game()
{
char mines[ROWS][COLS], show[ROWS][COLS], ret;
int x, y;
init_mines(mines, show, ROWS, COLS, ROW, COL);//初始化棋盘
display(mines, ROW, COL);//显示雷区
display(show, ROW, COL);//显示可见区
while (1)
{
printf("输入 x and y:");
scanf_s("%d.%d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)//坐标在棋盘内
{
ret = checkwin(mines, show, ROW, COL, x, y);
if (ret == 'w')
{
display(mines, ROW, COL);
printf("恭喜你,你赢了!\n");
break;
}
else if (ret == '*')
{
display(mines, ROW, COL);
printf("你输了!\n");
break;
}
else
{
display(show, ROW, COL);
printf("\n");
}
}
else printf("输入错误,请重新输入.\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入:");
scanf_s("%d",&input);
switch(input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("error");
break;
}
}
while(input);
return 0;
}
本次编写有很多缺陷。因本人能力不足暂且放下,等实力有所长进之后,会完善!!!!