扫雷完整代码

 play.h

#pragma once
 
#include
#include
#include

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

//初始化棋盘
void is_board(char board[ROWS][COLS], int rows, int cols,char ret);
//打印棋盘
void print_board(char board[ROWS][COLS], int row, int col);

//布置雷的位置
void set_mine(char mine[ROWS][COLS], int row, int col);

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

tast.c

#define _CRT_SECURE_NO_WARNINGS
#include"play.h"
void game()
{
	//二维数组 --- 9 * 9 棋盘 --- 考虑一个方格周围8个方格时,可以横纵增大一圈,即为 11 * 11
	char mine[ROWS][COLS] = { 0 };//排查雷
	char show[ROWS][COLS] = { 0 };//排查出雷的信息
	//初始化棋盘
	is_board(mine, ROWS, COLS,'0');
	is_board(show, ROWS, COLS,'*');
	//打印棋盘
	print_board(show, ROW, COL);
	//print_board(mine, ROW, COL);
	//布置雷的位置 --- 排查雷
	set_mine(mine,ROW,COL);//布置雷的位置--- 传入mine
	//print_board(mine, ROW, COL);
	clear_mine(mine,show, ROW, COL);//排雷 -- mine排查雷 -- 放到show 

}
void menu()
{
	printf("**************************\n");
	printf("***** 1.   play      *****\n");
	printf("***** 0.   exit      *****\n");
	printf("**************************\n");
}
int main()
{
	//打印菜单
	int input = 0;//输入值
	srand((unsigned int)time(NULL));//获取随机值 0 - 32767
	do
	{
		menu();
		printf("请输入—>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			//printf("玩游戏\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

 play.c

#define _CRT_SECURE_NO_WARNINGS
#include"play.h"

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

//打印棋盘 --- 不打印拓展的两行两列
void print_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;//行
	int j = 0;//列,每行
	printf("****** 扫 雷 ******\n");
	for (j = 0; j <= col; j++)//打印列号.为保证行列相对应,从零开始
	{
		printf("%d ", j);
	}
	printf("\n");//换行
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)

		{
			printf("%c ",board[i][j]);
		}
		printf("\n");
	}                                                                                 
}

//布置雷的位置
void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int count = COUNT;
	//int x = rand() % row + 1;//获取的随机值 % 9 = (0-8) +1 = (1 - 9) 
	//int y = rand() % col + 1;//1-9 表示对应的行列
	//写在外面,会进入死循环,每次的x,y都是同一个值
	while (count)//当count为0时,循环结束
	{
		int x = rand() % row + 1;//获取的随机值 % 9 = (0-8) +1 = (1 - 9) 
		int y = rand() % col + 1;//1-9 表示对应的行列
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//排查雷
int cal_lei(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 clear_mine(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 - COUNT)
	{
		printf("请输入扫雷的坐标—>");
		scanf("%d %d", &x, &y); 
		//判断输入坐标的合法性
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] == '*')
			{
				//判断成功输入的坐标是否为雷
				if (mine[x][y] == '1')//'0'为非雷 '1'为雷
				{
					printf("很遗憾,游戏失败\n");
						print_board(mine, ROW, COL);
						break;
				}
				else
				{
					//传入输入的坐标值
					int count = cal_lei(mine, x, y);//计算周围雷的总个数
					show[x][y] = count + '0';
					print_board(show, ROW, COL);
					win++;
				}
			}
			else
			{
				printf("该坐标已被排查");
			}
		}
		else
		{
			printf("输入的坐标不在指定范围内,请重新输入\n");
		}
	}
	if (win == row * col - COUNT)
	{
		printf("恭喜你排雷成功\n");
		print_board(mine, ROW, COL);
	}
}

你可能感兴趣的:(c语言)