扫雷游戏的实现

实现步骤:1、创建两个棋盘,一个放雷,一个用来展开给玩家看

                2、先放雷,然后让玩家操作来排雷

主要的问题就是如何将棋盘展开

下面就是实现的代码了:

game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 

#define COLS 11
#define ROWS 11
#define ROW 9
#define COL 9
#define Mine_Count 10

void BoardInit(char board[ROWS][COLS], int row, int col, char x);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void PushMine(char board[ROWS][COLS], int row, int col);
void ClearMine(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int row, int col);

void ScanMinel(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMiner(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMinelt(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMinelb(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMineb(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMinerb(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMinert(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);
void ScanMinet(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);

int IsWin(char numboard[ROWS][COLS], int row, int col);
void ScanMine(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y);

game.c

#include "game.h"

void BoardInit(char board[ROWS][COLS], int row, int col, char x)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			board[i][j] = x;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 1;
	printf("  ");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void PushMine(char board[ROWS][COLS], int row, int col)
{
	int count = Mine_Count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '*';
			count--;
		}
	}
}

//[x-1][y-1] [x][y-1] [x+1][y-1] [x+1][y] [x+1][y+1] [x][y+1] [x-1][y+1] [x-1][y]
	//ScanMine(numboard, mineboard, x - 1, y - 1);//1=
	//ScanMine(numboard, mineboard, x, y - 1);//2=
	//ScanMine(numboard, mineboard, x + 1, y - 1);//3=
	//ScanMine(numboard, mineboard, x + 1, y);//4=
	//ScanMine(numboard, mineboard, x + 1, y + 1);//5=
	//ScanMine(numboard, mineboard, x - 1, y + 1);//7=
	//ScanMine(numboard, mineboard, x - 1, y);//8=

void ScanMinet(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMinet(numboard, mineboard, x-1, y);
}

void ScanMinert(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMinert(numboard, mineboard, x - 1, y + 1);
}

void ScanMinerb(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMinerb(numboard, mineboard, x+1, y + 1);
}

void ScanMineb(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMineb(numboard, mineboard, x + 1, y);
}

void ScanMinelb(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMinelb(numboard, mineboard, x + 1, y - 1);
}

void ScanMinelt(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMinelt(numboard, mineboard, x - 1, y - 1);
}

void ScanMiner(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;

	ScanMiner(numboard, mineboard, x, y - 1);
}

void ScanMinel(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';
	if (numboard[x][y] != '0' || x < 1 || y < 1 || x > ROW || y > COL)
		return;
	
	ScanMinel(numboard, mineboard, x, y + 1);//6
}

void ScanMine(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int x, int y)
{
	int count = 0;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (mineboard[i][j] == '*')
				count++;
		}
	}
	numboard[x][y] = count + '0';

	ScanMinel(numboard, mineboard, x, y);
	ScanMiner(numboard, mineboard, x, y);
	ScanMinet(numboard, mineboard, x, y);
	ScanMineb(numboard, mineboard, x, y);
	ScanMinelt(numboard, mineboard, x, y);
	ScanMinelb(numboard, mineboard, x, y);
	ScanMinerb(numboard, mineboard, x, y);
	ScanMinert(numboard, mineboard, x, y);
}

int IsWin(char numboard[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (numboard[i][j] != '@')
				count++;
		}
	}
	if (count + Mine_Count == row * col)
		return 1;
	else
	{
		return 0;
	}
}

void ClearMine(char numboard[ROWS][COLS], char mineboard[ROWS][COLS], int row, int col)
{
	int c_num = 0;
	while (!IsWin(numboard, row, col))
	{
		int x = 0, y = 0;
		
		printf("请输入你想要点击的坐标:>");
		scanf("%d %d", &x, &y);
		if (x<1 || y<1 || x>row || y>col)
		{
			printf("您输入的坐标有误,请重新输入\n");
			continue;
		}
		if (numboard[x][y] != '@')
		{
			printf("这个坐标已经展开了哦\n");
			continue;
		}
		if (mineboard[x][y] == '*')
		{
			printf("你被地雷炸死啦!\n");
			DisplayBoard(mineboard, ROW, COL);
			break;
		}
		ScanMine(numboard, mineboard, x, y);
		int fx = x; int fy = y;
		while (numboard[fx][fy] == '0')
		{
			ScanMine(numboard, mineboard, fx, fy);
			fx++;
		}
		fx = x; fy = y;
		while (numboard[fx][fy] == '0')
		{
			ScanMine(numboard, mineboard, fx, fy);
			fx--;
		}
		fx = x; fy = y;
		while (numboard[fx][fy] == '0')
		{
			ScanMine(numboard, mineboard, fx, fy);
			fy++;
		}
		fx = x; fy = y;
		while (numboard[fx][fy] == '0')
		{
			ScanMine(numboard, mineboard, fx, fy);
			fy--;
		}
		
		DisplayBoard(numboard, ROW, COL);
	}
	if (IsWin(numboard,row,col))
		printf("恭喜你获得游戏胜利!\n");
}

test.c

#include "game.h"

void menu()
{
	printf("*******************************\n");
	printf("*******   1、扫雷游戏   ********\n");
	printf("*******   0、退出游戏   ********\n");
	printf("*******************************\n");
}

void game()
{
	char mineboard[ROWS][COLS] = { 0 };
	char numboard[ROWS][COLS] = { 0 };
	BoardInit(mineboard, ROWS, COLS, '0');
	BoardInit(numboard, ROWS, COLS, '@');
	//放雷
	PushMine(mineboard, ROW, COL);
	//DisplayBoard(mineboard, ROW, COL);
	DisplayBoard(numboard, ROW, COL);
	//排雷
	ClearMine(numboard, mineboard, ROW, COL);
	
}

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

下面给大家看一下玩游戏的过程O(∩_∩)O

扫雷游戏的实现_第1张图片

 

你可能感兴趣的:(游戏,c++,开发语言)