用c语言实现(完美)扫雷

目录

一,游戏的玩法以及设计理念

二,各种功能的代码实现

      1,创建菜单(菜单--menu)

      2,主函数的实现

      3,创建棋盘并且初始化(初始化--initial)

      4,打印棋盘(打印--display)

      5,布置雷的位置(布置---deposit)

      6,排查雷(排查---check)

----本文的标识符名称都是使用表示其含义的英文,使读者更加通俗易懂!------

三,代码汇总

        1,test.c文件

        2,game.c文件

        3,game.h文件


一,游戏的玩法

扫雷游戏的玩法:给出一张棋盘,根据我们输入的坐标位置来进行排查,可以选择是否排雷,是--则在雷上做标记,否则继续,如果该位置有雷我们没有进行排雷,则被炸死---游戏结束;如果不是雷,则该位置显示周围雷的个数,如果该位置周围没有雷,则显示空白,同时判定其周围的位置情况如出一辙(这里我们用递归实现);继续排查-----当棋盘上只有雷的时候游戏胜利!

   设计思想:

        1,在创建定义棋盘时,我们这里实现打印9*9的棋盘,用二维数组来实现!

        2,我们要创建两个棋盘,一个是存放雷的棋盘(后台信息),一个用来存放排查出雷的信息(显示的棋盘)!

        3,初始化棋盘时,存放雷的棋盘全部先存放字符‘0’,显示棋盘全部先存放字符‘*‘!

        4,布置雷:由电脑随机布置----用字符’1‘表示雷!

        5,排查雷:又玩家自行判断----用字符’#‘表示标记!

二,各功能代码的实现

 1,创建菜单------

void menu()
{
	printf("**********************\n");
	printf("***     1.play    ****\n");
	printf("***     0.exit    ****\n");
	printf("**********************\n");
}

2,主函数的实现-----

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

3,创建棋盘并且初始化----

      创建时建立两个二维数组:

#define ROW 9
#define COL 9

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

//创建存放雷的棋盘
char depositboard[ROWS][COLS] = { 0 };
//创建排查雷的棋盘
char checkboard[ROWS][COLS] = { 0 };

    棋盘的初始化:

//初始化棋盘
initboard(depositboard, ROWS, COLS,'0');
initboard(checkboard, ROWS, COLS, '*');


//初始化棋盘
void initboard(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ret;
		}
	}
}

4,打印棋盘-----

//打印棋盘
displayboard(depositboard, ROW, COL);
printf("\n");
displayboard(checkboard, ROW, COL);

//打印棋盘
void displayboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 0; i <= row; i++)  
	{
		printf(" %d ",i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		int j = 0;
		printf(" %d ", i);
			for (j = 1; j <= col; j++)
			{
				printf(" %c ", board[i][j]);
			}
			printf("\n");
	}
}

5,布置雷的位置-----

//布置雷
depositray(depositboard, ROW, COL);
displayboard(depositboard, ROW, COL);

//布置雷
void depositray(char board[ROWS][COLS], int row, int col)
{
	int count = RAY;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

6,排查雷-----

//排查雷
checkray(depositboard, checkboard, ROW, COL);

//算周围棋子数量
char raysum(char board[ROWS][COLS], int x, int y)
{
	return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
		+ board[x][y - 1] + board[x][y + 1]
		+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1])-'0'*7;
}
//递归实现空白
int check(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y,int* count)
{
	(*count)--;
	board2[x][y] = raysum(board1, x, y);
	int i = 0;
	int j = 0;
	if (board2[x][y] == '0')
	{
		board2[x][y] = ' ';
		for (i = x - 1; i <= x + 1; i++)
		{
			if (i >= 1 && i <= ROW)
			{
				for (j = y - 1; j <= y + 1; j++)
				{
					if (j >= 1 && j <= COL)
					{
						if (i != x || j != y || (i == x && j != y) || (i != x && j == y))
						{
							if(board2[i][j]=='*')
							check(board1, board2, i, j, count);
						}
					}
				}
			}
		}
	}
	return count;
}
//排查雷
void checkray(char board1[ROWS][COLS],char board2[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("请排查->\n");
	int count =row*col- RAY;
	while (count)
	{
		int ray = 0;
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			printf("是否排雷?--1表示排雷--否则不排\n");
			scanf("%d", &ray);
			if (ray == 1)
			{
				system("cls");
				board2[x][y] = '#';
				displayboard(board2, ROW, COL);
			}
			else {
				if (board1[x][y] == '1')
				{
					printf("你已经被雷炸死了\n");
					displayboard(board1, ROW, COL);
					break;
				}
				else
				{
					system("cls");
					check(board1, board2, x, y, &count);
					displayboard(board2, ROW, COL);
				}
			}
		}
		else printf("输入非法,请重新输入->\n");
	}
	if (count == 0)
	{
		printf("恭喜你,扫雷成功!\n");
		displayboard(board1, ROW, COL);
	}
}

 在这过程中统计周围雷的个数的函数的实现----raysum()----

//算周围棋子数量
char raysum(char board[ROWS][COLS], int x, int y)
{
	return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
		+ board[x][y - 1] + board[x][y + 1]
		+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1])-'0'*7;
}

在这过程用递归进行区域的扩展的函数实现---check()---

//递归实现空白
int check(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y,int* count)
{
	(*count)--;
	board2[x][y] = raysum(board1, x, y);
	int i = 0;
	int j = 0;
	if (board2[x][y] == '0')
	{
		board2[x][y] = ' ';
		for (i = x - 1; i <= x + 1; i++)
		{
			if (i >= 1 && i <= ROW)
			{
				for (j = y - 1; j <= y + 1; j++)
				{
					if (j >= 1 && j <= COL)
					{
						if (i != x || j != y || (i == x && j != y) || (i != x && j == y))
						{
							if(board2[i][j]=='*')
							check(board1, board2, i, j, count);
						}
					}
				}
			}
		}
	}
	return count;
}

三,代码汇总

  1,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()
{
	//创建存放雷的棋盘
	char depositboard[ROWS][COLS] = { 0 };
	//创建排查雷的棋盘
	char checkboard[ROWS][COLS] = { 0 };

	//初始化棋盘
	initboard(depositboard, ROWS, COLS,'0');
	initboard(checkboard, ROWS, COLS, '*');
	//打印棋盘
	/*displayboard(depositboard, ROW, COL);
	printf("\n");*/
	displayboard(checkboard, ROW, COL);
	printf("\n");
	//布置雷
	depositray(depositboard, ROW, COL);
	displayboard(depositboard, ROW, COL);
	//排查雷
	checkray(depositboard, checkboard, ROW, COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择->\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

2,game.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化棋盘
void initboard(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ret;
		}
	}
}
//打印棋盘
void displayboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 0; i <= row; i++)  
	{
		printf(" %d ",i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		int j = 0;
		printf(" %d ", i);
			for (j = 1; j <= col; j++)
			{
				printf(" %c ", board[i][j]);
			}
			printf("\n");
	}
}
//布置雷
void depositray(char board[ROWS][COLS], int row, int col)
{
	int count = RAY;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
//算周围棋子数量
char raysum(char board[ROWS][COLS], int x, int y)
{
	return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
		+ board[x][y - 1] + board[x][y + 1]
		+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1])-'0'*7;
}
//递归实现空白
int check(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y,int* count)
{
	(*count)--;
	board2[x][y] = raysum(board1, x, y);
	int i = 0;
	int j = 0;
	if (board2[x][y] == '0')
	{
		board2[x][y] = ' ';
		for (i = x - 1; i <= x + 1; i++)
		{
			if (i >= 1 && i <= ROW)
			{
				for (j = y - 1; j <= y + 1; j++)
				{
					if (j >= 1 && j <= COL)
					{
						if (i != x || j != y || (i == x && j != y) || (i != x && j == y))
						{
							if(board2[i][j]=='*')
							check(board1, board2, i, j, count);
						}
					}
				}
			}
		}
	}
	return count;
}
//排查雷
void checkray(char board1[ROWS][COLS],char board2[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("请排查->\n");
	int count =row*col- RAY;
	while (count)
	{
		int ray = 0;
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			printf("是否排雷?--1表示排雷--否则不排\n");
			scanf("%d", &ray);
			if (ray == 1)
			{
				system("cls");
				board2[x][y] = '#';
				displayboard(board2, ROW, COL);
			}
			else {
				if (board1[x][y] == '1')
				{
					printf("你已经被雷炸死了\n");
					displayboard(board1, ROW, COL);
					break;
				}
				else
				{
					system("cls");
					check(board1, board2, x, y, &count);
					displayboard(board2, ROW, COL);
				}
			}
		}
		else printf("输入非法,请重新输入->\n");
	}
	if (count == 0)
	{
		printf("恭喜你,扫雷成功!\n");
		displayboard(board1, ROW, COL);
	}
}

3,game.h文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9

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

#define RAY 2
//初始化棋盘
void initboard(char board[ROWS][COLS], int row, int col, char ret);
//打印棋盘
void displayboard(char board[ROWS][COLS], int row, int col);
//布置雷
void depositray(char board[ROWS][COLS], int row, int col);
//排查雷
void checkray(char board1[ROWS][COLS],char board2[ROWS][COLS], int row, int col);

嘻嘻,慢慢来一步一步不要着急,思绪要好,如果本文对你有启发的话就给哥们来个”一键三连“

好了,就到这里了,拜拜了您嘞!

猜你喜欢

转载自blog.csdn.net/m0_71676870/article/details/130597547
今日推荐