Lenguaje C para lograr el barrido de minas: ¡la versión que florece en todas partes!

Tabla de contenido

1. ¿Cómo lograrlo?

En segundo lugar, la construcción del cuerpo principal del juego ()

Tres, ¡la atención se centra en ganar o perder!

        función de ganar

Cuarto, el código general


1. ¿Cómo lograrlo?

        El marco general del juego -> menú, cuerpo principal del juego -> ¿para qué se utiliza el cuerpo principal del juego? -> Dos matrices -> Una pantalla (lo que ve el jugador) y la otra se usa para almacenar datos (almacenamiento de minas y procesamiento de datos) -> Verifique si la elección del jugador es incorrecta o no -> Aviso -> El cuerpo principal necesita un bucle para realizar el menú y el juego La visualización del cuerpo principal -> se confirma el marco general.


void menu()//菜单
{
	printf("--------------------\n");
	printf("|******************|\n");
	printf("|***** 1.play *****|\n");
	printf("|***** 0.exit *****|\n");
	printf("|******************|\n");
	printf("--------------------\n");
}

void game()//游戏本体
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	Initboard(mine, ROWS, COLS,'0');
	Initboard(show, ROWS, COLS, '*');
	Getmine(mine, MINE);
	//Showboard(mine, ROW, COL);
	printf("--------------------\n");
	Showboard(show, ROW, COL);
	win(mine, show, ROW, COL);
}

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

	} while (input);
	return 0;
}

En segundo lugar, la construcción del cuerpo principal del juego ()

        Construya un marco general -> ¿Qué funciones se necesitan? -> Inicializar -> Plantar minas -> Verificar minas -> Obtener los datos del número de minas -> Determinar las condiciones ganadoras o perdedoras. Cada operación debe decidir si pisar una mina -> por lo que es mejor inicializar la matriz que almacena los datos de la mina como '1' y '0', '1' significa mía y '0' significa que no es mía. Generalmente confirmado

el código se muestra a continuación:


void Initboard(char board[ROWS][COLS],int rows,int cols,char a)//初始化数组为a
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = a;
		}
	}
}

void Showboard(char board[ROWS][COLS], int row, int col)//打印扫雷的游戏棋盘
{
	for (int i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n ———————————————————\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d|", i);
		for (int j = 1; j <= col; j++)
		{
			if(j!=col)
			printf("%c ", board[i][j]);
			else
				printf("%c|", board[i][j]);

		}
		printf("\n");
	}
	printf(" ———————————————————\n");
}

void Getmine(char board[ROWS][COLS], int mine)//种地雷,种的数量为mine
{
	int count = 0;
	while (count < mine)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count++;
		}
	}
}

int Countmine(char board[ROWS][COLS], int row, int col)//数周围的地雷数量
{
	return (board[row - 1][col] + board[row-1][col-1] + board[row][col-1] + board[row+1][col-1] + board[row+1][col] +
		board[row+1][col+1] + board[row][col+1] + board[row-1][col+1] - '0'*8 );
}

void flower(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int* count)//递归查询周围地雷实现地雷游戏的遍地开花
{
	if (row > 0 && row < ROWS && col>0 && col < COLS)//使数组不越距
	{
		if (show[row][col] == '*')//递归结束条件
		{
			if (Countmine(mine, row, col)  == 0)//周围都空遍地开花
			{
				show[row][col] = ' ';//使其显示为空格
				(*count)--;
				flower(mine, show, row - 1, col, count);
				flower(mine, show, row - 1, col - 1, count);
				flower(mine, show, row, col - 1, count);
				flower(mine, show, row + 1, col - 1, count);
				flower(mine, show, row + 1, col, count);
				flower(mine, show, row + 1, col + 1, count);
				flower(mine, show, row, col + 1, count);
				flower(mine, show, row - 1, col + 1, count);
			}
			else//周围不是全空则标出周围地雷数量
			{
				show[row][col] = Countmine(mine, row, col) + '0';
				(*count)--;
			}
		}
	}
	
}

void change(char mine[ROWS][COLS], int row, int col)//就是为了好看点^_^用来游戏结尾显示地雷
{
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= col; j++)
		{
			if (mine[i][j] == '1')
				mine[i][j] = '$';
		}
	}
}

void win(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//判断输赢
{
	int a = 0, b = 0;
	int move = 81 - MINE;
	while (move != 0)
	{
		printf("输入扫雷位置(空格隔开):");
		scanf("%d %d", &a, &b);
		if (a >= 1 && a <= row && b >= 1 && b <= col&&show[a][b]=='*')//使数组不越距并且防止选同样的位置
		{
			if (mine[a][b] == '1')
			{
				system("cls");//清屏为了好看
				change(mine, ROW, COL);
				printf("你被炸4了!\n");
				Showboard(mine, row, col);
				break;
			}
			else
			{
					flower(mine, show, a, b, &move);
					system("cls");
					Showboard(show, row, col);
			}
		}
		else
		{
			printf("输入错误!请重新输入! ");
		}
	}
	if (move == 0)
	{
		system("cls");
		printf("恭喜你!扫完了全部雷!\n");
		change(mine, ROW, COL);
		Showboard(mine, row, col);
	}

}

Tres, ¡la atención se centra en ganar o perder!

        ¿Cómo se cuenta como una victoria? La razón principal es que el jugador ha barrido todas las minas, por lo que se debe definir un movimiento variable para lograr el conteo. El número inicial de movimientos es 9X9, el número de minas, y el jugador mueve -1 hasta que es 0 y salta. fuera de la lupa. El punto clave es florecer en todas partes (las coordenadas seleccionadas incluyen un total de 8 espacios vacíos alrededor sin minas). Cada vez que encuentres uno, conviértelo en un espacio. Esta también es una acción de jugador y debes mover-1; mover- 1.

        Recorriendo una función recursiva

void flower(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int* count)//递归查询周围地雷实现地雷游戏的遍地开花
{
	if (row > 0 && row < ROWS && col>0 && col < COLS)//使数组不越距
	{
		if (show[row][col] == '*')//递归结束条件
		{
			if (Countmine(mine, row, col)  == 0)//周围都空遍地开花
			{
				show[row][col] = ' ';//使其显示为空格
				(*count)--;
				flower(mine, show, row - 1, col, count);
				flower(mine, show, row - 1, col - 1, count);
				flower(mine, show, row, col - 1, count);
				flower(mine, show, row + 1, col - 1, count);
				flower(mine, show, row + 1, col, count);
				flower(mine, show, row + 1, col + 1, count);
				flower(mine, show, row, col + 1, count);
				flower(mine, show, row - 1, col + 1, count);
			}
			else//周围不是全空则标出周围地雷数量
			{
				show[row][col] = Countmine(mine, row, col) + '0';
				(*count)--;
			}
		}
	}
	
}

        función de ganar

void win(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//判断输赢
{
	int a = 0, b = 0;
	int move = 81 - MINE;
	while (move != 0)
	{
		printf("输入扫雷位置(空格隔开):");
		scanf("%d %d", &a, &b);
		if (a >= 1 && a <= row && b >= 1 && b <= col&&show[a][b]=='*')//使数组不越距并且防止选同样的位置
		{
			if (mine[a][b] == '1')
			{
				system("cls");//清屏为了好看
				change(mine, ROW, COL);
				printf("你被炸4了!\n");
				Showboard(mine, row, col);
				break;
			}
			else
			{
					flower(mine, show, a, b, &move);
					system("cls");
					Showboard(show, row, col);
			}
		}
		else
		{
			printf("输入错误!请重新输入! ");
		}
	}
	if (move == 0)
	{
		system("cls");
		printf("恭喜你!扫完了全部雷!\n");
		change(mine, ROW, COL);
		Showboard(mine, row, col);
	}

}

Cuarto, el código general

juego.h

#pragma once

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

#define ROW 9
#define COL 9
#define MINE 10

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

void Initboard(char board[ROWS][COLS], int rows, int cols, char a);//初始化

void Showboard(char board[ROW][COL], int row, int col);//打印

void Getmine(char board[ROWS][COLS],int mine);//种地雷

void win(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//判赢

juego.c

#define _CRT_SECURE_NO_WARNINGS 01

#include"game.h"


void Initboard(char board[ROWS][COLS],int rows,int cols,char a)//初始化数组为a
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			board[i][j] = a;
		}
	}
}

void Showboard(char board[ROWS][COLS], int row, int col)//打印扫雷的游戏棋盘
{
	for (int i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n ———————————————————\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d|", i);
		for (int j = 1; j <= col; j++)
		{
			if(j!=col)
			printf("%c ", board[i][j]);
			else
				printf("%c|", board[i][j]);

		}
		printf("\n");
	}
	printf(" ———————————————————\n");
}

void Getmine(char board[ROWS][COLS], int mine)//种地雷,种的数量为mine
{
	int count = 0;
	while (count < mine)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count++;
		}
	}
}

int Countmine(char board[ROWS][COLS], int row, int col)//数周围的地雷数量
{
	return (board[row - 1][col] + board[row-1][col-1] + board[row][col-1] + board[row+1][col-1] + board[row+1][col] +
		board[row+1][col+1] + board[row][col+1] + board[row-1][col+1] - '0'*8 );
}

void flower(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int* count)//递归查询周围地雷实现地雷游戏的遍地开花
{
	if (row > 0 && row < ROWS && col>0 && col < COLS)//使数组不越距
	{
		if (show[row][col] == '*')//递归结束条件
		{
			if (Countmine(mine, row, col)  == 0)//周围都空遍地开花
			{
				show[row][col] = ' ';//使其显示为空格
				(*count)--;
				flower(mine, show, row - 1, col, count);
				flower(mine, show, row - 1, col - 1, count);
				flower(mine, show, row, col - 1, count);
				flower(mine, show, row + 1, col - 1, count);
				flower(mine, show, row + 1, col, count);
				flower(mine, show, row + 1, col + 1, count);
				flower(mine, show, row, col + 1, count);
				flower(mine, show, row - 1, col + 1, count);
			}
			else//周围不是全空则标出周围地雷数量
			{
				show[row][col] = Countmine(mine, row, col) + '0';
				(*count)--;
			}
		}
	}
	
}

void change(char mine[ROWS][COLS], int row, int col)//就是为了好看点^_^用来游戏结尾显示地雷
{
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= col; j++)
		{
			if (mine[i][j] == '1')
				mine[i][j] = '$';
		}
	}
}

void win(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)//判断输赢
{
	int a = 0, b = 0;
	int move = 81 - MINE;
	while (move != 0)
	{
		printf("输入扫雷位置(空格隔开):");
		scanf("%d %d", &a, &b);
		if (a >= 1 && a <= row && b >= 1 && b <= col&&show[a][b]=='*')//使数组不越距并且防止选同样的位置
		{
			if (mine[a][b] == '1')
			{
				system("cls");//清屏为了好看
				change(mine, ROW, COL);
				printf("你被炸4了!\n");
				Showboard(mine, row, col);
				break;
			}
			else
			{
					flower(mine, show, a, b, &move);
					system("cls");
					Showboard(show, row, col);
			}
		}
		else
		{
			printf("输入错误!请重新输入! ");
		}
	}
	if (move == 0)
	{
		system("cls");
		printf("恭喜你!扫完了全部雷!\n");
		change(mine, ROW, COL);
		Showboard(mine, row, col);
	}

}

texto.c

#define _CRT_SECURE_NO_WARNINGS 01

#include"game.h"

void menu()//菜单
{
	printf("--------------------\n");
	printf("|******************|\n");
	printf("|***** 1.play *****|\n");
	printf("|***** 0.exit *****|\n");
	printf("|******************|\n");
	printf("--------------------\n");
}

void game()//游戏本体
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	Initboard(mine, ROWS, COLS,'0');
	Initboard(show, ROWS, COLS, '*');
	Getmine(mine, MINE);
	//Showboard(mine, ROW, COL);
	printf("--------------------\n");
	Showboard(show, ROW, COL);
	win(mine, show, ROW, COL);
}

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

	} while (input);
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_64038246/article/details/130589915
Recomendado
Clasificación