雷的个数和棋盘大小可以随意改变
一、test.c(主程序)
#include "game.h"void menu()
{printf("****************************\n");printf("***** 开始游戏-1 *****\n");printf("***** 结束游戏-0 *****\n");printf("****************************\n");
}//正式开始游戏
void game()
{char hide[Rows][Cols];char show[Rows][Cols];char set1 = '0';char set2 = '.';// 布置雷的棋盘初始化initialize_board(hide, Rows, Cols, set1);//排雷棋盘初始化initialize_board(show, Rows, Cols, set2);//排雷棋盘打印print_board(show, Row, Col);//布置雷layout_tunter(hide, Row, Col, number);//排查雷mine(hide, show, Rows, Cols, number);
}int main()
{int input = 0;do {//游戏目录menu();srand((unsigned int)time(NULL));printf("是否开始游戏(1/0):");scanf("%d", &input);switch (input){case 1:game();break;case 0:printf("游戏结束\n");break;default:printf("输入无效,请重新输入\n");break;}} while (input);return 0;
}
二、game.h(头文件)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define Row 3
#define Col 3#define Rows Row+2
#define Cols Col+2#define number 1//棋盘初始化
void initialize_board(char board[Rows][Cols], int rows, int cols, char set);//棋盘打印
void print_board(char board[Rows][Cols], int row, int col);//布置雷
void layout_tunter(char hide[Rows][Cols], int row, int col,int num);//排雷
int mine(char hide[Rows][Cols],char show[Rows][Cols], int rows, int cols,int num);
三、game.c(调用函数)
#include "game.h"//布置雷的棋盘初始化
void initialize_board(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 print_board(char board[Rows][Cols], int row, int col)
{printf("--------扫雷-------\n");int j = 0;for (j = 0; j <= col; j++){printf("%d ", j);}printf("\n");int i = 0;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 layout_tunter(char hide[Rows][Cols], int row, int col,int num)
{int i = 0;int count = num;while (count > 0){int s1 = rand() % row;int s2 = rand() % col;if (s1 != 0 && s2 != 0 && hide[s1][s2] != '1'){hide[s1][s2] = '1';count--;}}}//查找周围雷的个数
int find(char hide[Rows][Cols], int x, int y)
{return (hide[x - 1][y] + hide[x - 1][y - 1] + hide[x][y - 1] +hide[x + 1][y - 1] + hide[x + 1][y] + hide[x + 1][y + 1] +hide[x][y + 1] + hide[x - 1][y + 1] - 8 * '0');
}//排雷
int mine(char hide[Rows][Cols], char show[Rows][Cols], int rows, int cols,int num)
{int x = 0;int y = 0;int count = (rows-2) * (cols-2) - num;while (1){printf("请输入您要排查的坐标:");scanf("%d %d", &x, &y);if (x > 0 && x < rows - 1 && y>0 && y < cols - 1){//被排查过就不用排查了if (show[x][y] != '.'){printf("该位置被排查过了,请重新排查!\n");continue;}if (hide[x][y] == '1'){printf("\n");printf("很遗憾,你被炸死了!\n");printf("------地雷分布图-----\n");printf("\n");print_board(hide, Row, Col);break;}else if (hide[x][y] != '1'){int n = find(hide, x, y);show[x][y] = n + '0';print_board(show, Row, Col);count--;}}elseprintf("输入错误,请重新输入\n");//把除了雷以外的都排查完就赢了if (count == 0){printf("\n");printf("恭喜你,获得胜利!!!\n");printf("\n");printf("------地雷分布图-----\n");print_board(hide, Row, Col);printf("\n");count--;break;}}
}