头文件:(test.h)
#ifndef _MINE_H_
#define _MINE_H_
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <time.h>//调用标准库或系统中的头文件
#pragma warning(disable:4996)//VS中解决scanf函数问题
#define ROW 12
#define COL 12
#define MINES 20//宏定义
void Menu();
void Game();
void SetMine(char mine_board[][COL], int row, int col);
void PlayGame(char show_board[][COL], char mine_board[][COL], int row, int col);
int GetMineNum(char mine_board[][COL], int i, int j);
void ShowBoard(char show_board[][COL], int row, int col);//函数声明
#endif
源文件:(test.c)
#include "mine.h"
void Menu()//菜单
{
printf("###########################################\n");
printf("## 1. Play 2. Exit ##\n");
printf("###########################################\n");
printf("Please Select:> ");
}
static int GetIndex(int start, int end)//[1, 10]
{
return rand() % (end - start + 1) + start;//随机埋雷
}
void SetMine(char mine_board[][COL], int row, int col)
{
srand((unsigned long)time(NULL));
int mine_num = MINES;
while (mine_num){
int i_index = GetIndex(1, col - 2);
int j_index = GetIndex(1, col - 2);
if (mine_board[i_index][j_index] == '0'){
mine_board[i_index][j_index] = '1';
mine_num--;
}
}
}
int GetMineNum(char mine_board[][COL], int i, int j)//确定选择坐标值旁边雷的个数
{
//'0 ' + '1'+'1'+'0'...+'0'
return mine_board[i - 1][j - 1] + mine_board[i - 1][j] + \
mine_board[i - 1][j + 1] + mine_board[i][j - 1] + \
mine_board[i][j + 1] + mine_board[i + 1][j - 1] + \
mine_board[i + 1][j] + mine_board[i + 1][j + 1] - 8 * '0';
}
void ShowBoard(char show_board[][COL], int row, int col)//显示界面
{
int i = 1;
int j = 1;
printf(" ");
for (; i <= col - 2; i++){
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= col - 1; i++){
printf("----");
}
printf("\n");
for (i = 1; i <= row - 2; i++){
printf("%2d|", i);
for (j = 1; j <= col - 2; j++){
printf(" %c |", show_board[i][j]);
}
printf("\n");
int k = 1;
for (; k <= col - 1; k++){
printf("----");
}
printf("\n");
}
}
void PlayGame(char show_board[][COL], char mine_board[][COL], int row, int col)
{
int i = 0;
int j = 0;
int total = (ROW - 2)*(COL - 2);//total还剩多少位置没扫
while (1){
ShowBoard(show_board, row, col);
printf("Please Enter Pos<x, y>: ");
scanf("%d%d", &i, &j);
if (i >= 1 && i <= row - 2 && j >= 1 && j <= col - 2){
if (mine_board[i][j] == '0'){
int num = GetMineNum(mine_board, i, j);
show_board[i][j] = num + '0';
total--;
}
else{
ShowBoard(mine_board, row, col);
printf("Game Over! You Lose :(!\n");
break;
}
}
else{
printf("Enter Error, Try Again!\n");
continue;
}
if (total == MINES){
printf("You Win:)\n");
break;
}
}
}
void Game()
{
char show_board[ROW][COL];//显示界面
char mine_board[ROW][COL];//雷区界面
memset(show_board, '*', sizeof(show_board));//未扫区域用“*”表示
memset(mine_board, '0', sizeof(mine_board));//扫完区域用“0”表示
SetMine(mine_board, ROW, COL);//扫完区域赋值
PlayGame(show_board, mine_board, ROW, COL);//游戏过程
}
执行文件(main.c)
#include "mine.h"
int main()
{
int select = 0;
int quit = 0;
while (!quit){
Menu();
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
printf("I Am Quit!\n");
quit = 1;
break;
default:
printf("Select Error! Try Again!\n");
break;
}
}
return 0;
}