扫雷游戏练习!

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>
#include<Windows.h>
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
#define MINES 10
int menu()
{
	printf("*******************************************************************\n");
	printf("************************欢迎玩家进入扫雷游戏!*********************\n");
	printf("*******************************************************************\n");
	printf("***************************1、开始游戏*****************************\n");
	printf("*******************************************************************\n");
	printf("***************************2、结束游戏*****************************\n");
	printf("*******************************************************************\n");
	printf("请输入您的选择:");
	int choice;
	scanf("%d", &choice);
	return choice;
}
void init_mines(char mine_map[ROWS][COLS], char show_map[ROWS][COLS], int rows, int cols, int row, int col)
{
	int i, x, y;
	memset(mine_map, '0', rows*cols * sizeof(char));
	memset(show_map, ' ', rows*cols * sizeof(char));
	for (i = 0; i < MINES; i++)
	{
		while (1)
		{
			x = rand() % row + 1;
			y = rand() % col + 1;
			if (mine_map[x][y] == '0')
			{
				mine_map[x][y] = '1';
				break;
			}
		}
	}
}
void display(char mine_map[ROWS][COLS], int row, int col)
{
	int i, j;
	for (i = 1; i <= row; i++)
	{
		printf("%4d", i);
	}
	printf("\n");
	for (i = 1; i <= col; i++)
	{
		printf("%2d", i);
		for (j = 1; j <= col; j++)
		{
			printf(" %c  ", mine_map[i][j]);
		}
		printf("\n");
	}
}
char safe_blank_check(char mine_map[ROWS][COLS], char show_map[ROWS][COLS], int x, int y)
{
	int m, n;
	char count = '0';
	for (m = x - 1; m <= x + 1; m++)
	{
		for (n = y - 1; n <= y + 1; n++)
		{
			if (mine_map[m][n] == '1')
				count++;
		}
	}
	return count;
}

int check_winner(char mine_map[ROWS][COLS], char show_map[ROWS][COLS], int row, int col, int x, int y)
{
	int count = 0;
	if (mine_map[x][y] == '0')
		show_map[x][y] = safe_blank_check(mine_map, show_map, x, y);
	else return '*';
	for (x = 1; x <= row; x++)
	{
		for (y = 1; y <= col; y++)
		{
			if (show_map[x][y] == ' ')
				count++;
		}
	}
	if (count == MINES)
		return 'w';
	return 0;
}
void game()
{
	srand((unsigned)time(0));
	char mine_map[ROWS][COLS];
	char show_map[ROWS][COLS];
	init_mines(mine_map, show_map, ROWS, COLS, ROW, COL);
	display(mine_map, ROW, COL);
	display(show_map, ROW, COL);
	while (1)
	{
		int row = 0;
		int col = 0;
		printf("请输入row和col:");
		scanf("%d %d", &row, &col);
		if (row >= 1 && row <= ROW && col >= 1 && col <= COL)
		{
			char ret = check_winner(mine_map, show_map, ROW, COL, row, col);
			if (ret == 'w')
			{
				display(mine_map, ROW, COL);
				printf("祝贺你,获得胜利!\n");
				break;
			}
			else if (ret == '*')
			{
				display(mine_map, ROW, COL);
				printf("你输了!你太菜了!\n");
				system("pause");
				break;
			}
			else
			{
				display(show_map, ROW, COL);
				printf("\n");
			}
		}
		else printf("输入错误,请重新输入!\n");
	}
	system("cls");
}
int main()
{
	srand((unsigned int)time(0));
	while (1)
	{
		int choice = menu();
		if (choice == 1)
		{
			game();
		}
		else if (choice == 2)
		{
			printf("游戏结束\n");
			break;
		}
		else
		{
			printf("输入错误,请重新输入!\n");
		}
		system("cls");
	}
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/qq_43780350/article/details/86237188