C语言实现扫雷游戏要求第一次不踩雷,能展开一片雷区(望各位大佬斧正)

对于实现扫雷游戏思路如下:

  1. 首先布置一定大小的扫雷区如9*9
  2. 随机(rand())布置一定数量的雷
  3. 实现扫雷(其中要求第一次踩不到雷,能展开一片无雷区)

其具体操作如下:
建立game.h, game.c, test.c文件以便整理
在test.c文件中:

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
	printf("*********************\n");
	printf("*****  1.play  ******\n");
	printf("*****  0.exit  ******\n");
	printf("*********************\n");	
}
void game()
{
	//设置雷区
	char my[ROWS][COLS] = {0};
	char show[ROWS][COLS] = {0};
	Initboard(my, ROWS, COLS, '0');
	Initboard(show, ROWS, COLS, '*');
	Displayboard(show, ROW, COL);
	//布雷
	Set_my(my, ROW, COL);
	Displayboard(my, ROW, COL);
	//排雷
	FindMy(my, 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;
}

在game.h中定义所需函数

#ifndef __GAME_H__
#define __GAME_H__

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

#define EASY_COUNT 6//雷的个数创建为6
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2

void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
void Displayboard(char board[ROWS][COLS], int row, int col);
void Set_my(char board[ROWS][COLS], int row, int col);
void FindMy(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMyCount(char my[ROWS][COLS], int x, int y);
void Open_up( char my[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
int Is_Win(char show[ROWS][COLS], int row, int col);

#endif __GAME_H__

接下来就是重点了在game.c中:

  1. 先设置好雷区
#include "game.h"

void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
	memset(&board[0][0], set, rows*cols*sizeof(board[0][0]));

}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("--------------------\n");
	for(i=0; i<=col; 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");
	}
	printf("--------------------\n");
}
  1. 随机布好雷,雷最好用“1”表示(能更方便计算周围有多少颗雷)
void Set_my(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while(count)
	{
		int x = rand()%row+1;
		int y = rand()%col+1;
		if(board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
  1. 接下来就要排雷了(第一次踩不到雷,周围无雷时展开一片)
void FindMy(char my[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int k = 0;
	int l = 0;
	while(Is_Win(show, row, col)>EASY_COUNT && Is_Win(show, row, col)<=row*col)
	{

		printf("请输入要排查的坐标:>");
		scanf("%d%d", &x, &y);
		if(x>=1 && x<=row && y>=1 && y<=col)
		{
			if(Is_Win(show, row, col) == row*col)//保证第一次不被雷炸死
			{
				if(my[x][y] == '1')
				{
					my[x][y] = '0';
					while(1)
					{
						k = rand()%row + 1;
						l = rand()%col + 1;
						if(my[k][l] == '0')
						{
							my[k][l] = '1';
							break;
						}
					}
				}
			}
			if(my[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				Displayboard(my, row, col);
				break;
			}
			else
			{
				int count = GetMyCount(my, x, y);
				show[x][y] = count + '0';
				Open_up(my, show, row, col, x, y);
				Displayboard(my, row, col);
				Displayboard(show, row, col);
			}
		}
		else
		{
			printf("坐标非法, 请重新输入!\n");
		}
	}
	if(Is_Win(show, row, col)==EASY_COUNT)//判断排雷成功
	{
		printf("排雷成功\n");
		Displayboard(my, row, col);
	}
}

int Is_Win(char show[ROWS][COLS], int row, int col)//判断雷什么时候排完
{
	int count = 0;
	int i = 0;
	int j = 0;
	for(i=1; i<=row; i++)
	{
		for(j=1; j<=col; j++)
		{
			if(show[i][j] == '*')
			{
				count++;
			}
		}
	}
	return count;
}

int GetMyCount(char my[ROWS][COLS], int x, int y)
{
	return my[x-1][y]+my[x-1][y-1]+
		   my[x][y-1]+my[x+1][y-1]+
		   my[x+1][y]+my[x+1][y+1]+
		   my[x][y+1]+my[x-1][y+1]-8*'0';//得到雷的个数
}
void Open_up( char my[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)//周围无雷时展开一片
{
    int ret = 0;
    ret = GetMyCount(my, x, y);
    if(ret == 0)
    {
        show[x][y] = ' ';
        if(x - 1>0 && y>0 && show[x - 1][y] == '*')
            Open_up(my, show, row, col, x - 1, y);
        if(x - 1>0 && y + 1 <= col && show[x - 1][y + 1] == '*')
            Open_up(my, show, row, col, x - 1, y + 1);
        if(x>0 && y + 1 <= col && show[x][y + 1] == '*')
            Open_up(my, show, row, col, x, y + 1);
        if(x + 1 <= row && y + 1 <= col && show[x + 1][y + 1] == '*')
            Open_up(my, show, row, col, x + 1, y + 1);
        if(x + 1 <= row && y>0 && show[x + 1][y] == '*')
            Open_up(my, show, row, col, x + 1, y);
        if(x + 1 <= row && y - 1>0 && show[x + 1][y - 1] == '*')
            Open_up(my, show, row, col, x + 1, y - 1);
        if(x>0 && y - 1>0 && show[x][y - 1] == '*')
            Open_up(my, show, row, col, x, y - 1);
        if(x - 1>0 && y - 1>0 && show[x - 1][y - 1] == '*')
            Open_up(my, show, row, col, x - 1, y - 1);
    }
    else
    {
        show[x][y] = GetMyCount(my, x, y) + '0';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值