C语言—扫雷

什么是扫雷

在这里插入图片描述

《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。

扫雷的起源(以及扫雷的玩法)

扫雷最原始的版本可以追溯到1973年一款名为“方块”的游戏。
不久,“方块”被改写成了游戏“Rlogic”。在“Rlogic”里,玩家的任务是作为美国海军陆战队队员,为指挥中心探出一条没有地雷的安全路线,如果路全被地雷堵死就算输。两年后,汤姆·安德森在“Rlogic”的基础上又编写出了游戏“地雷”,由此奠定了现代扫雷游戏的雏形。
1981年,微软公司的罗伯特·杜尔和卡特·约翰逊两位工程师在Windows3.1系统上加载了该游戏,扫雷游戏才正式在全世界推广开来。
这款游戏的玩法是在一个99(初级),1616(中级),16*30(高级),或自定义大小的方块矩阵中随机布置一定量的地雷(初级为10个,中级为40个,高级为99个)。由玩家逐个翻开方块,以找出所有地雷为最终游戏目标。如果玩家翻开的方块有地雷,则游戏结束。

设计扫雷游戏的思路

在这里插入图片描述

游戏的实现

1.首先要打印一个菜单

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

2.创建扫雷游戏

  • 首先创建一个11*11的格子(用到二维数组)
  • 创建布置好的雷的信息
  • 创建排查出的雷的信息
	char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
  • 初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char sz)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < rows;i++)
	{
		for (j = 0;j < cols;j++)
		{
			board[i][j] = sz;
		}
	}
}
  • 打印棋盘
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 x = 0;
	int y = 0;
	int count= EASY_COUNT;
	
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
	
}
  • 排查雷的信息,统计x,y坐标周围有几个雷
static int get_mine_count(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';
}
  • 检查雷
    (1)是雷——很遗憾你被炸死了——游戏结束
    (2)不是雷——统计坐标周围有几个雷——存储排查雷的信息到show数组——游戏继续
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<COL * ROW - 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
			{
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, row, col);
				win++;
			}


		}
		else
		{
			printf("坐标不合法,请重新输入\n");
		}

	}
	if (win == COL * ROW - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, row, col);
	}
}

3.游戏可以玩多次

int main()
{
	
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//game();
			printf("游戏开始\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;

		}
	} while (input);
	return 0;
}

游戏的源码

一、game.c

#include"game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char sz)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < rows;i++)
	{
		for (j = 0;j < cols;j++)
		{
			board[i][j] = sz;
		}
	}
}

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 x = 0;
	int y = 0;
	int count= EASY_COUNT;
	
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
	
}

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


也可以这样写
//for(i=-1;i<=1;i++)
//{
//	for(j=-1;j<=1;j++)
//	{
//		if(mine[x+i][y+j]!='0')
//			count++;
//	}
//}

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<COL * ROW - 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
			{
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, row, col);
				win++;
			}


		}
		else
		{
			printf("坐标不合法,请重新输入\n");
		}

	}
	if (win == COL * ROW - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, row, col);
	}
}

二、game.h

#include<stdlib.h>
#include<time.h>
#include<stdio.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 sz);
//打印棋盘
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

#include"game.h"
void menu()
{
	printf("******************************\n");
	printf("******    1. play      *******\n");
	printf("******    0. exit      *******\n");
	printf("******************************\n");
}
void game()
{
	char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
	//初始化棋盘
	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);
	FindMine(mine, show, ROW, COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game(); //扫雷游戏
			//printf("游戏开始\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;

		}
	} while (input);
	return 0;
}

代码运行结果

在这里插入图片描述

进阶版

优化的问题

1.如果坐标不是雷且周围也没有雷可以展开一片
2.如果确定那个地方是雷可以做标记

源码

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char sz)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < rows;i++)
	{
		for (j = 0;j < cols;j++)
		{
			board[i][j] = sz;
		}
	}
}

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 x = 0;
	int y = 0;
	int count= EASY_COUNT;
	while (count)
	{
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
	
}
static void sign()
{
	printf("******************************\n");
	printf("******    1. mark      *******\n");
	printf("******    0. exit      *******\n");
	printf("******************************\n");
}
static int get_mine_count(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';
}
 static void lp(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
	int count = get_mine_count(mine, x, y);
	if (count == 0)
	{
		show[x][y] = ' ';
		if (x - 1 >= 0 && x-1 <= ROW && y >= 0 && y <= COL && show[x - 1][y] == '*')
		{
			lp(mine, show, x - 1, y);
		}
		if (x + 1 >= 0 && x+1 <= ROW && y >= 0 && y <= COL && show[x + 1][y] == '*')
		{
			lp(mine, show, x + 1, y);
		}
		if (x-1 >= 0 && x-1 <= ROW && y - 1 >= 0 && y-1 <= COL && show[x-1][y - 1] == '*')
		{
			lp(mine, show, x - 1, y-1);
		}
		if (x-1 >= 0 && x-1 <= ROW && y + 1 >= 0 && y+1 <= COL && show[x-1][y + 1] == '*')
		{
			lp(mine, show, x - 1, y+1);
		}
		if (x+1 >= 0 && x+1 <= ROW && y + 1 >= 0 && y+1 <= COL && show[x+1][y + 1] == '*')
		{
			lp(mine, show, x + 1, y+1);
		}
		if (x+1 >= 0 && x+1 <= ROW && y -1  >= 0 && y-1 <= COL && show[x+1][y -1] == '*')
		{
			lp(mine, show, x + 1, y-1);
		}
		if (x  >= 0 && x  <= ROW && y - 1 >= 0 && y - 1 <= COL && show[x ][y - 1] == '*')
		{
			lp(mine, show, x , y-1);
		}
		if (x  >= 0 && x  <= ROW && y + 1 >= 0 && y + 1 <= COL && show[x ][y + 1] == '*')
		{
			lp(mine, show, x , y+1);
		}
	}
	else
	{
		show[x][y] = count + '0';
	}
}
static int win(char show[ROWS][COLS], int row, int col)
 {
	 int i = 0;
	 int j = 0;
	 int gs = 0;
	 for (i = 1; i <= row; i++)
	 {
		 for (j = 1; j <= col; j++)
		 {
			 if (show[i][j] != '*'&& show[i][j] != '@')
			 {
				 gs++;
			 }
		 }
	 }
	 return gs;
 }



void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int input = 0;
	int x = 0;
	int y = 0;
	int w = 0;
	while (1)
	{
		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
			{
				lp(mine, show, x, y);
				int Is_win(char show[ROWS][COLS], int row, int col);
				int w=win(show, row, col);
				if (w == COL * ROW - EASY_COUNT)
				{
					printf("恭喜你,排雷成功\n");
					DisplayBoard(mine, row, col);
					break;
				}
				DisplayBoard(show, row, col);
				
				do
				{				
					sign();
					printf("请选择:>");
					scanf("%d", &input);
					switch (input)
					{
					case 1:
						printf("请输入要标记的坐标:>");
						scanf("%d %d", &x, &y);
						if (show[x][y] == '*')
						{
							show[x][y] = '@';
							DisplayBoard(show, row, col);
						}
						else
						{
							printf("输入错误,输入选择\n");
						}
						break;
					case 0:
						printf("退出标记\n");
						break;
					default:
						printf("选择错误,重新选择\n");
						break;

					}
				} while (input);
			}
		}
		else
		{
			printf("坐标不合法,请重新输入\n");
		}


	}
	
}

game.h

#pragma once
#include<stdlib.h>
#include<time.h>
#include<stdio.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 sz);
//打印棋盘
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"game.h"
void menu()
{
	printf("******************************\n");
	printf("******    1. play      *******\n");
	printf("******    0. exit      *******\n");
	printf("******************************\n");
}
void game()
{
	char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
	//初始化棋盘
	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);
	//排查雷
	FindMine(mine, show, ROW, COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game(); //扫雷游戏
			//printf("游戏开始\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;

		}
	} while (input);
	return 0;
}

运行结果

在这里插入图片描述

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值