C语言实现扫雷游戏

设计思路

需要用到三个文件:

  • test.c       游戏的逻辑运行
  • game.h    游戏相关函数声明
  • game.c    游戏相关函数实现

布局方式

以9×9棋盘,10个雷为例:

第一种方式:将有雷的位置用1表示,无雷的位置用0表示 (图一)。

                               图一                                                                                 图二

这种方法的问题是,统计一个位置周围的雷,这个位置本身不是雷,但如果它周围有一个雷,那么应当显示出1,那么这个“1”有歧义。

第二种方法式:将有雷的位置用*表示,无雷的位置用#表示(图三)。

                           图三                                                                                  图四

这种方法的问题是,点开非雷的位置后,需要用数字显示雷的个数(图四),元素过多(有* # 数字),需要判断的次数也就很多,所以也不太适合。

正确的布局方式:

需要用到两个棋盘(两个数组)。第一个放雷的信息,有雷的位置用1表示,无雷的位置用0表示(图五)。第二个用来显示扫雷后,其周围雷的个数(图六)。

图五                                                                             图六     

但是此时无法统计靠边的位置周围雷的个数,如八行九列的位置,所以我们需要每一边都多加一行或一列,变成了十一行,十一列的图形(图六),多加的行和列都不要放雷,防止越界,那么9×9棋盘的边上和角落周围雷的个数也可以统计,如图上黄色框住的位置。

第二个用来放排查出雷的信息的数组,我们首先都用*号挡住,来保持神秘感,排查出哪个雷,再显示出来。

编写程序

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

game.c

#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
		for (int j = 0; j < cols; j++)
			board[i][j] = set;
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	for (int i = 0; i <= row; i++) {
		printf("%d  ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++) {
		printf("%d  ", i);
		for (int 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;
		int y = rand() % col + 1;
		if (board[x][y] != '1') {
			board[x][y] = '1';
			count--;
		}
	}
}

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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	int win = 0;
	while (win < row * col - EASY_COUNT)
	{
		printf("请输入坐标:>");
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row && y > 0 && y <= col)
		{
			if (mine[x][y] == '1') {
				printf("很遗憾,扫雷失败!");
				Sleep(1000);
				system("cls");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else if(show[x][y] != '*') {
				printf("重复输入!");
			}
			else
			{
				//这个位置不是雷,且之前未被扫过
				int count = GetMineCount(mine,x, y);
				show[x][y] = count + '0';
				system("cls");
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
			printf("坐标非法,请重新输入!");
	}
	if (win == row * col - EASY_COUNT) {
		system("cls");
		printf("恭喜你,扫雷成功!\n");
		DisplayBoard(mine, ROW, COL);
	}
}

test.c

#include "game.h"
void menu()
{
	printf("----------扫雷游戏----------\n");
	printf("--------1.开始游戏----------\n");
	printf("--------0.退出游戏----------\n");
	printf("----------------------------\n");
}

void game()
{
	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	//棋盘初始化
	// mine数组初始化为‘0’,show数组初始化为‘*’
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	
	//布置雷
	SetMine(mine, ROW, COL);
	//打印棋盘
	//DisplayBoard(mine, ROW, COL);
	//printf("\n");
	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);
		system("cls");
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入!\n");
		}
	} while (input);
	return 0;
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值