通过不同函数实现不同的功能
这个程序包括两个,c文件一个.h文件
一. game .h文件中进行函数的声明和一些公共的头文件
#pragma once
#include
#include
#include
#define ROW 9 //通过#define定义,使之后行和列使用更加方便
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EAST_COUNT 80//测试
//函数的声明,初始化棋盘.头文件声明 .c文件实现
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set);
//打印棋盘的函数
void DisplayBoard(char board[ROWS][COLS], int rows, int cols);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col);
2.test.c
#define _CRT_SECURE_NO_WARNINGS
//#include
#include"game.h"
void menu() {//主菜单的实现
printf("************************\n");
printf("****** 1.play ******\n");
printf("****** 0.exit ******\n");
printf("************************\n");
}
void game() {
//数组的创建
char mine[ROWS][COLS];//'0'
char show[ROWS][COLS];//'*'
//通过函数,初始化棋盘(数组)
InitBoard(mine, ROWS, COLS,'0');//调用两次InitBoard函数初始化两次棋盘
InitBoard(show, ROWS, COLS,'*');
//棋盘打印
/*DisplayBoard(mine, ROW, COL);*///还雷的数组
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);//把雷放到mine数组中
//DisplayBoard(mine, ROW, COL);//查看雷
//排查雷
FindMine(mine, show, ROW, COL);
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("exit\n");
break;
default:
printf("输入错误,请选择0或1\n");
break;
}
} while (input);
return 0;
}
3.game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//#include
//#include
//#include
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;//遍历数组
for (i = 0; i < rows; i++) {
int j = 0;
for (j = 0; j < cols; j++) {
board[i][j] = set;//完成棋盘的初始化
}
}
}
//棋盘打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("-----扫雷游戏------\n");
for (i = 0; i <= row; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++) {
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----扫雷游戏------\n");
}
//布置雷 对n取模后还剩(n-1)
void SetMine(char mine[ROWS][COLS], int row, int col)//传11*11 操作9*9
{
int count = EAST_COUNT;
while (count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0') {
mine[x][y] = '1';
count--;
}
}
}
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0, y = 0,win=0;
printf("请输入要排查的坐标:>");
while (win
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("dead\n");
DisplayBoard(mine, ROW, COL);
break;
}
else {
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';//count + '0'=1
DisplayBoard(show, ROW, COL);
win++;
}
}
else {
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - EAST_COUNT) {
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
//mine 数组[x][y]周围有几个雷
static int GetMineCount(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');
}
二.源代码
三.简单测试