扫雷游戏的代码实现

扫雷游戏

game.h

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

#define DIF_ROW 21
#define DIF_COL 21
#define DIF_COUNT 40

#define DIF_ROWS DIF_ROW+2
#define DIF_COLS DIF_COL+2

#define EASY_COUNT 10

//初始化棋盘
extern void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols, char ch);
//打印棋盘
extern void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col);
//存放雷
extern void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt);
//排查雷
extern void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt, int visted[DIF_ROWS][DIF_COLS]);
extern int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y);
extern void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS]);

game.c

#include "game.h"

void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols, char ch)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ch;
		}
	}
}

void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col)
{
	int i = 0;
	printf("   ");
	for (i = 1; i <= row; i++)
	{
		printf("%2d ", i);
	}
	printf("\n");
	printf("  |");
	for (i = 1; i <= row; i++)
	{
		printf("---");
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%2d|", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt)
{
	while (cnt > 0)
	{
		int x = 0;
		int y = 0;
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			cnt--;
		}
	}
}

int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y)
{
	int cnt = 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';
	return cnt;

}

void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS])
{
	//visted[x][y] == 1;
	int vist[DIF_ROWS][DIF_COLS] = { 0 };
	int cnt = MineCount(mine, x, y);
	if (cnt > 0)
		show[x][y] = cnt + '0';
	else
	{
		show[x][y] = ' ';
		if (visted[x - 1][y - 1] == 0)
		{
			vist[x - 1][y - 1] = 1;
			visted[x - 1][y - 1] = 1;
		}
		if (visted[x - 1][y] == 0)
		{
			vist[x - 1][y] = 1;
			visted[x - 1][y] = 1;
		}
		if (visted[x - 1][y + 1] == 0)
		{
			vist[x - 1][y + 1] = 1;
			visted[x - 1][y + 1] = 1;
		}
		if (visted[x][y - 1] == 0)
		{
			vist[x][y - 1] = 1;
			visted[x][y - 1] = 1;
		}
		if (visted[x][y + 1] == 0)
		{
			vist[x][y + 1] = 1;
			visted[x][y + 1] = 1;
		}
		if (visted[x + 1][y - 1] == 0)
		{
			vist[x + 1][y - 1] = 1;
			visted[x + 1][y - 1] = 1;
		}
		if (visted[x + 1][y] == 0)
		{
			vist[x + 1][y] = 1;
			visted[x + 1][y] = 1;
		}
		if (visted[x + 1][y + 1] == 0)
		{
			vist[x + 1][y + 1] = 1;
			visted[x + 1][y + 1] = 1;
		}


		if (vist[x - 1][y - 1] == 1)
		{
			Expend(mine, show, x - 1, y - 1, visted);
		}
		if (vist[x - 1][y] == 1)
		{
			Expend(mine, show, x - 1, y, visted);
		}
		if (vist[x - 1][y + 1] == 1)
		{
			Expend(mine, show, x - 1, y + 1, visted);
		}
		if (vist[x][y - 1] == 1)
		{
			Expend(mine, show, x, y - 1, visted);
		}
		if (vist[x][y + 1] == 1)
		{
			Expend(mine, show, x, y + 1, visted);
		}
		if (vist[x + 1][y - 1] == 1)
		{
			Expend(mine, show, x + 1, y - 1, visted);
		}
		if (vist[x + 1][y] == 1)
		{
			Expend(mine, show, x + 1, y, visted);
		}
		if (vist[x + 1][y + 1] == 1)
		{
			Expend(mine, show, x + 1, y + 1, visted);
		}
	}
}

void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt, int visted[DIF_ROWS][DIF_COLS])
{
	while (1)
	{
		int x = 0;
		int y = 0;
		printf("请输入:>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (1 == visted[x][y])
			{
				printf("该位子已被占用\n");
			}
			else if (mine[x][y] == '1')
			{
				show[x][y] = '$';
				DisPlayBoard(show, row, col);
				printf("你踩到雷了\n");
				break;
			}
			else
			{
				visted[x][y] == 1;
				Expend(mine, show, x, y, visted);
				DisPlayBoard(show, row, col);
			}
		}
		else
		{
			printf("超出范围\n");
		}
		int count = 0;
		int i = 0;
		for (i = 1; i <= row; i++)
		{
			int j = 0;
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] == '*')
				{
					count++;
				}
			}
		}
		if (count == cnt)
		{
			printf("恭喜你,你赢了\n");
			break;
		}
	}
}

text.c

#include "game.h"

int ROW = 0;
int COL = 0;
int ROWS = 0;
int COLS = 0;
int count = 0;


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


void game()
{
	char mine[DIF_ROWS][DIF_COLS];
	char show[DIF_ROWS][DIF_COLS];
	int visted[DIF_ROWS][DIF_COLS];
	int i = 0;
	for (i = 0; i < ROWS; i++)
	{
		int j = 0;
		for (j = 0; j < COLS; j++)
		{
			if (i == 0)
				visted[i][j] = 1;
			else if (j == 0)
				visted[i][j] = 1;
			else if (i == ROWS - 1)
				visted[i][j] = 1;
			else if (j == COLS - 1)
				visted[i][j] = 1;
			else
				visted[i][j] = 0;
		}
	}
	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	DisPlayBoard(show, ROW, COL);
	//存放雷
	SetMine(mine, ROW, COL, count);
	//排查雷
	FindMine(mine, show, ROW, COL, count, visted);
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			ROW = DIF_ROW;
			COL = DIF_COL;
			ROWS = DIF_ROWS;
			COLS = DIF_COLS;
			count = DIF_COUNT;
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);


	return 0;
}

扫雷游戏的实现:
在这里插入图片描述

文章代码参考

http://t.csdn.cn/UVGu7

  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十月三十二号的风情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值