//头文件
#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<windows.h>
#include<time.h>
#define ROW12
#define COL12
#define NUM20voidmenu();voidgame();
#endif
//game.c
#include"game.h"voidmenu()//显示菜单{printf("***********************\n");printf("* 1.Play 2.Exit *\n");printf("***********************\n");}voidSetMines(char mine[][COL], int row, int col)//设置雷{
int count =NUM;while(count){
int x =rand()%10+1;
int y =rand()%10+1;if(mine[x][y]=='0')//b保证不重复的20个雷{
mine[x][y]='1';
count--;}}}voidShowLine(int nums){printf("---");for(int i =0; i < nums; i++){printf("-");}printf("\n");}voidShowBoard(char show[][COL], int row, int col)//显示棋盘{printf(" ");for(int i =1; i < row -1; i++)//row=12{printf(" %2d ", i);}printf("\n");ShowLine(2* col + col +4);for(int i =1; i < row -1; i++){printf("%2d|", i);for(int j =1; j < col -1; j++){printf(" %c |", show[i][j]);}printf("\n");ShowLine(2* col + col +4);}}
int GetMines(char mine[][COL], int row, int col, 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';}voidgame(){srand((unsigned long)time(NULL));
char show_board[ROW][COL];
char mine_board[ROW][COL];memset(show_board,'*',sizeof(show_board));memset(mine_board,'0',sizeof(mine_board));SetMines(mine_board,ROW,COL);
int count =(ROW-2)*(COL-2)-NUM;
int x, y;do{ShowBoard(show_board,ROW,COL);printf("请输入子的坐标1~10:");scanf_s("%d %d",&x,&y);if(x<1|| x>10|| y<1|| y>10){printf("输入位置不合法!请重新输入!\n");continue;}if(show_board[x][y]!='*'){printf("该位置已排除!请重新输入!\n");continue;}if(mine_board[x][y]=='1'){printf("你被炸死了!");break;}
int num =GetMines(mine_board,ROW,COL, x, y);
show_board[x][y]= num +'0';
count--;system("cls");//清屏}while(count >0);if(count ==0){printf("你赢了!\n");}printf("下面是雷区分布!\n");ShowBoard(mine_board,ROW,COL);}//main.c
#include"game.h"//使用自己定义的头文件用双引号
int main(){
int quit =0;while(!quit){menu();
int x;printf("请输入:");scanf_s("%d",&x);switch(x){case1:game();break;case2:quit =1;break;default:printf("输入有误!请重新输入!\n");continue;}}system("pause");return0;}