C语言实现扫雷小游戏-要求第一次不被炸死

本文介绍了如何使用C语言编写扫雷小游戏,并确保玩家在第一次尝试时不会失败。涵盖主函数文件、game.h头文件和game.c实现文件的内容,以及最终的游戏结果显示。

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

扫雷小游戏-第一次不能被炸死

主函数文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
	printf("*****************\n");
	printf("*****0.exit*******\n");
	printf("*****1.play******\n");
	printf("*****************\n");
}
void game()
{
	char mineInfo[ROWS][COLS]; 
	char mine[ROWS][COLS]; 
	InitBoard(mineInfo, ROWS, COLS, '*');   //初始化玩家界面 *覆盖
	InitBoard(mine, ROWS, COLS, '0');
	ShowBoard(mineInfo, ROWS, COLS);   //打印玩家界面
	SetMine(mine, ROW, COL);   //布雷
	FindMine(mineInfo, mine, 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:
			break;
		}
	} while (input);
	return 0;
}

game.h头文件

#pragma once   //防止重复引用

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

#define ROW  9
#define COL  9  //内部的扫雷区域

#define ROWS  ROW+2
#define COLS  COL+2  //在周围加一圈为坐标的数字


#define MINENUM 10  //初始化雷的个数

void InitBoard(char board[][COLS], int rows, int cols, char set);   //初始化
void ShowBoard(char board[][COLS], int rows, int cols);   //打印
int  GetMine(char mine[][COLS], int x, int y);
void SetMine(char mine[][COLS], int row, int col);   //设置雷
void SafeMine(char mine[][COLS], char mineInfo[][COLS], int row, int col);  //保证第一次不被炸死
void FindMine(char mineInfo[][COLS], char mine[][COLS], int row, int col);   //排雷

game.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[][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 ShowBoard(char board[][COLS], int rows, int cols)   //打印
{
	int i = 0;
	int j = 0;
	printf("======================\n");
	for (i = 0; i < rows - 1; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i < rows - 1; i++)
	{
		printf("%d ", i);
		for (j = 1; j < cols - 1; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("======================\n");
}//打印

void SetMine(char mine[][COLS], int row, int col)   //设置雷的位置
{
	int count = MINENUM;  //雷的个数 
	int x = 0;
	int y = 0;
	while (count != 0)
	{
		x = rand() % row + 1;//[0-9)  [1,10)
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

void SafeMine(char mine[][COLS], char mineInfo[][COLS], int row, int col)   //输入第一次要输入的坐标进行排查 如果为雷 则把该位置重新设置为没有雷 并在其他位置重新布雷
{
	int x = 0;
	int y = 0;
	int ret = 1;
	int ch = 0;
	printf("请输入要排查的坐标:>");
	scanf("%d%d", &x, &y);
	if (mine[x][y] == '1')//如果第一次输入坐标为雷将坐标改为不是雷
	{
		mine[x][y] = '0';//获取周围雷的个数
		ch = GetMine(mine, x, y);
		mineInfo[x][y] = ch + '0';
		while (ret)//在其余有空的地方重新设置一个雷
		{
			x = rand() % 9 + 1;
			y = rand() % 9 + 1;
			if (mine[x][y] == '0')
			{
				mine[x][y] = '1';
			}
			ret--;
		}
	}
	ShowBoard(mineInfo, row, col);
}

 static int GetMine(char mine[][COLS], int x, int y)
{
	return mine[x - 1][y - 1] +
		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] - 8 * '0';
}


void FindMine(char mineInfo[][COLS], char mine[][COLS], int row, int col)   //扫雷
{
	int x = 0;
	int y = 0;
	int count = 0;
	SafeMine(mine, mineInfo, row, col); //先对要输入的坐标进行排查 如果为雷 将雷取消 并在其他位置重新布雷
	while (count  < row * col - MINENUM)
	{
		printf("请输入你的坐标:");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (mine[x][y] == '1')
			{
				printf("你被炸死了\n");
				break;
			}
			else
			{
				int mineNum = GetMine(mine, x, y);
				mineInfo[x][y] = mineNum + '0';
				count++;
				ShowBoard(mineInfo, ROWS, COLS);
			}
		}
		else
		{
			printf("坐标不合法\n");
		}
	}
	if (count == row * col - MINENUM)
	{
		printf("扫雷成功\n");
	}
}

结果展示:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值