游戏:扫雷

本文介绍了一个使用纯C语言实现的扫雷游戏项目。该项目包括游戏初始化、显示、雷区设定、玩家交互等功能,并提供了两种难度模式供玩家选择。

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

纯C语言实现游戏扫雷

  • game.h
#ifndef __game_h__
#define __game_h__

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

#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define Easy 10
#define Hard 30

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void PrintBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col, int count);
void ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count);

#endif  //__game_h__
  • game.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(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 PrintBoard(char board[ROWS][COLS], int row, int col)   //打印棋盘
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= col; i++)
	{
		printf(" %d ", i);
		if (i != col)
		{
			printf("|");
		}
	}
	printf("\n---|---|---|---|---|---|---|---|---|---\n");
	for (i = 1; i <= row; i++)
	{
		printf(" %d |", i);
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j != col)
			{
				printf("|");
			}
		}
		if (i != row)
		{
			printf("\n---|---|---|---|---|---|---|---|---|---");
		}
		printf("\n");
	}
	printf("\n");
}

void SetMine(char board[ROWS][COLS], int row, int col, int count)   //设置雷
{
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

int Surround(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 Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)  //展开
{
	if (Surround(mine, x, y) == 0)
	{
		show[x][y] = ' ';
		if ((x - 1) > 0 && show[x - 1][y] == '*')
		{
			Expand(mine, show, x - 1, y);
		}
		if ((x - 1) > 0 && (y - 1) > 0 && show[x - 1][y - 1] == '*')
		{
			Expand(mine, show, x - 1, y - 1);
		}
		if ((y - 1) > 0 && show[x][y - 1] == '*')
		{
			Expand(mine, show, x, y - 1);
		}
		if ((x + 1) <= ROW && (y - 1) > 0 && show[x + 1][y - 1] == '*')
		{
			Expand(mine, show, x + 1, y - 1);
		}
		if ((x + 1) <= ROW && show[x + 1][y] == '*')
		{
			Expand(mine, show, x + 1, y);
		}
		if ((x + 1) <= ROW && (y + 1) <= COL && show[x + 1][y + 1] == '*')
		{
			Expand(mine, show, x + 1, y + 1);
		}
		if ((y + 1) <= COL && show[x][y + 1] == '*')
		{
			Expand(mine, show, x, y + 1);
		}
		if ((x - 1) > 0 && (y + 1) <= COL && show[x - 1][y + 1] == '*')
		{
			Expand(mine, show, x - 1, y + 1);
		}
	}
	else
	{
		show[x][y] = Surround(mine, x, y) + '0';
	}
}

void ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int count)   //排雷
{
	int flag = 0;
	while (1)
	{
		int i = 0;
		int j = 0;
		int sum = 0;
		int x = 0;
		int y = 0;
		printf("请输入要排查的x坐标值:");
		scanf("%d",&x);
		printf("请输入要排查的y坐标值:");
		scanf("%d", &y);
		if (x > 0 && x <= row && y > 0 && y <= col)
		{
			if (show[x][y] == '*')
			{
				if (flag == 0 && mine[x][y] == '1')  //保证第一次排雷不会被雷炸
				{
					flag = 1;
					SetMine(mine, ROW, COL, 1);
					mine[x][y] = '0';
				}
				else if (flag != 0 && mine[x][y] == '1')
				{
					printf("你被雷炸了,游戏失败!\n");
					printf("雷阵分布图如下:\n");
					PrintBoard(mine, ROW, COL);
					break;
				}
				else
				{
					flag = 1;
				}
				Expand(mine, show, x, y);  //展开
				system("cls");  //清屏
				PrintBoard(show, ROW, COL);
			}
			else
			{
				printf("该坐标处已排查,请重新输入...\n");
			}
		}
		else
		{
			printf("坐标值超范围,请重新输入...\n");
		}
		for (i = 1; i <= row; i++)
		{
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] != '*')
				{
					sum++;
				}
			}
		}
		if (sum == row*col - count)
		{
			printf("恭喜扫雷成功!\n");
			break;
		}
	}
}
  • test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void Menu1()
{
	printf("********扫雷游戏********\n");
	printf("**                    **\n");
	printf("**       1.Play       **\n");
	printf("**       0.Exit       **\n");
	printf("**                    **\n");
	printf("************************\n");
}

void Menu2()
{
	printf("********游戏难度********\n");
	printf("**                    **\n");
	printf("**   1.Easy (%d个雷)  **\n",Easy);
	printf("**   2.Hard (%d个雷)  **\n",Hard);
	printf("**                    **\n");
	printf("************************\n");
}

void Game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	int choice = 0;
	int count = 0;
	srand((unsigned int)time(NULL));
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	Menu2();
	printf("请选择:");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1: 
		{
			count = Easy;
			PrintBoard(show, ROW, COL);
			SetMine(mine, ROW, COL, count);
			ClearMine(mine, show, ROW, COL, count);
		}
		break;
	case 2:
	    {
		    count = Hard;
	        PrintBoard(show, ROW, COL);
			SetMine(mine, ROW, COL, count);
			ClearMine(mine, show, ROW, COL, count);
	    }
		break;
	default: printf("选择有误,请重新开始...\n"); break;
	}
}

int main()
{
	int input = 0;
	do
	{
		Menu1();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1: Game(); break;
		case 0: break;
		default: printf("选择有误,请重新选择...\n"); break;
		}
	} while (input);
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值