【C语言】实现小游戏扫雷(可自动扩展)


一、游戏整体思路

1、Minesweeping.c  ------ 测试游戏
2、game.c          ------ 函数实现
3、game.h          ------ 声明函数


二、游戏具体构建

1、创建二个数组,一个数组(mine)用来存放布置好雷的信息,另一个数组(show)存放排查出雷的信息
2、mine数组初始化为‘ 0 ’,布置雷的时候,改成‘ 1’
   show数组初始化为‘ * ’,排查雷后,改为数字字符,即为周围雷的数量
3、布置雷
4、排雷

在这里插入图片描述在这里插入图片描述

代码如下:

#include "game.h"
void menu()
{
	printf("**********************\n");
	printf("****** 1、play *******\n");
	printf("****** 0、exit *******\n");
	printf("**********************\n");
}
void game()
{
	//设计2个数组存放信息
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	
	//初识化棋盘
	//mine初始化全为'0'
	//show初始化全为'*'
	init_chessboard(mine, ROWS, COLS, '0');
	init_chessboard(show, ROWS, COLS, '*');
	
	//布置雷
	set_mine(mine, ROW, COL);
	
	//排雷
	display_chessboard(show, ROW, COL);
	find_mine(mine, show, ROW, COL);
	
}

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("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

二、游戏函数具体实现

1、初始化棋盘

代码如下:

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

2、打印棋盘

代码如下:

void display_chessboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	//列号
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

3、布置雷

void set_mine(char mine[ROWS][COLS], int row, int col)
{
	//布置10个雷
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

4、排查雷

//展示周围8个格子有几个雷
int get_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');
}

//判断输赢
int check_show(char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int i = 0;
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*')
				win++;
		}
	}
	return win;
}

//排查雷
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 (1)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//坐标被排查过
			if (show[x][y] == '*')
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					display_chessboard(mine, ROW, COL);
					break;
				}
				else
				{
					automatic_mine(mine, show, x, y);
					display_chessboard(show, ROW, COL);	
					win = check_show(show, row, col);
					if (win == EASY_COUNT)
						break;
				}
			}
			else
			{
				printf("该坐标已经被排查过了\n");
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (win == EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		display_chessboard(mine, ROW, COL);
	}
}

5、自动展开

void automatic_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	int count = get_mine_count(mine, x, y);
	if (count != 0)
	{
		show[x][y] = count + '0';
	}
	else
	{
		show[x][y] = ' ';
		if (show[x - 1][y] == '*')
		{
			automatic_mine(mine, show, x - 1, y);
		}
		if (show[x - 1][y - 1] == '*')
		{
			automatic_mine(mine, show, x - 1, y - 1);
		}
		if (show[x][y - 1] == '*')
		{
			automatic_mine(mine, show, x, y - 1);
		}
		if (show[x + 1][y - 1] == '*')
		{
			automatic_mine(mine, show, x + 1, y - 1);
		}
		if (show[x + 1][y] == '*')
		{
			automatic_mine(mine, show, x + 1, y);
		}
		if (show[x + 1][y + 1] == '*')
		{
			automatic_mine(mine, show, x + 1, y + 1);
		}
		if (show[x][y + 1] == '*')
		{
			automatic_mine(mine, show, x, y + 1);
		}
		if (show[x - 1][y + 1] == '*')
		{
			automatic_mine(mine, show, x - 1, y + 1);
		}
	}
}

三、游戏实现

1、创建Minesweeping.c文件

代码如下:

#include "game.h"
void menu()
{
	printf("**********************\n");
	printf("****** 1、play *******\n");
	printf("****** 0、exit *******\n");
	printf("**********************\n");
}
void game()
{
	//设计2个数组存放信息
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	//初识化棋盘
	//mine初始化全为'0'
	//show初始化全为'*'
	init_chessboard(mine, ROWS, COLS, '0');
	init_chessboard(show, ROWS, COLS, '*');

	//布置雷
	set_mine(mine, ROW, COL);
	
	//排雷
	display_chessboard(show, ROW, COL);
	find_mine(mine, show, ROW, COL);
	
}

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("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

2、创建game.c文件

代码如下:

#include "game.h"

//初始化棋盘
void init_chessboard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

//打印棋盘
void display_chessboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	//列号
	for (j = 0; j <= col; j++)
	{
		printf("%d ", j);
	}
	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_mine(char mine[ROWS][COLS], int row, int col)
{
	//布置10个雷
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//展示周围8个格子有几个雷
int get_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');
}

//判断结果
int check_show(char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int i = 0;
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*')
				win++;
		}
	}
	return win;
}

//排查雷
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 (1)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//坐标被排查过
			if (show[x][y] == '*')
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					display_chessboard(mine, ROW, COL);
					break;
				}
				else
				{
					automatic_mine(mine, show, x, y);
					display_chessboard(show, ROW, COL);	
					win = check_show(show, row, col);
					if (win == EASY_COUNT)
						break;
				}
			}
			else
			{
				printf("该坐标已经被排查过了\n");
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (win == EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		display_chessboard(mine, ROW, COL);
	}
}

//自动展开
void automatic_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	int count = get_mine_count(mine, x, y);
	if (count != 0)
	{
		show[x][y] = count + '0';
	}
	else
	{
		show[x][y] = ' ';
		if (show[x - 1][y] == '*')
		{
			automatic_mine(mine, show, x - 1, y);
		}
		if (show[x - 1][y - 1] == '*')
		{
			automatic_mine(mine, show, x - 1, y - 1);
		}
		if (show[x][y - 1] == '*')
		{
			automatic_mine(mine, show, x, y - 1);
		}
		if (show[x + 1][y - 1] == '*')
		{
			automatic_mine(mine, show, x + 1, y - 1);
		}
		if (show[x + 1][y] == '*')
		{
			automatic_mine(mine, show, x + 1, y);
		}
		if (show[x + 1][y + 1] == '*')
		{
			automatic_mine(mine, show, x + 1, y + 1);
		}
		if (show[x][y + 1] == '*')
		{
			automatic_mine(mine, show, x, y + 1);
		}
		if (show[x - 1][y + 1] == '*')
		{
			automatic_mine(mine, show, x - 1, y + 1);
		}
	}
}

3、创建game.c文件

#pragma once

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

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

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

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

//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);

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

//自动展开
void automatic_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

//判断结果
int check_show(char show[ROWS][COLS], int row, int col);
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柒个葫芦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值