C语言实现扫雷游戏(详解和具体代码)

前言

目录

一、扫雷游戏的实现步骤

1.打印目录

2.初始化棋盘

3.打印棋盘

4.随机布置雷

5.排雷

二、具体代码展示

1.game.h

2.game.c

3.test.c

前言

扫雷游戏是我童年的回忆,我感觉对我的感触还挺深的,在这里实现一下扫雷游戏,致敬我那回不去的童年。

一、实现扫雷的步骤

注:这里各个数组的数都进行了预定义,以便后续进行修改。

#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2

#define COUNTS 12

1.打印目录

void menu()
{
	printf("******************\n");
	printf("****  1.play  ****\n");
	printf("****  0.exit  ****\n");
	printf("******************\n");
}

2.初始化棋盘

这里使用两个二维数组一个用于展示,一个用来布置雷。

(1)布置雷的数组全部初始化为‘0’

(2)展示的数组全部初始化为 ‘*’

这里需要将初始化为我们想要的字符的进行传参。

void init_board(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ret;
		}
	}
}

3.打印棋盘

这里我们可以分别打印行号和列号这样可以使我们更利于分辨具体的排雷位置

void display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= row; 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");
	}

}

4.随机布置雷

注:

这里随机布置雷需要运用到srand((unsinged int) time(NULL))rand用来生成数,其中雷的个数可以随机布置

这里用rand分别对行和列进行取模再进行+1以保证对其数组中的每一个坐标。


void set_sec(char sec[ROWS][COLS], int row, int col)
{
	int count = COUNTS;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (sec[x][y] == '0')
		{
			sec[x][y] = '1';
			count--;
		}
	}
}

5.排查雷

(1)这里只要我们在我们排查所有不是雷的位置之前就进入循环,中途排查到雷直接用break跳出循环即可,在中途需要创建一个数用来进行计数。

(2)这里需要对每个排查的的坐标附近的8个坐标进行排查,这里只需要将8个坐标一对一进行相加,再减去‘0’及可以得到该位置附近雷的个数。

(3)这里我们不妨每次排查雷后对屏幕进行一次清屏,可以保证我们能够更清晰看到我们所排查出的雷的位置。

(4)这里排查雷之后不妨打印一句鼓励的话来表达排查完雷之后的开心。

int fine_sec_count(char sec[ROWS][COLS], int x, int y)
{
	return (sec[x - 1][y] +
		sec[x - 1][y - 1] +
		sec[x][y - 1] +
		sec[x + 1][y - 1] +
		sec[x + 1][y] +
		sec[x + 1][y + 1] +
		sec[x][y + 1] +
		sec[x - 1][y + 1] - 8 * '0');
}

void invest_sec(char sec[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int w = 0;
	while (w < row * col - COUNTS)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (sec[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					display_board(sec, ROW, COL);
					break;
				}
				else
				{
					int count = fine_sec_count(sec, x, y);
					show[x][y] = count + '0';
					system("cls");
					display_board(show, ROW, COL);
					w++;
				}

			}
			else
			{
				printf("该坐标已经排查过\n");
			}
		}

		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (w == row * col - COUNTS)
	{
		printf("恭喜你,排雷成功\n");
		display_board(sec, ROW, COL);
	}
}

二、具体代码展示

1.game.h

#define  _CRT_SECURE_NO_WARNINGS 1
#pragma once

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

#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2

#define COUNTS 12

//初始化棋盘
void init_board(char board[ROWS][COLS], int rows, int cols, char ret);

//打印棋盘
void display_board(char board[ROWS][COLS], int row, int col);

//随机布置雷
void set_sec(char sec[ROWS][COLS], int row, int col);

//排查雷
void invest_sec(char sec[ROWS][COLS], char show[ROWS][COLS],int row, int col);

2.game.c

#include "game.h"

void init_board(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ret;
		}
	}
}

void display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= row; 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");
	}

}

void set_sec(char sec[ROWS][COLS], int row, int col)
{
	int count = COUNTS;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (sec[x][y] == '0')
		{
			sec[x][y] = '1';
			count--;
		}
	}
}

int fine_sec_count(char sec[ROWS][COLS], int x, int y)
{
	return (sec[x - 1][y] +
		sec[x - 1][y - 1] +
		sec[x][y - 1] +
		sec[x + 1][y - 1] +
		sec[x + 1][y] +
		sec[x + 1][y + 1] +
		sec[x][y + 1] +
		sec[x - 1][y + 1] - 8 * '0');
}

void invest_sec(char sec[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int w = 0;
	while (w < row * col - COUNTS)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (sec[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					display_board(sec, ROW, COL);
					break;
				}
				else
				{
					int count = fine_sec_count(sec, x, y);
					show[x][y] = count + '0';
					system("cls");
					display_board(show, ROW, COL);
					w++;
				}

			}
			else
			{
				printf("该坐标已经排查过\n");
			}
		}

		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (w == row * col - COUNTS)
	{
		printf("恭喜你,排雷成功\n");
		display_board(sec, ROW, COL);
	}
}

3.test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void game()
{
	//分别设置两个数组一个用于布置雷,一个用于展示
	char sec[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	
	//初始化棋盘
	init_board(sec, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');

	//随机布置雷
	set_sec(sec, ROWS, COLS);

	//打印棋盘
	display_board(show, ROW, COL);

	//排查雷
	invest_sec(sec, show, ROWS, COLS);
}
void menu()
{
	printf("******************\n");
	printf("****  1.play  ****\n");
	printf("****  0.exit  ****\n");
	printf("******************\n");
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请输入数字:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入:>");
			printf("\n");
			break;
		}
	} while (input);
	return 0;
}

感谢大家能够看到这里,谢谢各位给我的支持!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值