扫雷游戏练习!

本文将引导你逐步学习如何开发经典的扫雷游戏。从游戏逻辑到界面设计,通过编程实践提升你的逻辑思维和编程技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值