C语言实现扫雷
mineClearanceMain.c 代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "mineClearanceGame.h"
void mine_clearance_game_menu() {
printf("***************************\n");
printf("****** 1.play *****\n");
printf("****** 0.exit *****\n");
printf("***************************\n");
}
void mine_clearance_game() {
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(mine , ROWS , COLS , '0'); // 初始化棋盘
init_board(show , ROWS , COLS , '*');
set_mine(mine, ROW, COL); // 设置雷
print_board(mine , ROW , COL); // 打印棋盘
//print_board(show , ROW , COL);
find_mine(mine , show , ROW , COL); // 排查雷
}
void mine_clearance_game_test() {
int input = 0;
srand((unsigned int)time(NULL));
do {
mine_clearance_game_menu();
printf("请选择:");
scanf("%d" , &input);
switch (input) {
case PLAY:
mine_clearance_game();
break;
case EXIT:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
}
int main() {
mine_clearance_game_test();
return 0;
}
mineClearanceGame.h 代码
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define PLAY 1
#define EXIT 0
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define DEFAULT_MINE 78
void init_board(char board[ROWS][COLS] , int rows , int cols , char fill_element);
void print_board(char board[ROWS][COLS], int row , int col);
void set_mine(char board[ROWS][COLS], int row, int col);
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row , int col);
mineClearanceGame.c 代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "mineClearanceGame.h"
void init_board(char board[ROWS][COLS], int rows, int cols, char fill_element) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
board[i][j] = fill_element;
}
}
}
void print_board(char board[ROWS][COLS], int row, int col) {
printf("-----------扫雷-----------\n");
int i = 0;
for (i = 0; i <= col; i++) {
printf("%d " , i);
}
printf("\n");
for (i = 1; i <= row; i++) {
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++) {
printf("%c " , board[i][j]);
}
printf("\n");
}
printf("-----------扫雷-----------\n");
}
void set_mine(char board[ROWS][COLS], int row, int col) {
int mine_count = DEFAULT_MINE;
while (mine_count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0') {
board[x][y] = '1';
mine_count--;
}
}
}
static int around_mine_count(char mine[ROWS][COLS], int x, int y) {
return (
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y + 1] +
mine[x + 1][y + 1] +
mine[x + 1][y] +
mine[x + 1][y - 1] +
mine[x][y - 1] +
mine[x - 1][y - 1] - 8 * '0'
);
}
// 递归展开
static void spread (char mine[ROWS][COLS] , char show[ROWS][COLS] , int x , int y) {
// 检查边界
if (x >= 1 && x <= ROW && y >= 1 && y <= COL) {
if (!around_mine_count(mine, x, y)) {
if (show[x][y] != ' ') {
show[x][y] = ' ';
// 说明周围一圈没有雷
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
spread(mine, show, x + i, y + j);
}
}
}
}
else {
// 周围有雷不展开
show[x][y] = around_mine_count(mine, x, y) + '0';
}
}
}
static bool is_win(char show[ROWS][COLS], int row, int col) {
int count = 0;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
if (show[i][j] != '*') {
count++;
}
}
}
// TODO:测试bug
//printf("\n%d-%d %d\n" , count , (row * col - DEFAULT_MINE), count >= (row * col - DEFAULT_MINE));
return count >= (row * col - DEFAULT_MINE);
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - DEFAULT_MINE) {
printf("请输入坐标:");
scanf("%d %d" , &x , &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("恭喜你,被炸死了\n");
print_board(mine , row , col);
return;
}
else {
spread(mine , show , x, y);
/*
if (++win == row * col - DEFAULT_MINE) {
printf("扫雷成功\n");
print_board(mine, row, col);
return;
}
print_board(show , row ,col);
*/
//printf("\n%d\n" , is_win(show, row, col));
if (is_win(show, row, col)) {
printf("扫雷成功\n");
print_board(mine, row, col);
return;
}
print_board(show, row, col);
}
}
else {
printf("坐标不合法\n");
}
}
}