扫雷游戏*

游戏规则: 通过菜单实现继续玩或者退出游戏,输入要排查处的坐标:
  • 如果位置不是雷,就显⽰周围一圈有⼏个雷
  •  如果位置是雷,就炸死游戏结束
  •  把所有⾮雷都找出来,排雷成功,游戏结束

一.游戏代码详解

  • 菜单

用分支结构,选1玩游戏,选2游戏结束。

void menu()
{
	printf("**************\n");
	printf("****1.play****\n");
	printf("****0.exit****\n");
}
  • 设置数组

一个用于埋雷,一个用于记录周围雷的个数

char mine[ROWS][COLS];

char show[ROWS][COLS];

设置行列数和雷数,可以调节排雷的规格和难度

注:为计算周围雷数时不超过行列的界限,设置的数组行列数比实际游戏中展示的始末各多1(2、3行为游戏中雷阵行列数,4、5行为数组行列数)

1 #define EASY_COUNT 10

2 #define ROW  9
3 #define COL 9

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

为计算周围雷个数时方便,雷记为1,非雷记为0;

先将埋雷数组元素全部设为'0',计数数组元素全设为'*'

1 InitBoard(mine, ROWS, COLS, '0');
2 InitBoard(show, ROWS, COLS, '*');

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
  • 检查数组

运用展示数组的函数(只需展示游戏中响应规格的雷阵,ROW * COL),在第一行和每行起始标上坐标(从0到10),便于玩家排雷

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf(" ****扫雷游戏****\n");
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for(i = 1;i <= row;i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

  • 埋雷

用while语句埋下指定个数的雷,m表示已埋雷个数,只有成功埋雷时加1,if语句排除已埋雷的地方

注:埋雷时要得到任意坐标,需用rand()函数,包含头文件#include <stdlib.h>,随机种子用到time函数(time(NULL)返回时间戳,时间改变种子改变),需包含头文件time.h.

void SetMine(char board[ROWS][COLS], int row, int col)
{
	int m = 0;
	while(m < EASY_COUNT)
	{
		int i = 0;
		int j = 0;
		i = rand() % (ROW - 1) + 1;
		j = rand() % (COL - 1) + 1;
		if (board[i][j] != '1')
		{
			board[i][j] = '1';
			m++;
		}
	}
}
  • 计算周围雷个数的函数

int GetMineCount(char board[ROWS][COLS], int x,int y)
{
	return (board[x][y + 1] + board[x][y - 1] + board[x - 1][y] + board[x - 1][y - 1] +         board[x - 1][y + 1] + board[x + 1][y] + board[x + 1][y + 1] + board[x + 1][y - 1] - 8 *     '0');
    
}
  • 游戏主代码

先向玩家展示雷阵(隐藏周围雷数的数组):

1 DisplayBoard(show, ROW, COL);

然后用while循环让玩家输入要排查的坐标(需判断玩家输入坐标是否合法),其中用if分支,根据玩家所选坐标是否有雷执行语句,若排查处有雷,则失败,用break跳出循环;若无雷,调用GetMineCount函数,展示该处周围雷数。win表示已排除非雷坐标的个数,所选位置非雷才加1,直至排除所有非雷坐标跳出循环。最后把显示所有雷的雷阵展示出来。

void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    int win = 0;
    while (win < row * col - EASY_COUNT)
    {
        printf("请输入要排查的坐标:");
        scanf("%d %d", &i, &j);
        if (i >= 1 && i <= row && j >= 1 && j <= col)
        {
            if (board1[i][j] == '1')
            {
                printf("很遗憾,你被炸死了");
                break;
            }
            else
            {
                board2[i][j] = GetMineCount(board1, i, j) + '0';
                DisplayBoard(board2, ROW, COL);
                win++;
            }
        }
        else
        {
            printf("输入的坐标不正确,请重新输入");
        }
    }
    if(win == row * col - EASY_COUNT)
    { 
        printf("恭喜你,成功啦!");
    }
    DisplayBoard(board1, ROW, COL);
}








三.扫雷游戏代码

函数代码较多,创建一个头文件(game.h)和一个专门写函数代码的源文件(game.c),saolei.c要包含头文件,并调用函数。

注:game.h需用预处理指令#pragma once,保证头文件只被编译一次

game.h

#define _CRT_SECURE_NO_WARNINGS 1

#pragma once

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

#define EASY_COUNT 10

#define ROW  9
#define COL 9

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

void InitBoard(char boar[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
int GetMineCount(char board[ROWS][COLS], int row, int col);

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;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf(" ****扫雷游戏****\n");
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for(i = 1;i <= row;i++)
	{
		printf("%d ", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
	int m = 0;
	while(m < EASY_COUNT)
	{
		int i = 0;
		int j = 0;
		i = rand() % (ROW - 1) + 1;
		j = rand() % (COL - 1) + 1;
		if (board[i][j] != '1')
		{
			board[i][j] = '1';
			m++;
		}
	}
}

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

void FindMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col)
{
	int i = 0;
    int j = 0;
	int win = 0;
	while (win < row * col - EASY_COUNT)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &i, &j);
		if (i >= 1 && i <= row && j >= 1 && j <= col)
		{
			if (board1[i][j] == '1')
			{
				printf("很遗憾,你被炸死了");
				break;
			}
			else
			{
				board2[i][j] = GetMineCount(board1, i, j) + '0';
				DisplayBoard(board2, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入的坐标不正确,请重新输入");
		}
	}
	if(win == row * col - EASY_COUNT)
	{ 
		printf("恭喜你,成功啦!");
	}
	DisplayBoard(board1, ROW, COL);
}



saolei.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

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

void game()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//DisplayBoard(show, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	SetMine(mine,ROW,COL);
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	FindMine(mine, show, ROW, COL);
}

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

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值