我是怎样一步步完成扫雷游戏的

一  了解游戏规则

我没有玩过扫雷游戏,要想完成扫雷的代码,得先知道游戏规则。

  • 1 游戏规则很简单,点击方格 ,如果是地雷,游戏失败,找到所 有地雷游戏胜利
  • 2 .刚开始需要碰运气,只要点开一个区域,就可以正式开始了。
  • 3 根据现有情况,判断出一定有雷的位置。
  • 该怎样判断呢 ?
  •   当点开的不是雷的时候,格子上会显示数字,数字表示该格子周围雷的个数。
  • 直到点开全部不是雷的格子才能胜利。

二 构思

1.首先将棋盘想成一个二维数组,该数组包含有地雷,数字,在扫雷过程中,想要排雷 就要知道该格子里面包含的多种情况,需要一步步判断,比较麻烦,所以我想到可以再定义一个二维字符数组。因此定义mine数组存放雷的信息,show数组来存放排雷的信息。

2.显示棋盘。定义好了两个数组后,show棋盘就要显示出来(第一个图),但是要扫雷的时候,是要输入坐标来扫雷,因此,show棋盘应该是这样(第二个图)

3.放雷(布置雷)。


头文件

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

rand 是用来随机生成雷的坐标的

	int x = rand() % row + 1;//0~8+1-->1~9
	int y = rand() % col + 1;

4.扫雷。扫雷的过程关键在于没有雷的时候要显示周围雷的个数。

最关键在于在棋盘中用字符 '1' 来表示雷,字符 ‘0’ 来表示没有雷。

如此以后,计算 空格(没有雷)周围雷个数的时候就很简单:将雷周围格子上的字符数字相加起来减去8* ‘0’。

//周围雷个数
int SetMineCount(char board[ROWS][COLS], int x, int y)
{
	return 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] + board[x - 1][y] - 8 * '0';
}

  在排雷过程中需要注意几个特别的情况,

一   是玩家输入的坐标可能超过范围,要写出这种情况

二  是玩家可能重复输入坐标,也要排除这种情况。

三  是玩家需要排出 ROW * COL - EASY_COUNT (格子数-雷的个数)个数目的空格才能说明玩家排雷成功。

注意:我要用的棋盘是 9 x 9  的,但是我们在创建棋盘是应该是 11 x 11的,因为处于边缘的格子中放有雷的话,在计算周围雷的个数是会跨界。但是在放雷,排查雷的时候是用的 9 x 9 的棋盘。

                                                                

扫雷游戏不仅仅只有 9 x 9 的,还有更大更难模式,为了改动方便,这时应该使用 define ,

#define ROW 9
#define COL 9

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

三  全部代码   

//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 <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		int j = 0;
		//打印行号
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;//0~8+1-->1~9
		int y = rand() % col + 1;

		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

int GetMineCount(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';
}

//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - EASY_COUNT)
	{
		printf("请输入要排查的坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,被炸死了\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				if (show[x][y] != '*')
				{
					printf("该坐标已经被排查过了,无需再排查\n");
				}
				else
				{
					//统计mine数组的x,y坐标周围8个坐标中有几个雷
					int count = GetMineCount(mine, x, y);
					show[x][y] = count + '0';
					DisplayBoard(show, ROW, COL);
					win++;
				}
			}
		}
		else
		{
			printf("输入的坐标非法,请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

     

// game.h

#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 board[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);

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

           

//test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include"game.h"

void game()
{
	char mine[ROWS][COLS] = { 0 };  //存放雷的信息
	char show[ROWS][COLS] = { 0 };  //存放排查雷的信息
	//初始化棋盘
	InitBoard(mine,ROWS,COLS, '0');
	InitBoard(show,ROWS,COLS, '*');

	//棋盘的显示
	// DisplayBoard(mine, ROW, COL);
	//DisplayBoard(show, ROW, COL);
	

	//布置雷
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);

	//排查雷
	FindMine(mine,show, ROW, COL);

}

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

}


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;
}

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值