【C语言】扫雷游戏


一、新建项

test.c - 测试游戏
game.h - 游戏函数的声明
game.c - 游戏的实现


二、主函数 do while

定义input变量,存放用户输入的数字。
如果用户输入1,开始游戏进入game函数;
如果用户输入0,显示”退出游戏”break;
如果用户输入的是错误的数字,提示“选择错误,重新选择!”。

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>\n");
		scanf("%d", &input);

		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	} while (input);

	return 0;
}

三、打印菜单

游戏开始前打印菜单,
提示用户输入数字1开始游戏
或0退出游戏

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

四、game函数

1、创建数组

  1. 创建两个数组:
    mine布置好的雷的信息
    show存放排查出的雷的信息
	char mine[ROWS][COLS] = { 0 }; // 布置好的雷的信息
	char show[ROWS][COLS] = { 0 }; // 存放排查出的雷的信息

2、定义行列

头文件内定义行列
#define ROW 9
#define COL 9

注:

  1. 我们需要99的棋盘,但后续排查雷的时候,需要排查周围一圈,所以这里再创建1111的棋盘,用ROW加2
  2. 不要忘了在test.c里引game.h
// game.h
#define ROW 9
#define COL 9

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

3、初始化棋盘

// test.c
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');

// game.h
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

// game.c
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;
		}
	}
}

4、打印棋盘

  1. 用DisplayBoard函数实现
  2. 打印放好雷的棋盘 只需里面的9*9
  3. 头文件里传的还是1111 但操作的是99
// test.c
	DisplayBoard(mine, ROW, COL); // 打印放好雷的棋盘 只需里面的9*9
	DisplayBoard(show, ROW, COL);

// game.h
void DisplayBoard(char board[ROWS][COLS], int row, int col); // 传的还是11*11 但操作的是9*9

// game.c
// 11*11下标0-11  里面9*9棋盘是1-9 从1开始
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 0; i <= 9; i++) // 打印列号
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++) // 从1开始 小于等于9
	{
		int j = 0;
		printf("%d ", i); // 打印行号
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("----------------------\n");
}

5、布置雷

  1. 布置雷SetMine(mine数组的9*9里)
  2. 生成随机值下标
  3. 判断如果没有雷 放置雷
// test.c 
SetMine(mine, ROW, COL); // 雷放在mine数组的9*9里

// game.h
void SetMine(char board[ROWS][COLS], int row, int col);

// game.c
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;

	while (count)
	{
		// 1. 生成随机值下标
		// main函数里调rand
		int x = rand() % row + 1; // row是9 %9是0-8 加一是1-9
		int y = rand() % col + 1;

		if (board[x][y] != '1')
		{
			board[x][y] = '1'; // 判断如果没有雷 放置雷
			count--; // 放置成功--
		}
	}
}

6、排查雷

  1. 排查了FindMine(涉及mine和show数组)
  2. 判断坐标是否合法
  3. 调用GetMineCount函数算周围有几个雷
// test.c
FindMine(mine, show, ROW, COL); // 涉及mine和show数组

// game.h
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

// game.c
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) // 71
	{
		printf("请输入要排查的坐标:>\n");
		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'; // 例'3'-'0'=3 打印的是字符 所以要加字符0
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,重新输入\n");
		}
	}

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

调用GetMineCount函数算周围有几个雷

int GetMineCount(char mine[ROWS][COLS], int x, int y) // 计算雷的个数
{
	// 都是字符 减字符0就是数字 一周8个
	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');
}

五、代码

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(mine, ROW, COL); // 打印放好雷的棋盘 只需里面的9*9
	//DisplayBoard(show, ROW, COL);

	// 布置雷
	SetMine(mine, ROW, COL); // 雷放在mine数组的9*9里
	//DisplayBoard(mine, ROW, COL); // 布置好雷后 打印
	DisplayBoard(show, ROW, COL);

	// 排查雷
	FindMine(mine, show, ROW, COL); // 涉及mine和show数组
}

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>\n");
		scanf("%d", &input);

		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	} while (input);

	return 0;
}

game.h

#pragma once


#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 // 简单版 10个雷

// 初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

// 打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col); // 传的还是11*11 但操作的是9*9

// 布置雷
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

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


// 打印棋盘
// 11*11下标0-11  里面9*9棋盘是1-9 从1开始
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 0; i <= 9; i++) // 打印列号
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++) // 从1开始 小于等于9
	{
		int j = 0;
		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)
	{
		// 1. 生成随机值下标
		// main函数里调rand
		int x = rand() % row + 1; // row是9 %9是0-8 加一是1-9
		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) // 计算雷的个数
{
	// 都是字符 减字符0就是数字 一周8个
	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) // 71
	{
		printf("请输入要排查的坐标:>\n");
		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'; // 例'3'-'0'=3 打印的是字符 所以要加字符0
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,重新输入\n");
		}
	}

	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

三春去后诸芳尽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值