话不多说,来一场9vs9的扫雷吧!

扫雷

扫雷游戏:游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。
在这里插入图片描述

扫雷实现

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

扫雷代码

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu()//打印游戏菜单
{
    
    
	printf("********************\n");
	printf("*****  1.paly  *****\n");
	printf("*****  0.exit  *****\n");
	printf("********************\n");
}

void game()
{
    
    
	char mine[ROWS][COLS] = {
    
     0 };//存放布置好的雷
	char show[ROWS][COLS] = {
    
     0 };//存放排查好的雷

	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');//'0'
	InitBoard(show, ROWS, COLS, '*');//'*'

	//打印一下棋盘
	DisplayBoard(show, ROW, COL);

	//布置雷
	SetMine(mine, ROW, COL);
	
	//排查雷
	FindMine(mine, show, ROW, COL);
}
int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));

	 do
	{
    
    
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 2:
			printf("退出游戏");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	 } while (input);
	return 0;
}

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

void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印表格
{
    
    
	int i = 0;
	int j = 0;
	printf("—··扫雷游戏··—\n");

	for (i = 0; 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][j]);
		}
		printf("\n");
	}
	printf("—··扫雷游戏··—\n");
}

void SetMine(char mine[ROWS][COLS], int row, int col)//布置雷
{
    
    
	//布置10个雷
	int count = EASY_COUNT;
	while (count)
	{
    
    
		//生产随机的下标
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
    
    
			mine[x][y] = '1';
			count--;
		}
	}
}


static int get_mine_count(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 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
			{
    
    
				
				int count = get_mine_count(mine, x, y);//不是雷情况下,统计x,y坐标周围有几个雷
				show[x][y] = count+'0';
				DisplayBoard(show, row, col);//显示排查出的信息
				win++;
			}
		}
		else
		{
    
    
			printf("坐标不合法,请重新输入\n");
		}
	}

	if (win == row * col - EASY_COUNT)
	{
    
    
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, row, col);//显示被隐藏的坐标
	}
}

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 mine[ROWS][COLS], int row, int col);

//排查雷
void FindMine(char mine[ROWS][COLS], char shwo[ROWS][COLS], int row, int col);

关于扫雷,我想说。

真上头!!!

猜你喜欢

转载自blog.csdn.net/qq_52988578/article/details/116243562