game.h
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MINE_COUNT 10 //Easy
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Itals(char Mxkf[ROWS][COLS], int rows, int cols, char set);
Print(char Mxkf[ROWS][COLS], int row, int col);
Setmine(char Mxkf[ROWS][COLS], int row, int col);
Demine(char Mxkf[ROWS][COLS], char Mxyh[ROWS][COLS], int row, int col);
text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "game.h"
void menu()
{
printf("****************************\n");
printf("********** 1 Play **********\n");
printf("********** 0 Exit **********\n");
printf("****************************\n");
}
void game()
{
char Mxkf[ROWS][COLS] = { 0 };
char Mxyh[ROWS][COLS] = { 0 };
//初始化棋盘
Itals(Mxkf, ROWS, COLS, '0');
Itals(Mxyh, ROWS, COLS, '*');
//打印棋盘
Print(Mxyh, ROW, COL);
//设置地雷
Setmine(Mxkf, ROW, COL);
//Print(Mxkf, ROW, COL);
//扫雷
Demine(Mxkf, Mxyh, 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:
break;
default:
break;
}
} while (input);
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//初始化棋盘
Itals(char Mxkf[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
Mxkf[i][j] = set;
}
}
}
//打印棋盘
Print(char Mxyh[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("\n ");
for (j = 1; j < col+1; j++)
{
printf("%2d", j); //打印列标号
}
printf("\n\n");
for (i = 1; i <= row; i++)
{
printf("%2d ", i); //打印行标号
for (j = 1; j <= col; j++)
{
printf("%c ", Mxyh[i][j]);
}
printf("\n");
}
}
//设置地雷
Setmine(char Mxkf[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = MINE_COUNT;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
//printf("%d %d\n",x,y);
if (Mxkf[x][y] == '0')
{
Mxkf[x][y] = '1';
count--;
}
}
}
//游戏结束
OverSetmine(char Mxkf[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 1; i <= row; i++)
{
for (j = 0; j <= col; j++)
{
//printf("%d %d\n",x,y);
if (Mxkf[i][j] == '0')
Mxkf[i][j] = ' ';
if (Mxkf[i][j] == '1')
Mxkf[i][j] = '@';
}
}
}
//统计周围地雷个数
int Record(char Mxkf[ROWS][COLS], int x, int y)
{
return Mxkf[x - 1][y - 1] +
Mxkf[x - 1][y] +
Mxkf[x - 1][y + 1] +
Mxkf[x][y - 1] +
Mxkf[x][y + 1] +
Mxkf[x + 1][y - 1] +
Mxkf[x + 1][y] +
Mxkf[x + 1][y + 1] - 8 * '0';
}
//展开
void Open(char Mxkf[ROWS][COLS], char Mxyh[ROWS][COLS], int x, int y)
{
int count = Record(Mxkf, x, y);
if (count == 0)
{
int i = 0;
int j = 0;
Mxyh[x][y] = ' ';
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (Mxyh[x+i][y+j]=='*'&& (x + i) >= 1 && (x + i) <= ROWS && (y + j) >= 1 && (y + j) <= COLS)
{
Open(Mxkf, Mxyh, x + i, y + j);
}
}
}
}
else
{
Mxyh[x][y] = count + '0';
}
}
//开始扫雷
Demine(char Mxkf[ROWS][COLS], char Mxyh[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
int win = 0;
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
//输入坐标合法
if (Mxkf[x][y] == '1')
{
system("cls");
printf("你被炸死了,游戏结束\n");
OverSetmine(Mxkf, row, col);
Print(Mxkf, row, col);
printf("\n");
break;
}
else
{
//统计一周地雷总数
Open(Mxkf, Mxyh, x, y);
system("cls");
Print(Mxyh, row, col);
}
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (Mxyh[i][j] == '*')
{
win++;
}
}
}
if (win == MINE_COUNT)
{
printf("***********恭喜通关*********\n\n");
break;
}
}
else
{
printf("坐标非法,请重新输入:>");
}
}
}
展开用到了函数递归,好难啊,琢磨三四个小时还半知半解