扫雷—C语言(代码)

目录

扫雷

game.h

game.c

test.c

说明:


有所不足,请多包涵!



扫雷

game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

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

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

//初始化
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 board[ROWS][COLS], int row, int col);
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//求周围雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y);
//标志雷
void Mark(char show[ROWS][COLS], int row, int col);
//展开周围
void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],
	                int row, int col, int x, int y, int state[][COLS]);
//展开自己
void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS], 
	           int row, int col, int x, int y, int state[][COLS]);

game.c

#include "game.h"

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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("-------------------------\n");
	printf("  ");
	for (int i = 1; i <= col; 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_COUNT;
	while (count)
	{
		int x = rand() % row + 1;
		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)
{
	int ret = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mine[i][j] == '1')
			{
				ret++;
			}
		}
	}
	return ret;
}

void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS],
	           int row, int col, int x, int y, int state[][COLS])
{
	if (state[x][y] == 0)
	{
		state[x][y] = 1;
		int count = GetMineCount(mine, x, y);
		show[x][y] = '0' + count;
	}
}

void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],
	                int row, int col, int x, int y, int state[][COLS])
{
	if (state[x][y] == 0)
	{
		state[x][y] = 1;
		int count1 = GetMineCount(mine, x, y);
		show[x][y] = '0' + count1;
		for (int i = x - 1; i <= x + 1; i++)
		{
			for (int j = y - 1; j <= y + 1; j++)
			{
				if (i >= 1 && i <= ROW && j >= 1 && j <= COL)
				{
					if (state[i][j] == 0)
					{
						int count = GetMineCount(mine, i, j);
						if (count != 0)
							OpenOwn(show, mine, row, col, i, j, state);
						else
							OpenNeighbor(show, mine, row, col, i, j, state);
					}
				}
			}
		}
	}
	else
	{
		return;
	}
}


void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int win = 0;
	int state[ROWS][COLS] = { 0 };
	//一共有COL*ROW个格子,当我们排了COL*ROW-EASY_COUNT个格子的时候,扫雷就成功了
	while (win < COL * ROW - EASY_COUNT)
	{
		char whethermark[10] = { 0 };
		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 = GetMineCount(mine, x, y);
				if (count != 0)
				{
					win += 1;
					OpenOwn(show, mine, ROW, COL, x, y, state);
					system("cls");
					DisplayBoard(show, ROW, COL);
					Sleep(2000);
				}
				else
				{
					OpenNeighbor(show, mine, row, col, x, y, state);
					int nums = 0;
					for (int i = 1; i <= ROW; i++)
					{
						for (int j = 1; j <= COL; j++)
						{
							if (state[i][j] == 1)
							{
								nums++;
							}
						}
					}
					win = nums;
					system("cls");
					DisplayBoard(show, ROW, COL);
					Sleep(2000);
				}
				while (1)
				{
					printf("是否要标记雷?YES OR NO\n");
					scanf("%s", whethermark);
					if (strcmp(whethermark, "YES") == 0)
					{
						Mark(show, ROW, COL);
					}
					else
					{
						break;
					}
				}
			}
		}
		else
		{
			printf("输入错误请重新输入\n");
		}
	}
	if (win == COL * ROW - EASY_COUNT)
	{
		printf("恭喜你扫雷成功!\n");
		printf("再来一把?\n");
	}
}

void Mark(char show[ROWS][COLS], int row, int col)
{
	int x, y;
again:
	printf("请输入要标记为雷的坐标->");
	scanf("%d %d", &x, &y);
	if (x >= 1 && x <= row && y >= 1 && y <= col)
	{
		system("cls");
		show[x][y] = '#';
		DisplayBoard(show, ROW, COL);
		return;
	}
	else
	{
		printf("输入错误 请重新输入\n");
		goto again;
	}

}


test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void menu()
{
	printf("***************************\n");
	printf("********** 扫雷游戏  *******\n");
	printf("********** 0.exit   *******\n");
	printf("********** 1.play   *******\n");
	printf("********** 2.rule   *******\n");
	printf("***************************\n");
}

void rule()
{
	printf("操作注意:\n");
	printf("当你要输入扫雷的坐标是,请以'a b'形式输入\n");
	printf("当你要标记某位置为雷时,请输入'YES'\n");
	printf("如果不标记雷的位置,请输入NO\n");
	printf("当你把所有的非雷位置都扫掉以后,游戏将获得胜利\n");
	printf("祝您游戏愉快\n");
}

void game()
{
	//雷的信息存储
	//1.布置好的雷的信息
	char mine[ROWS][COLS] = { 0 };//11*11

	//2.排查出的雷的信息
	char show[ROWS][COLS] = { 0 };
	//初始化
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	//DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
	//布置雷
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
	//扫雷
	FindMine(mine, show, ROW, COL);
}

void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("退出游戏\n");
			break;
		case 1:
			system("cls");
			game();
			break;
		case 2:
			system("cls"); 
			rule();
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

说明:

 扫雷,我们需要两个数组做棋盘,一个棋盘是实际上的棋盘,用0表示无雷,用1表示有雷;一个棋盘是展示给玩家看的,用 * 覆盖:

如果你想标记雷,也是可以的: 

 如有问题,下方留言!

猜你喜欢

转载自blog.csdn.net/qq_54880517/article/details/123389279