扫雷(C语言)(展开一片)(标记)

1.打印菜单

首先我们先打印玩游戏的菜单,玩游戏不过瘾想再玩一次,用do while循环打印菜单

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
	printf("************************\n");
	printf("*******  1.play  *******\n");
	printf("*******  0.exit  *******\n");
	printf("************************\n");
}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("扫雷\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

效果如下:

************************
*******  1.play  *******
*******  0.exit  *******
************************
请选择:>1
扫雷
************************
*******  1.play  *******
*******  0.exit  *******
************************
请选择:>8
选择错误,请重新输入
************************
*******  1.play  *******
*******  0.exit  *******
************************
请选择:>0
退出游戏

2.初始化棋盘

我们要做的是一个9*9的扫雷棋盘,方格中数字则表示其周围的8个方格隐藏了几颗雷.

在这里插入图片描述

但是当排查角格周围的雷时,角格周围只有3个格子可以排查,剩下5个格子在棋盘外面,所以我们可以把棋盘扩展一圈。

在这里插入图片描述

相当于行增加2行,列增加2列,
9—>11

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

我们需要创建一个mine棋盘显示布置雷的信息,再创建一个show棋盘显示排查雷的信息。

void init_board(char board[ROWS][COLS], int rows, int cols, int set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

mine棋盘初始化为‘0’,有雷则为‘1’;
show棋盘初始化为‘*’表示没有排查过,排查过则显示数字字符代表周围8个格子里的雷和

    init_board(mine, ROWS, COLS,'0');
	init_board(show, ROWS, COLS,'*');

3.打印棋盘

初始化好之后打印两个棋盘

void display_board(char board[ROWS][COLS], int row, int col)
{
	printf("------扫雷游戏-----\n");//添加分界线方便观察
	int i = 0;
	int j = 0;
	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");
	}
	printf("------扫雷游戏-----\n");
}

效果如下:

------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0
------扫雷游戏-----
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
------扫雷游戏-----

标记好行号和列号,方便输入每个方格的坐标。

4.布置雷

我做的是简单模式,布置10个雷。

用到rand()函数,rand产生0到32767之间的随机数
rand()%9—>0到8;
rand()%9+1—>产生1~9之间的随机数

void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = 0;
	while (count < easy_count)
	{
		int x = 0;
		int y = 0;
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count++;
		}
	}
}

效果如下

------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 1 0 0 0 0 0 0 0 0
2 0 1 0 0 0 0 0 1 0
3 0 0 0 0 0 0 0 1 0
4 0 0 0 0 1 0 0 0 0
5 1 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 1
7 0 0 1 1 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 1 0 0
------扫雷游戏-----

5.排雷

接下来我们开始排雷,mine和show两个数组都要调用。

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	while (1)
	{
		int x = 0;
		int y = 0;
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)//保证不越界
		{
			if (show[x][y] != '*')
			{
				printf("该坐标已被排查,请重新输入\n");
			}
			else
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					display_board(mine, ROW, COL);
					break;
				}
				else
				{
					OpenUp(mine, show, ROW, COL, x, y);//如果该坐标不是雷,周围没有雷,实现打开一片
					display_board(show, ROW, COL);
					SignMine(show, ROW, COL);//标记雷
					display_board(show, ROW, COL);
					int ret=IsWin(show, ROW, COL);
					if (ret == easy_count)
					{
						printf("恭喜你,排雷成功\n");
						display_board(mine, ROW, COL);
						break;
					}
				}
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
}

5.1计算周围雷个数

在这里插入图片描述

布置雷的棋盘里面是雷为’1’,非雷为’0’
‘1’-‘0’=1
(字符1的ascii码值为49,字符0的ascii码值为48,相减等于数字1。)
‘0’-‘0’=0

int RoundMine(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';
}

5.2展开一片函数

用递归来实现
前提条件:①该坐标不是雷,②该坐标周围没有雷,③该坐标没有被排查过,④该坐标没有越界

void OpenUp(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
	int ret = RoundMine(mine, x, y);
	if (ret == 0)
	{
		show[x][y] = ' ';
		if(x-1>0&&y-1>0&&show[x-1][y-1]=='*')
			OpenUp(mine, show, ROW, COL, x-1, y-1);
		if(x-1>0&&show[x-1][y])
			OpenUp(mine, show, ROW, COL, x - 1, y);
		if(x - 1 >0&&y+1<=col&&show[x-1][y+1]=='*')
			OpenUp(mine, show, ROW, COL, x - 1, y+1);
		if(y-1>0&&show[x][y-1]=='*')
			OpenUp(mine, show, ROW, COL, x , y - 1);
		if(y+1<=col&&show[x][y+1]=='*')
			OpenUp(mine, show, ROW, COL, x, y + 1);
		if(x+1<=row&&y-1>0&&show[x+1][y-1]=='*')
			OpenUp(mine, show, ROW, COL, x+1, y-1);
		if(x+1<=row&&show[x+1][y]=='*')
			OpenUp(mine, show, ROW, COL, x + 1, y);
		if (x + 1 <=row && y + 1 <=col&&show[x+1][y+1]=='*')
			OpenUp(mine, show, ROW, COL, x + 1, y + 1);
	}
	else
	{
		show[x][y] = ret + '0';
	}
}

效果如下:

------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请输入要排查的坐标:>3 3
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * 1     1 * * * *
2 * 1     1 * * * *
3 1 1   1 2 * * * *
4       1 * * * * *
5     1 2 * * * * *
6     1 * * * * * *
7   1 2 * * * * * *
8   1 * * * * * * *
9   1 * * * * * * *
------扫雷游戏-----

5.3标记雷

void SignMine(char show[ROWS][COLS], int row, int col)
{
	int sign = 0;
	do
	{
		printf("请选择是否标记(1/0):>");
		scanf("%d", &sign);
		switch (sign)
		{
		case 1:
			printf("请输入要标记的坐标:>");
			int x = 0;
			int y = 0;
			scanf("%d %d", &x, &y);
			if (x >= 1 && x <= row && y >= 1 && y <= col)
			{
				if (show[x][y] == '*')
				{
					show[x][y] = '@';
					display_board(show, ROW, COL);
				}
				else if (show[x][y] == '@')
				{
                    show[x][y] = '*';//取消标记
					display_board(show, ROW, COL);
				}
					
				else
					printf("该坐标已被排查\n");
			}
			else
				printf("坐标非法\n");
			break;
		case 0:
			break;
		default:
			break;
		}
	} while (sign);
}

效果如下:

------扫雷游戏-----
请输入要排查的坐标:>2 7
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * 1   1 * 1
2 1 1   2 * 2
3       2 * 2 1 1 1
4       1 2 * * * *
5         1 * * * *
6 1 1 1   1 1 1 1 *
7 * * 2         1 1
8 * * 2   1 1 1
9 * * 1   1 * 1
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>1 1
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 @ 1   1 * 1
2 1 1   2 * 2
3       2 * 2 1 1 1
4       1 2 * * * *
5         1 * * * *
6 1 1 1   1 1 1 1 *
7 * * 2         1 1
8 * * 2   1 1 1
9 * * 1   1 * 1
------扫雷游戏-----
请选择是否标记(1/0):>

5.4判断胜利

遍历数组每个元素,如果没有被排查的和已经被标记的和等于雷的个数,则排雷成功。

int IsWin(char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int i = 0;
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*' || show[i][j] == '@')
				win++;
		}
	}
	return win;
}

6.完整代码

6.1 test.c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
	printf("************************\n");
	printf("*******  1.play  *******\n");
	printf("*******  0.exit  *******\n");
	printf("************************\n");
}
void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	init_board(mine, ROWS, COLS,'0');
	init_board(show, ROWS, COLS,'*');
	SetMine(mine, ROW, COL);
	display_board(mine, ROW, COL);
	display_board(show, ROW, COL);
	FindMine(mine, show, ROW, COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			system("cls");
			game();
			system("pause");
			system("cls");

			break;
		case 0:

			printf("退出游戏\n");
			system("pause");
			system("cls");

			break;
		default:
			printf("选择错误,请重新输入\n");
			system("pause");
			system("cls");
			break;
		}
	} while (input);
	return 0;
}

6.2 game.c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void init_board(char board[ROWS][COLS], int rows, int cols, int set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void display_board(char board[ROWS][COLS], int row, int col)
{
	printf("------扫雷游戏-----\n");
	int i = 0;
	int j = 0;
	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");
	}
	printf("------扫雷游戏-----\n");
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = 0;
	while (count < easy_count)
	{
		int x = 0;
		int y = 0;
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count++;
		}
	}
}
int RoundMine(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 OpenUp(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
	int ret = RoundMine(mine, x, y);
	if (ret == 0)
	{
		show[x][y] = ' ';
		if(x-1>0&&y-1>0&&show[x-1][y-1]=='*')
			OpenUp(mine, show, ROW, COL, x-1, y-1);
		if(x-1>0&&show[x-1][y])
			OpenUp(mine, show, ROW, COL, x - 1, y);
		if(x - 1 >0&&y+1<=col&&show[x-1][y+1]=='*')
			OpenUp(mine, show, ROW, COL, x - 1, y+1);
		if(y-1>0&&show[x][y-1]=='*')
			OpenUp(mine, show, ROW, COL, x , y - 1);
		if(y+1<=col&&show[x][y+1]=='*')
			OpenUp(mine, show, ROW, COL, x, y + 1);
		if(x+1<=row&&y-1>0&&show[x+1][y-1]=='*')
			OpenUp(mine, show, ROW, COL, x+1, y-1);
		if(x+1<=row&&show[x+1][y]=='*')
			OpenUp(mine, show, ROW, COL, x + 1, y);
		if (x + 1 <=row && y + 1 <=col&&show[x+1][y+1]=='*')
			OpenUp(mine, show, ROW, COL, x + 1, y + 1);
	}
	else
	{
		show[x][y] = ret + '0';
	}
}
void SignMine(char show[ROWS][COLS], int row, int col)
{
	int sign = 0;
	do
	{
		printf("请选择是否标记(1/0):>");
		scanf("%d", &sign);
		switch (sign)
		{
		case 1:
			printf("请输入要标记的坐标:>");
			int x = 0;
			int y = 0;
			scanf("%d %d", &x, &y);
			if (x >= 1 && x <= row && y >= 1 && y <= col)
			{
				if (show[x][y] == '*')
				{
					show[x][y] = '@';
					display_board(show, ROW, COL);
				}
				else if (show[x][y] == '@')
				{
                    show[x][y] = '*';//取消标记
					display_board(show, ROW, COL);
				}
					
				else
					printf("该坐标已被排查\n");
			}
			else
				printf("坐标非法\n");
			break;
		case 0:
			break;
		default:
			break;
		}
	} while (sign);
}
int IsWin(char show[ROWS][COLS], int row, int col)
{
	int win = 0;
	int i = 0;
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*' || show[i][j] == '@')
				win++;
		}
	}
	return win;
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	while (1)
	{
		int x = 0;
		int y = 0;
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (show[x][y] != '*')
			{
				printf("该坐标已被排查,请重新输入\n");
			}
			else
			{
				if (mine[x][y] == '1')
				{
					printf("很遗憾,你被炸死了\n");
					display_board(mine, ROW, COL);
					break;
				}
				else
				{
					OpenUp(mine, show, ROW, COL, x, y);
					display_board(show, ROW, COL);
					SignMine(show, ROW, COL);
					display_board(show, ROW, COL);
					int ret=IsWin(show, ROW, COL);
					if (ret == easy_count)
					{
						printf("恭喜你,排雷成功\n");
						display_board(mine, ROW, COL);
						break;
					}
				}
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
}

6.3 game.h

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define easy_count 10
void init_board(char board[ROWS][COLS], int rows, int cols,int set);
void display_board(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);

6.4展示结果

6.4.1成功

------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请输入要排查的坐标:>4 2
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 * * * * *
2     1 2 * * * * *
3     1 * * * * * *
4     1 * * * * * *
5     1 * * * * * *
6     2 * * * * * *
7     2 * * * * * *
8 1 2 3 * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 * * * * *
2     1 2 * * * * *
3     1 * * * * * *
4     1 * * * * * *
5     1 * * * * * *
6     2 * * * * * *
7     2 * * * * * *
8 1 2 3 * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请输入要排查的坐标:>6 8
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 * * * * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * * 1 1 1 1
8 1 2 3 * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 * * * * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * * 1 1 1 1
8 1 2 3 * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请输入要排查的坐标:>8 6
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 * * * * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>1 5
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ * * * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>1 6
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ * * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ * * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>1 7
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 * *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>1 8
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 *
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>1 9
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 *
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>2 9
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 * 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>3 4
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 * 1
5     1 * 2 1
6     2 * * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>6 4
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 * 1
5     1 * 2 1
6     2 @ * 1
7     2 * 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>1
请输入要标记的坐标:>7 4
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 * 1
5     1 * 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 * 1
5     1 * 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>4 4
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 * 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 * 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>5 4
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * *
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>8 9
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 * * * 1     1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 * * * 1     1 * *
------扫雷游戏-----
请输入要排查的坐标:>9 8
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 * * * 1     1 1 *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 * * * 1     1 1 *
------扫雷游戏-----
请输入要排查的坐标:>9 9
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 * * * 1     1 1 1
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 * * * 1     1 1 1
------扫雷游戏-----
请输入要排查的坐标:>9 1
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 1 * * 1     1 1 1
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1       1 @ @ 1 1 1
2     1 2 3 2 1 1 @
3     1 @ 1     1 1
4     1 1 1
5     1 2 2 1
6     2 @ * 1
7     2 @ 3 1 1 1 1
8 1 2 3 2 1   1 * 1
9 1 * * 1     1 1 1
------扫雷游戏-----
恭喜你,排雷成功
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 0 0 0 0 1 1 0 0 0
2 0 0 0 0 0 0 0 0 1
3 0 0 0 1 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 0 0 1 1 0 0 0 0
7 0 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 0 1 0
9 0 1 1 0 0 0 0 0 0
------扫雷游戏-----
请按任意键继续. . .


6.4.2失败

------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请输入要排查的坐标:>7 2
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * 1 * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * 1 * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
------扫雷游戏-----
请输入要排查的坐标:>6 7
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * 1 1 1 1
5 * * * * * 1
6 * * * * * 1
7 1 1 1 * 2 1
8     1 1 1   1 1 1
9             1 * *
------扫雷游戏-----
请选择是否标记(1/0):>0
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * 1 1 1 1
5 * * * * * 1
6 * * * * * 1
7 1 1 1 * 2 1
8     1 1 1   1 1 1
9             1 * *
------扫雷游戏-----
请输入要排查的坐标:>2 2
很遗憾,你被炸死了
------扫雷游戏-----
0 1 2 3 4 5 6 7 8 9
1 0 0 0 0 0 0 0 0 1
2 0 1 0 0 0 0 0 0 0
3 0 0 0 0 1 0 0 1 0
4 0 1 0 0 0 0 0 0 0
5 0 0 0 1 0 0 0 0 0
6 1 0 0 0 1 0 0 0 0
7 0 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 1 0
------扫雷游戏-----
请按任意键继续. . .

扫雷是一种经典的游戏,可以使用C语言来实现。以下是一个简单的扫雷程序的实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 10 // 行数 #define COLS 10 // 列数 #define MINES 10 // 雷的数量 void init_game(char board[][COLS], int rows, int cols); // 初始化游戏面板 void print_board(char board[][COLS], int rows, int cols); // 打印游戏面板 void set_mines(char board[][COLS], int rows, int cols, int mines); // 布雷 void get_neighbors(int row, int col, int rows, int cols, int neighbors[]); // 获取邻居格子 int count_mines(char board[][COLS], int row, int col, int rows, int cols); // 计算某个格子周围的雷数 void reveal(char board[][COLS], int row, int col, int rows, int cols); // 翻开某个格子 int game_over(char board[][COLS], int rows, int cols); // 判断游戏是否结束 int main() { char board[ROWS][COLS]; int row, col; srand((unsigned)time(NULL)); // 设置随机数种子 init_game(board, ROWS, COLS); // 初始化游戏面板 set_mines(board, ROWS, COLS, MINES); // 布雷 print_board(board, ROWS, COLS); // 打印游戏面板 while (1) { printf("请输入坐标(行 列): "); scanf("%d %d", &row, &col); if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { printf("输入错误,请重新输入\n"); continue; } if (board[row][col] == '*') { // 点到雷了 printf("游戏结束!你输了\n"); board[row][col] = 'X'; // 标记点到的雷 print_board(board, ROWS, COLS); break; } else { reveal(board, row, col, ROWS, COLS); // 翻开格子 print_board(board, ROWS, COLS); } if (game_over(board, ROWS, COLS)) { // 判断游戏是否结束 printf("游戏结束!你赢了\n"); break; } } return 0; } // 初始化游戏面板 void init_game(char board[][COLS], int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = '-'; } } } // 打印游戏面板 void print_board(char board[][COLS], int rows, int cols) { int i, j; printf(" "); for (j = 0; j < cols; j++) { printf(" %d", j); } printf("\n"); for (i = 0; i < rows; i++) { printf("%d ", i); for (j = 0; j < cols; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); } // 布雷 void set_mines(char board[][COLS], int rows, int cols, int mines) { int count = 0, i, j; while (count < mines) { i = rand() % rows; j = rand() % cols; if (board[i][j] == '-') { board[i][j] = '*'; count++; } } } // 获取邻居格子 void get_neighbors(int row, int col, int rows, int cols, int neighbors[]) { int i, j, k = 0; for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < rows && j >= 0 && j < cols && (i != row || j != col)) { neighbors[k++] = i * cols + j; } } } } // 计算某个格子周围的雷数 int count_mines(char board[][COLS], int row, int col, int rows, int cols) { int neighbors[8], count = 0, i; get_neighbors(row, col, rows, cols, neighbors); for (i = 0; i < 8; i++) { if (board[neighbors[i] / cols][neighbors[i] % cols] == '*') { count++; } } return count; } // 翻开某个格子 void reveal(char board[][COLS], int row, int col, int rows, int cols) { int count; if (board[row][col] != '-') { // 已经翻开了 return; } count = count_mines(board, row, col, rows, cols); if (count > 0) { // 周围有雷 board[row][col] = '0' + count; } else { // 周围没有雷 board[row][col] = ' '; reveal(board, row - 1, col, rows, cols); reveal(board, row + 1, col, rows, cols); reveal(board, row, col - 1, rows, cols); reveal(board, row, col + 1, rows, cols); } } // 判断游戏是否结束 int game_over(char board[][COLS], int rows, int cols) { int i, j, count = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { if (board[i][j] == '-' || board[i][j] == '*') { count++; } } } return count == MINES; } ``` 在这个示例程序中,我们使用二维字符数组来表示游戏面板。其中,'-'表示未翻开的格子,'*'表示雷,' '表示周围没有雷的格子,'1'-'8'表示周围有1-8个雷的格子,'X'表示点到的雷。我们使用了递归算法来实现翻开格子的功能,当某个格子周围没有雷时,我们会递归地翻开它周围的格子,直到所有周围有雷的格子都被标记为'1'-'8'。 这只是一个简单的示例程序,还有很多可以改进的地方。比如,可以添加计时器和计分系统,以及更加友好的界面等等。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值