C语言简单实现扫雷游戏

本文分享了如何用C语言简单实现扫雷游戏。代码包括三个文件:main.c、C_day09.h和C_day09.c。游戏初始设置为EASY_COUNT 10个雷,首次踩雷不会失败,会自动标记无雷并重新布雷。玩家通过输入坐标(X Y)进行游戏,地图用1和0表示雷区和安全区,*代表未知区域,数字显示周围雷的数量。如有疑问,作者承诺会及时回复。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//C_day09.h
#ifndef C_day_h
#define C_day_h
#include "C_day09.h"
#include <stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>

#define ROW 9
#define COL 9
#define ROWS (ROW+2)
#define COLS (COL+2)
#define EASY_COUNT 10

void game();
void InitBoard(char mine[ROWS][COLS], char set);
void DispBoard(char mine[ROWS][COLS]);
void SetMine(char board[ROWS][COLS]);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS]);
int ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int win);


#endif //C_day_h

//C_day09.c
#include "C_day09.h"

void game(){
	char mine[ROWS][COLS] = {0};
	char show[ROWS][COLS] = {0};
	InitBoard(mine, '0');
	InitBoard(show, '*');
	SetMine(mine);
	DispBoard(mine);
	DispBoard(show);
	FindMine(mine, show);
}

void InitBoard(char board[ROWS][COLS], char set){
	memset(board, set, ROWS*COLS*sizeof(board[0][0]));
}

void DispBoard(char board[ROWS][COLS]){
	int i, j;
	for(i=0; i<=COL; i++)
		printf("%d ", i);
	printf("\n");
	for(i=1; i<=ROW; i++){
		printf("%d ", i);
		for(j=1; j<=COL; j++){
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void SetMine(char board[ROWS][COLS]){
	int x, y;
	int count = EASY_COUNT;
	while(count){
		x = rand()%9+1;
		y = rand()%9+1;
		if(board[x][y] == '0'){
			board[x][y] = '1';
			count--;
		}
	}
}

int GetMineCount(char board[ROWS][COLS], int x, int y){
	return board[x-1][y]+
		board[x-1][y-1]+
		board[x][y-1]+
		board[x+1][y-1]+
		board[x+1][y]+
		board[x+1][y+1]+
		board[x][y+1]+
		board[x-1][y+1]-8*'0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS]){
	int win = 0;
	int count;
	while(win < ROW*COL-EASY_COUNT){
		int x, y;
		printf("请输入坐标->:");
		scanf("%d%d", &x, &y);
		if(win == 0 && mine[x][y] == '1'){
			count = 1;
			mine[x][y] = '0';
			while(count){
				int i = rand()%9+1;
				int j = rand()%9+1;
				if(mine[i][j] == '0' && (i!=x + j!=y) >= 1){
					mine[i][j] = '1';
					count--;
				}
			}
		}
		if(x>=1 && x<=ROW && y>=1 && y<=COL){
			if(mine[x][y] == '1'){
				printf("游戏结束\n");
				DispBoard(mine);
				break;
			}
			else{
				win = ExpandBoard(mine, show, x, y, win);
				DispBoard(show);
			}
		}
		else
			printf("坐标非法\n");
	}
	if(win >= ROW*COL-EASY_COUNT)
		printf("恭喜你游戏胜利\n");
}

int ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int win){
	int count;
	if(x>=1 && x<=ROW && y>=1 && y<=COL){
		count = GetMineCount(mine, x, y);
		if(count == 0 && show[x][y] == '*'){
			show[x][y] = '0';
			win++;
			win = ExpandBoard(mine, show, x-1, y+1, win);
			win = ExpandBoard(mine, show, x+1, y-1, win);
			win = ExpandBoard(mine, show, x-1, y, win);
			win = ExpandBoard(mine, show, x+1, y, win);
			win = ExpandBoard(mine, show, x-1, y-1, win);
			win = ExpandBoard(mine, show, x+1, y+1, win);
			win = ExpandBoard(mine, show, x, y-1, win);
			win = ExpandBoard(mine, show, x, y+1, win);
		}
		else if(show[x][y] == '*'){
			show[x][y] = count + '0';
			win++;
		}
	}
	return win;
}
//main.c
#include "C_day09.h"
void menu(){
	printf("*************************\n");
	printf("***  1.play   0.exit  ***\n");
	printf("*************************\n");
}
void test(){
	int input;
	srand((unsigned int)time(NULL));
	do{
		menu();
		printf("请输入->:");
		scanf("%d", &input);
		switch(input){
			case 1:
				game();
				break;
			case 0:
				break;
			default:
				printf("输入错误,请重新输入.\n");
				break;
		}
	}while(input);
}


int main() {
	test();
    return 0;
}

直接上代码了,总体分为三个文件。

main.c

C_day09.h

C_day09.c

两个源文件和一个头文件;

使用前须知

1. 游戏为展开式扫雷,默认雷个数EASY_COUNT 10可自行适当更改。游戏预定第一次不会炸死,当第一次恰好选择了雷的地方,程序将会自动把此处设置为无雷,并重新开辟一个新的雷点。

2. 自带横纵轴,第一张为内置答案,1代表此处有雷,0代表没有。第二张为玩家玩的图,*代表未知,数字代表周围雷的个数。

3. 输入坐标方式为: X Y(中间有一个空格)。

还有不清楚的欢迎留言询问,我将在看见第一时间回答相关问题。

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值