目录
game.h-----用于存放声明
game.c-----用于存放函数
test.c--------用于进行游戏运行
#pragma once
#include
#include
#include
#include
//符号的定义
//实际棋盘的大小
#define ROW 11
#define COL 11
//展示棋盘的大小
#define ROWS 9
#define COLS 9
//地雷数量
#define MINE 10
//函数的声明
extern void menu();
extern void InitBoard(char board[ROW][COL], int row, int col, char set);
extern void DisplayBoard(char board[ROW][COL], int row, int col);
extern void SetMine(char board[ROW][COL], int row, int col);
extern int get_mine(char board[ROW][COL], int x, int y);
extern int OpenBlankSpace(char board1[ROW][COL], char board2[ROW][COL], int rows, int cols, int x, int y);
extern void FindMine(char board1[ROW][COL], char board2[ROW][COL], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*****************************************\n");
printf("*****************************************\n");
printf("********** 1.start *************\n");
printf("********** 0.exit *************\n");
printf("*****************************************\n");
printf("*****************************************\n");
}
void InitBoard(char board[ROW][COL], int row, int col, char set)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
board[i][j] = set;
}
}
}
void SetMine(char board[ROW][COL], int rows, int cols)
{
srand((unsigned int)time(NULL));
int count = MINE;
while (count)
{
int i = rand() % rows + 1;
int j = rand() % cols + 1;
if (board[i][j] == '0')
{
board[i][j] = '1';
count--;
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < col + 1; ++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");
}
}
int get_mine(char board[ROW][COL], int x, int y)
{
int count = 0;
for (int i = x - 1; i <= x + 1; ++i)
{
for (int j = y - 1; j <= y + 1; ++j)
{
if (board[i][j] == '1')
{
++count;
}
}
}
return count;
}
//用于展开无雷区
int OpenBlankSpace(char board1[ROW][COL], char board2[ROW][COL], int rows, int cols, int x, int y)
{
int count = 0;
if (get_mine(board1, x, y) == 0)
{
board2[x][y] = '0';
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= cols; ++j)
{
if (get_mine(board1, i, j) == 0)
{
board2[i][j] = '_';
++count;
}
}
}
return count + OpenBlankSpaceAgain(board1, board2, ROWS, COLS);
}
else
return 0;
}
//对无雷区进行再次展开,将'_'旁边的数字都显示出来
int OpenBlankSpaceAgain(char board1[ROW][COL], char board2[ROW][COL], int rows, int cols)
{
int count = 0;
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= cols; ++j)
{
if (board2[i][j] == '_')
{
for (int n = -1; n <= 1; ++n)
{
for (int m = -1; m <= 1; ++m)
{
if (i+n >= 1 && i+n<= rows && j + m >= 1 && m + j <= cols && board2[i + n][j + m] == '*')
{
board2[i + n][j + m] = '0' + get_mine(board1, i + n, j + m);
++count;
}
}
}
}
}
}
return count;
}
void FindMine(char board1[ROW][COL], char board2[ROW][COL], int rows, int cols)
{
int x = 0, y = 0, count = 0;
while (count < ROWS * COLS - MINE)
{
printf("请输入行列坐标(行 列):>");
scanf("%d %d", &x, &y);
//对输入的坐标进行合法性判断
if (x >= 1 && x <= rows && y >= 1 && y <= cols && board2[x][y] == '*')
{
//判断是否踩雷
if (board1[x][y] == '1')
{
system("cls");
printf("BOOM!游戏结束!\n");
DisplayBoard(board2, ROWS, COLS);
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= cols; ++j)
{
if (board1[i][j] == '1')
{
board2[i][j] = '#';
}
else
{
if (board2[i][j] == '*');
{
board2[i][j] = '_';
}
}
}
}
board2[x][y] = 'B';
DisplayBoard(board2, ROWS, COLS);
printf("\n\n'#'表示地雷,'B'表示触爆地点。\n");
printf("--------------------------------------------------------\n\n\n");
break;
}
else
{
board2[x][y] = get_mine(board1, x, y) + '0';
count += OpenBlankSpace(board1, board2, ROWS, COLS, x, y);
system("cls");
//作弊代码
//DisplayBoard(board1, ROWS, COLS);
DisplayBoard(board2, ROWS, COLS);
}
++count;
}
else
printf("格式有误!重新输入!\n");
}
if (count == ROWS * COLS - MINE)
{
printf("\n-----------\n");
printf("你赢了!\n");
printf("\n-----------\n");
Sleep(3000);
system("cls");
}
else if (count > ROWS * COLS - MINE)
printf("未知的bug出现了!");
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
//创建地雷棋盘
char board1[ROW][COL];
//创建演示棋盘
char board2[ROW][COL];
//格式化棋盘
InitBoard(board1, ROW, COL, '0');
InitBoard(board2, ROW, COL, '*');
//随机布置雷
SetMine(board1, ROWS, COLS);
//打印棋盘
//作弊代码DisplayBoard(board1, ROWS, COLS);
DisplayBoard(board2, ROWS, COLS);
//显示周围的地雷数
FindMine(board1, board2, ROWS, COLS);
}
void main()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("-------------------\n");
printf("正在进入扫雷游戏!\n");
printf("-------------------\n");
Sleep(1500);
system("cls");
game();
break;
case 0:
printf("See you next time!");
break;
}
} while (input);
}