C语言实现扫雷

写一个简单的程序实现扫雷游戏

首先先创建头文件<game.h>定义常量,以及引入的库文件

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROWS 11
#define COLS 11
#define boom 10

void game();

完成菜单,以及实现菜单选项


#include "game.h"

void test(){
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		printf("*****************************\n");
		printf("**********1.play*************\n");
		printf("**********0.exit ************\n");
		printf("*****************************\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏成功!\n");
			break;
		}
	} while (input);
}

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

我们要选择进入游戏,则进入game(),在game()中调用我们需要实现的功能,

void game() {

	char Boom[ROWS][COLS] = { 0 };

	char Show[ROWS][COLS] = { 0 };

	InitBoard(Boom, ROWS, COLS, '0');

	InitBoard(Show, ROWS, COLS, '*');

	DisplayShow(Show, ROWS, COLS);

	SetBoom(Boom, ROWS, COLS);
	
	Find(Boom, Show, ROWS, COLS);
}

要用2个棋盘,一个棋盘用来随机安置雷的位置,另一个棋盘用来展现给玩家。创建好两个char的二维数组后,首先要对两个棋盘初始化。

InitBoard();

void InitBoard(char Board[ROWS][COLS], int row, int col, char ch) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			Board[i][j] = ch;
		}
	}
}

初始化完成后,要将我们初始化的棋盘展示给玩家

Display();

void DisplayShow(char Board[ROWS][COLS], int row, int col) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row - 1; i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i < row - 1; i++) {
		printf("%d ", i);
		for (j = 1; j < col - 1; j++) {
			printf("%c ", Board[i][j]);
		}
		printf("\n");
	}
}

需要将放置雷的棋盘放好雷,我们用字符’1’来表示雷

SetBoom();

void SetBoom(char Board[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int i = 0;
	int count1 = 0;
	while (count1 < boom) {
		x = (rand() % (row - 2)) + 1;
		y = (rand() % (col - 2)) + 1;
		if (Board[x][y] == '0') {
			Board[x][y] = '1';
			count1++;
		}
	}
}

最后一步就是让玩家输入坐标去躲避雷的位置;如果输入的坐标和雷的坐标一致游戏就结束,如果不一致就输入下一个坐标,直到所有的雷都被找到。

void Find(char Boom[ROWS][COLS], char Show[ROWS][COLS], int row, int col) {

	int x = 0;
	int y = 0;

	while (1) {
		printf("请输入坐标:>");
		scanf("%d%d", &x, &y);

		if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9)) {
			if (Show[x][y] != '*') {
				printf("该位置被占用,请重新输入\n");
			}
			if (Boom[x][y] == '1') {
				printf("被炸死\n");
				break;
			}
			if (Boom[x][y] == '0') {
				open_board(Boom, Show, x, y);

				DisplayShow(Show, ROWS, COLS);
				
				if (is_win(Show, ROWS, COLS) == boom) {
					printf("扫雷成功\n");
					break;
				}
			}
		}
		else {
			printf("坐标输入错误请重新输入\n");
		}
	}
}

在这中间我们要实现点开一个坐标就能够返回它周围8个坐标的雷的数量,返回值传给这个坐标,所以返回值是char类型

SearchBoom();

char SearchBoom(char Boom[ROWS][COLS], int x, int y) {

	return Boom[x - 1][y - 1] + Boom[x][y - 1] + Boom[x + 1][y - 1]
		 + Boom[x - 1][y] + Boom[x][y] + Boom[x + 1][y]
		 + Boom[x - 1][y + 1] + Boom[x][y + 1] + Boom[x + 1][y + 1] - 8 * '0';
}

最后如果这个坐标周围没有雷的话,我们需要把这个坐标的周围展开,直到下个坐标周围有雷为止,所以用递归实现。

open_board();

void open_board(char Boom[ROWS][COLS], char Show[ROWS][COLS], int x, int y) {
	Show[x][y] = SearchBoom(Boom, x, y);
	if (Show[x][y] == '0' && x > 0 && x < ROWS && y>0 && y < COLS) {
		if (Show[x - 1][y - 1] == '*') {
			open_board(Boom, Show, x - 1, y - 1);
		}
		if (Show[x][y - 1] == '*') {
			open_board(Boom, Show, x, y - 1);
		}
		if (Show[x + 1][y - 1] == '*') {
			open_board(Boom, Show, x + 1, y - 1);
		}
		if (Show[x - 1][y] == '*') {
			open_board(Boom, Show, x - 1, y);
		}
		if (Show[x + 1][y] == '*') {
			open_board(Boom, Show, x + 1, y);
		}
		if (Show[x - 1][y + 1] == '*') {
			open_board(Boom, Show, x - 1, y + 1);
		}
		if (Show[x][y + 1] == '*') {
			open_board(Boom, Show, x, y + 1);
		}
		if (Show[x + 1][y + 1] == '*') {
			open_board(Boom, Show, x + 1, y + 1);
		}
	}
}

完成到这里所有的功能就可以实现了

最后放一下扫雷实现代码链接:扫雷的实现

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值