扫雷(简易版)

本人使用的是VS 2022

创建两个源文件(test.c   game.c),一个头文件(game.h)

具体操作如下:(以两数相加的函数举例)

扫雷(简易版)_第1张图片

1.构建大体的游戏框架

扫雷(简易版)_第2张图片

2.在game()函数里实现扫雷游戏(9*9的格子(可更改),10颗雷(可更改))

扫雷(简易版)_第3张图片 

(1)创建两个11*11的二维数组 (2)初始化数组  (3)打印扫雷的格子

 扫雷(简易版)_第4张图片

(4)在mine里布置雷:通过随机数随机布置10颗雷 

 扫雷(简易版)_第5张图片

(5)玩游戏

 

 具体的代码如下:

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()
{
	//创建11*11的二维数组
	char show[ROWS][COLS] = { 0 };
	char mine[ROWS][COLS] = { 0 };
	//初始化数组
	Initboard(show, ROWS, COLS, '*');
	Initboard(mine, ROWS, COLS, '0');
	//打印9*9的格子
	Displayboard(show, ROW, COL);
	//Displayboard(mine, ROW, COL);
	//布置雷
	Setmine(mine, ROW, COL);
	Displayboard(mine, ROW, COL);
	//玩游戏
	Playgame(show, mine, ROW, COL);
}

void test()
{
	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("选择错误,请重新选择:");
			break;
		}
	} while (input);
}


int main()
{
	test();
	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;
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	int j = 0;
	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)
{
	int count = EASY;
	while (count)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

int Count(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 Playgame(char show[ROWS][COLS], char mine[ROWS][COLS], int rows, int cols)
{
	int count = ROW * COL - EASY;
	while (count)
	{
		int x = 0;
		int y = 0;
		printf("请选择坐标:");
		scanf_s("%d %d", &x, &y);
		if (show[x][y] == '*')
		{
			if (mine[x][y] != '1')
			{
				show[x][y] = Count(mine, x, y) + '0';
				Displayboard(show, ROW, COL);
				count--;
			}
			else
			{
				printf("踩雷了,游戏失败\n");
				break;
			}
		}
		else
		{
			printf("此坐标已排查,请重新选择\n");
		}
	}
	if (count == 0)
	{
		printf("游戏获胜\n");
	}
}

add.h

#pragma once

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

#define EASY 10


#include
#include
#include

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 Playgame(char show[ROWS][COLS], char mine[ROWS][COLS], int rows, int cols);

你可能感兴趣的:(c语言,算法,c++)