第一次扫雷游戏

扫雷游戏

扫雷的过程中,布置的雷和排查出的雷的信息都需要存储

利用了多⽂件的形式对函数的声明和定义

分析

假设我们排查(8,6)这个坐标时,我们访问周围的⼀圈8个⻩⾊位置,统计周围雷的个数时,最下⾯的三 个坐标就会越界,为了防⽌越界,我们在设计的时候,给数组扩⼤⼀圈,雷还是布置在中间的99的坐 标上,周围⼀圈不去布置雷就⾏,这样就解决了越界的问题。所以我们将存放数据的数组创建成1111 是⽐较合适。
在这里插入图片描述

test.c //⽂件中写游戏的测试逻辑 
game.c //⽂件中写游戏中函数的实现等
game.h //⽂件中写游戏需要的数据类型和函数声明等

首先对于 test.c

//先写出游戏开始表单
void menu()
{
	printf("************************\n");
	printf("*********1.play*********\n");
	printf("*********0.exit*********\n");
	printf("************************\n");
}
int main()
{
	int input = 0;
    //写出一个循环实现多次打印表单
	do
	{
		menu();//调用表单打印函数
		printf("请选择:》");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//扫雷游戏函数
			game();
			break;
		case 0:
			printf("退出游戏");
			break;
		default:
                //如果选择非1,0的值
			printf("重新选择");
			break;
		}
	} while (input);
    //输出1进入扫雷游戏函数
    //输出0退出整个游戏
	return 0;
}

在game()函数中

写出存放布置好的雷的信息,以及来存放排查出的雷的个数信息

定义两个数组

char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息
char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息

之后在game.h文件进行定义常量

#define ROW 9
#define COL 9

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

1.分别写出初始化棋盘函数,进行对两个数组初始化

//初始化棋盘
	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)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

在这里插入图片描述

2.写出布置棋盘雷的函数

//布置雷
SetMine(mine, ROW, COL);

在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;//该常量由game.h文件进行定义
    //#define EASY_COUNT 10
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
//以上函数进行随机在数组中进行布置雷
//调⽤ rand 函数之前先调⽤ srand 函数,通过 srand 函数的参数seed来设置rand函数⽣成随机数的时候的种⼦
//srand((unsigned int)time(NULL));

在这里插入图片描述

3.写出排查棋盘雷的函数

FindMine(mine, show, ROW, COL);

在game.h文件定义排查函数

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)
	{
		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 
			{
				if (show[x][y]!='*')
				{
					printf("已排查过\n");
				}
				else
				{
					//统计调用
					int count = Get_Mine_Count(mine, x, y);
					show[x][y] = count + '0';
					DisplayBoard(show, ROW, COL);
					win++;
				}
	
			}
		}
		else
		{
			printf("请输入坐标非法,请重新输入");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,赢了\n");
		DisplayBoard(mine, ROW, COL);
	}
}
//统计排查周围8个方块函数
int Get_Mine_Count(char mine[ROWS][COLS], int x,int y)
{
	return mine[x - 1][y] + mine[x - 1][y - 1] + 
		mine[x - 1][y + 1] +mine[x][y - 1] + mine[x][y + 1] +
		mine[x + 1][y] + mine[x + 1][y - 1] + mine[x + 1][y + 1] - 8 * '0';
}

最后一步写出打印函数

DisplayBoard(show, ROW, COL);

在game.h文件定义函数

void DisplayBoard(char board[ROWS][COLS], int row, int col);

在game.c文件书写打印函数

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("--------扫雷-------\n");
	//打印列号
	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");
	}
}

以上为基本编写过程

以下为代码

https://gitee.com/wuqianhg/c_-language-warehouse/commit/443e93e1b08ac58878aac5fe613a640df9edb622

后续还会进行修改,以及添加功能

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用C语言编写的扫雷游戏,满足您的要求。在这个版本中,第一次扫雷不会碰到雷。 ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 #define MINES 15 void initializeBoard(char board[SIZE][SIZE]) { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { board[i][j] = '-'; } } } void placeMines(char board[SIZE][SIZE]) { int count = 0; while (count < MINES) { int x = rand() % SIZE; int y = rand() % SIZE; if (board[x][y] != '*') { board[x][y] = '*'; count++; } } } void printBoard(char board[SIZE][SIZE]) { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { printf("%c ", board[i][j]); } printf("\n"); } } int countMines(char board[SIZE][SIZE], int row, int col) { int count = 0; int i, j; for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < SIZE && j >= 0 && j < SIZE && board[i][j] == '*') { count++; } } } return count; } void revealCell(char board[SIZE][SIZE], char displayBoard[SIZE][SIZE], int row, int col) { if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || displayBoard[row][col] != '-') { return; } displayBoard[row][col] = board[row][col]; if (board[row][col] == '0') { revealCell(board, displayBoard, row - 1, col - 1); revealCell(board, displayBoard, row - 1, col); revealCell(board, displayBoard, row - 1, col + 1); revealCell(board, displayBoard, row, col - 1); revealCell(board, displayBoard, row, col + 1); revealCell(board, displayBoard, row + 1, col - 1); revealCell(board, displayBoard, row + 1, col); revealCell(board, displayBoard, row + 1, col + 1); } } int main() { char board[SIZE][SIZE]; char displayBoard[SIZE][SIZE]; int row, col; int firstMove = 1; srand(time(NULL)); initializeBoard(board); placeMines(board); for (row = 0; row < SIZE; row++) { for (col = 0; col < SIZE; col++) { if (board[row][col] != '*') { int count = countMines(board, row, col); board[row][col] = count + '0'; } } } initializeBoard(displayBoard); while (1) { printf("Current board:\n"); printBoard(displayBoard); if (firstMove) { printf("Enter row and column for your first move (e.g., 2 3): "); scanf("%d %d", &row, &col); firstMove = 0; if (board[row][col] == '*') { // If the first move hits a mine, regenerate the board initializeBoard(board); placeMines(board); for (row = 0; row < SIZE; row++) { for (col = 0; col < SIZE; col++) { if (board[row][col] != '*') { int count = countMines(board, row, col); board[row][col] = count + '0'; } } } initializeBoard(displayBoard); revealCell(board, displayBoard, row, col); } else { revealCell(board, displayBoard, row, col); } } else { printf("Enter row and column (e.g., 2 3): "); scanf("%d %d", &row, &col); if (board[row][col] == '*') { printf("Game over! You hit a mine.\n"); break; } else { revealCell(board, displayBoard, row, col); } } } printf("Final board:\n"); printBoard(board); return 0; } ``` 这个版本的扫雷游戏会在第一次扫雷时避开雷区,确保玩家不会在刚开始游戏时就触雷。希望对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值