头文件
调用头文件和声明函数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>#define ROW 9
#define COL 9#define ROWS ROW+2
#define COLS COL+2#define EASY_CONT 10//声明函数
//初始化棋盘函数
void InitBoard(char board[ROWS][COLS], int rows, int cols, char mark);
//打印棋盘函数
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);
游戏主体
#include "game.h"//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char mark)
{for (int i = 0; i < rows; i++){for(int j = 0; j < cols; j++){board[i][j] = mark;}}
}//打印棋盘
void Displayboard(char board[ROWS][COLS], int row, int col)
{printf("--------------扫雷游戏---------------\n");for (int i = 0; i <= row; i++){printf("%d ",i);}printf(" \n");for (int i = 1; i <= row; i++){printf("%d ",i);for (int j = 1; j <= col; j++){printf("%c ",board[i][j]);}printf(" \n");}printf("--------------扫雷游戏---------------\n");
}//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{int cont = EASY_CONT;while (cont){int x = rand() % 9 + 1;int y = rand() % 9 + 1;if (mine[x][y] == '0'){mine[x][y] = '1';cont--;}}
}//计算雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +mine[x][y - 1] + mine[x][y + 1] +mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0');
}//排雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{int x = 0, y = 0;int win = 0;while (win<row*col-EASY_CONT){printf("请输入扫描坐标:");scanf("%d %d", &x, &y);if (x >=1 && x<= row && y >= 1 && y <= col){if (mine[x][y] == '1'){printf("GAME OVER~~~~~\n");Displayboard(mine, ROW, COL);break;}else{int cont = GetMineCount(mine, x, y);show[x][y] = cont + '0';win++;Displayboard(show, ROW, COL);}}else{printf("//其他区域为安全区域,请输入排查区域//\n");}}if (win == row*col - EASY_CONT){printf("此区域以安全,排雷成功\n");Displayboard(mine, ROW, COL);}
}
界面和玩法
菜单和选项
#include "game.h"void menu()
{printf("************************************\n");printf("************1.开始游戏**************\n");printf("************0.退出游戏**************\n");printf("************************************\n");printf("************************************\n");
}int game()
{//初始化棋盘char mine[ROWS][COLS];char show[ROWS][COLS];InitBoard(mine, ROWS, COLS, '0');InitBoard(show, ROWS, COLS, '*');//打印棋盘//Displayboard(mine, ROW, COL);Displayboard(show, ROW, COL);//埋雷SetMine(mine,ROW,COL);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("退出游戏\n");break;default:printf("非法输入,请重新输入\n");break;}} while (input);return 0;
}