扫雷(简易版 9*9)

1.首先对棋盘初始化,并印出棋盘。
2.对棋盘进行埋雷。
3.输入坐标进行排雷。
4.编写函数统计当前位置周围的雷数,当周围8个位置都没有雷时进行递归展开。
5.统计棋盘中未展开的坐标数是否与雷数相等来判断输赢。

创建2个棋盘,一个用来布置雷,一个用来展示给用户体验,

棋盘总大小为9*9,但在边界的时候判断雷的信息会越界,所有一个创建一个11*11的棋盘

代码如下:

game.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 9
#define COL 9

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


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

void Display(char Board[ROWS][COLS], int row, int col);//显示棋盘

void SetMine(char Board[ROWS][COLS], int row, int col);//布置雷

void FindMy(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];//自己的棋盘
	char Show[ROWS][COLS];//展示的棋盘
	InitBoard(Mine, ROWS, COLS,'0');       //初始化 Mine 棋盘 '0'
	InitBoard(Show, ROWS, COLS,'*');       //初始化 Show 棋盘 '*'

	Display(Show, ROW, COL);//显示 Show 棋盘

	SetMine(Mine, ROW, COL);//布置 Mine 雷
	//Display(Mine, ROW, COL);//显示 Mine 棋盘

	FindMy(Mine, Show, ROW, COL);//排查雷

}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));;
	do 
	{
		menu();
		printf("请输入 : ");
		scanf_s("%d", &input);
		switch(input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,重新输入\n");
		}
	} while (input);
	return 0;
}

初始化棋盘:

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 Display(char Board[ROWS][COLS], int row, int col)//显示雷
{
	printf("  ");
	for (int i = 1;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_MINE;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (Board[x][y] == '0')
		{
			Board[x][y] = '1';
			count--;
		}
	}
}

统计坐标周围雷的个数:

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

递归展开:

void Open(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col)//递归展开
{
	if (row > 0 && row <= ROW && col > 0 && col <= COL)
	{
		int c = Getcount(Mine, row, col) ;//统计坐标周围雷的个数
		if (c != 0)
			Show[row][col] = c +'0';

		else if (Show[row][col] != ' ')
		{
			Show[row][col] = ' ';
			int i = 0;
			for (i = row - 1; i <= row + 1;i++)
			{
				int j = 0;
				for (j = col-1;j <= col + 1;j++)
				{
					Open(Mine, Show, i, j);
				}
			}
		}
		else
		{
			return;
		}
	}

}

排查雷:

void FindMy(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col)//排查雷
{
	int x, y;
	int win = 0;//统计是否把雷排完
	while (win<row*col-EASY_MINE)
	{
		printf("请输入坐标 : ");
		scanf_s("%d %d", &x, &y);
		printf("\n");
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (Mine[x][y] == '1')
			{
				Display(Mine, ROW, COL);//显示 Mine 棋盘
				printf("你死了\n\n");
				break;
			}
			else
			{
				Open(Mine, Show, x, y);//展开棋盘
				Display(Show, ROW, COL);//显示 Show 棋盘
				win++;
				printf("\n");
			}
		}
		else
			printf("坐标非法,重新输入\n");
	}
	if (win == row * col - EASY_MINE)
	{
		Display(Mine, ROW, COL);//显示 Mine 棋盘
		printf("赢了\n");
		printf("\n");
	}
}

效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于C语言的9x9扫雷游戏代码示例,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 9 #define COLS 9 #define ROW 9 #define COL 9 #define COUNT 10 void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col, int count); int GetMineCount(char board[ROWS][COLS], int x, int y); int main() { char mine[ROW][COL] = {0}; // 扫雷棋盘 char show[ROW][COL] = {0}; // 显示棋盘 int x, y; // 坐标 int win = 0; // 是否胜利 int total = ROW * COL - COUNT; // 扫雷总数 // 初始化棋盘 InitBoard(mine, ROW, COL, '0'); InitBoard(show, ROW, COL, '*'); DisplayBoard(show, ROW, COL); // 布雷 srand((unsigned int)time(NULL)); SetMine(mine, ROW, COL, COUNT); while (win < total) { printf("请输入坐标(x,y):\n"); scanf("%d%d", &x, &y); if (x < 1 || x > ROW || y < 1 || y > COL) { printf("坐标非法,请重新输入!\n"); continue; } x--; y--; if (mine[x][y] == 'X') // 踩雷 { printf("很遗憾,你踩雷了!\n"); DisplayBoard(mine, ROW, COL); break; } else if (show[x][y] != '*') // 重复操作 { printf("该位置已经被打开,请重新输入!\n"); continue; } else // 打开格子 { int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; if (win == total) // 胜利 { printf("恭喜你,扫雷成功!\n"); DisplayBoard(mine, ROW, COL); break; } } } return 0; } // 初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = ch; } } } // 显示棋盘 void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i, j; printf(" "); for (i = 1; 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 - 1][j - 1]); } printf("\n"); } } // 布雷 void SetMine(char board[ROWS][COLS], int row, int col, int count) { int i, x, y; for (i = 0; i < count; i++) { do { x = rand() % row; y = rand() % col; } while (board[x][y] == 'X'); board[x][y] = 'X'; } } // 计算周围雷数 int GetMineCount(char board[ROWS][COLS], int x, int y) { int count = 0; if (x > 0 && board[x - 1][y] == 'X') count++; if (x < ROW - 1 && board[x + 1][y] == 'X') count++; if (y > 0 && board[x][y - 1] == 'X') count++; if (y < COL - 1 && board[x][y + 1] == 'X') count++; if (x > 0 && y > 0 && board[x - 1][y - 1] == 'X') count++; if (x > 0 && y < COL - 1 && board[x - 1][y + 1] == 'X') count++; if (x < ROW - 1 && y > 0 && board[x + 1][y - 1] == 'X') count++; if (x < ROW - 1 && y < COL - 1 && board[x + 1][y + 1] == 'X') count++; return count; } ``` 该代码实现了一个基本的9x9扫雷游戏,具有布雷、打开格子、计算周围雷数等功能。但是,该代码仅供参考,完整的扫雷游戏需要进一步完善和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值