C语言扫雷简化版(b站鹏哥)


前言

扫雷是大部分C语言学习者初期所遇到的步骤比较繁琐的题目,其代码量较大,大致可以分为三部分。该篇文章较适合于初级程序猿阅览。

系列文章目录

  • 扫雷基本介绍
  • 游戏的头文件(game.h)
  • 游戏界面
  • 初始化雷盘(InitBoard)
  • 展示雷盘(DisplayBoard)
  • 布置雷(SetMine)
  • 排查雷(GetMineCount
  • 寻找雷(FindMine)
  • 代码如下[(game.h),(test.c),(game.c)]
  • 操作效果图

扫雷基本介绍

扫雷是大多数计算机自带的一款游戏,开始游戏时点击如果遇到雷则会直接游戏结束,若不是雷则会点开这片区域,并在点击处显示其周边雷数。如果周围没有雷就会展开(c语言实现该步骤需要用到递归,目前鄙人能力有限只能写出简化版的,即不能因为周边没雷而展开区域的雷盘)。

游戏的头文件(game.h)

ROWS和COLS是为了避免当雷被随机分布到9x9雷盘时无法进行GetMineCount而布置的。

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10

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);
int GetMineCount(char mine[ROWS][COLS], int x, int y);

游戏界面

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

当player来到界面时应输入相关数字:1.玩游戏;0.退出游戏   若输入其他则判断为输入错误。

初始化雷盘(InitBoard)

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

先将数组初始化,以便雷的随即排放。

展示雷盘(DisplayBoard)

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

利用嵌套循环实现将雷盘展示在player眼中,同时将row和col数打印出来以方便player选择坐标。for (i = 0; i < +col; i++)中i从0开始是因为在行列间有一个位置空了,需要用0来填补。若从1开始的话则无法把行列数明确地表示出来。

布置雷(SetMine)

void SetMine(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] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

依靠rand()布置雷,点击某坐标,如果在该坐标上有雷则重新选择坐标,每布置一个雷count就要自减1。其中rand() % row(col)能产生0~9的数,令其加1则可以产生1~10的数。

排查雷(GetMineCount

int GetMineCount(char mine[ROWS][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][y + 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] - 8 * '0');
}

排除二维数组mine[x][y]周围雷的数目并返回。(即周围的八个坐标,‘0’在Ascll码值中为0)

x-1,y+1 x,y+1 x+1,y+1
x-1,y

x,y

x+1,y
x-1,y-1 x,y-1 x+1,y-1

寻找雷(FindMine)

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int sum = row * col - EASY_COUNT;
	int x = 0;
	int y = 0;
	while (win < sum)
	{
		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 
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入的坐标错误n");
		}
	}
	if (win == sum)
	{
		printf("牛啊牛啊,你已经排雷成功啦!n");
			DisplayBoard(show, ROW, COL);
	}
}

当player点击的坐标有雷则游戏结束并展示雷,无雷则返回该坐标的雷数并count++,当count与sum相等时player赢下游戏。在游戏过程中player输入的坐标应在有效范围以内,否则提示输入错误。

代码如下[(game.h),(test.c),(game.c)]

game.h部分

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10

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);
int GetMineCount(char mine[ROWS][COLS], int x, int y);

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 mine[ROWS][COLS];
	char show[ROWS][COLS];
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	SetMine(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 1:
			game();
			break;
		case 0:
			printf("退出游戏n");
			break;
		default:
			printf("选择错误n");
			break;
		}
	} while (input != 0);
	return 0;
}

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


int GetMineCount(char mine[ROWS][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][y + 1] +
		mine[x + 1][y - 1] +
		mine[x + 1][y] +
		mine[x + 1][y + 1] - 8 * '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");
}


void SetMine(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] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}
}


void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int sum = row * col - EASY_COUNT;
	int x = 0;
	int y = 0;
	while (win < sum)
	{
		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 
			{
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入的坐标错误n");
		}
	}
	if (win == sum)
	{
		printf("牛啊牛啊,你已经排雷成功啦!n");
			DisplayBoard(show, ROW, COL);
	}
}

操作效果图

 

 

 

 

感谢能阅读到最后的你!以上内容都是用心敲出来的,打字不容易,希望能得到你的支持。好好学习,天天编程。未来是敲出来的,加油吧,一起奔向更远的远方~

总结

刚入门的程序员可能会犯的错误:

1.在头文件中写出#define 9,加入会导致代码无法运行; 

2.GetMineCount是有返回值的;    

3.SetMine FineMine GetMineCount 是在9x9中运行的,不应用rows,cols;

4.在分步写代码时未在源文件部分引用“game.h”;

5.%写成/,==写成=。

                                                                                                                                                                      

                                                                                                                                                                                                                             

     

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>