扫雷增强版

 之前写过一个普通版的扫雷,最近编写了一个增强版主要又包括了以下两个新功能:

1. 为了保证游戏体验第一次扫雷不被炸死,如果踩到雷上,那颗雷更换位置

2. 扫雷的时候如果选中位置周围没有雷将选中位置进行扩展


#ifndef _GAME_H_
#define _GAME_H_

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

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

void Setmine(char arr[ROWS][COLS], int  row, int col);
void Inintmeun(char arr[ROWS][COLS], int row, int col, char set);
void Output(char arr[ROWS][COLS], int row, int col);
int  Clean(char arr[ROWS][COLS], char arr1[ROWS][COLS], int row, int col);
int num(char arr[ROWS][COLS], int x, int y);
void  Extend(char arr[ROWS][COLS], char arr1[ROWS][COLS], int x, int y);

#endif 
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
	printf("*****************************\n");
	printf("**1.play game      0.exit  **\n");
	printf("*****************************\n");
	printf("**    请通过键盘进行选择   **\n");
}



int main()
{
	int n = 0;
	menu();
	scanf("%d", &n);
	int i = 0;
	char menu[ROWS][COLS];
	char  game[ROWS][COLS];
	char set1 = '*';
	char set2 = '0';
	printf("   ");
	for (i = 1; i <= ROW; i++)
	{

		printf(" %d ", i);
	}
	printf("\n");
	while (n)
	{
		Inintmeun(menu, ROWS, COLS,set1);
		Inintmeun(game, ROWS, COLS, set2);
		Output(menu, ROW, COL);
		Output(game,ROW, COL);
		Setmine(game, ROW, COL);
		Output(game, ROW, COL);
		Clean(game,menu, ROWS, COLS);
		Output(menu, ROW, COL);
		n = 0;
	}
	system("pause");
}

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void Inintmeun(char arr[ROWS][COLS], int row, int col, char set)
{
	memset(arr, set, row*col*sizeof(set));
}
void Output(char arr[ROWS][COLS], int row, int col)
{
	int x = 1;
	int y = 1;
	printf("   ");
	int n = 1;
	for (n = 1; n <=9; n++)
	{
		printf(" %d ", n);
	}
	printf("\n");
	for (x = 1; x <= row; x++)
	{
		printf(" %d ", x);
		for (y = 1; y <= col; y++)
		{
			printf(" %c ", arr[x][y]);
		}
		printf(" %d", x);
		printf("\n");
	}
	printf("\n");
}

void Setmine(char arr[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	srand((unsigned)time(NULL));
	while (count < COUNT)
	{
		
		x = rand() % row + 1;
		y = rand() % col+ 1;
		if (arr[x][y] =='0')
		{
			arr[x][y] = '1';
			count++;
		}
	
	}

}
int num(char arr[ROWS][COLS],  int x ,int y)
{
	int count = 0;
	if (arr[x + 1][y] == '1')
		count++;
	if (arr[x -1][y] =='1')
		count++;
	if (arr[x + 1][y+1] == '1')
		count++;
	if (arr[x + 1][y-1] == '1')
		count++;
	if (arr[x -1][y+1] == '1')
		count++;
	if (arr[x - 1][y-1] == '1')
		count++;
	if (arr[x][y-1] == '1')
		count++;
	if (arr[x][y+1] == '1')
		count++;
	return count;
}
int  Clean(char arr[ROWS][COLS],char arr1[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;
	int sum = 0;
	printf("请输入想要清除的坐标");
	scanf("%d", &x);
	scanf("%d", &y);
	while(((x >= 1) &&( x <= 9)) && ((y >= 1) && (y <= 9)))
	{

		if (arr[x][y] == '1')
		{
			if (sum == 0)
			{
				arr[x][y] = '0';
				int m = 0;
				int n = 0;
				int count1 = 0;
				srand((unsigned)time(NULL));
				while (count <1)
				{

					m = rand() % row + 1;
					n = rand() % col + 1;
					if (arr[m][n] == '0')
					{
						arr[m][n] = '1';
						count++;
					}

				}
				sum++;
				count = num(arr, x, y);
				arr1[x][y] = count + '0';
				Extend(arr, arr1, x, y);
				Output(arr1, ROW, COL);
				

			}
			else
			{

				arr1[x][y] = '1';
				Output(arr1, ROW, COL);
				printf("被炸死了\n");
				return 0;
			}
		}

		else
		{
			sum++;
			count = num(arr, x, y);
			arr1[x][y] = ' ';
			Extend(arr, arr1, x, y);
			Output(arr1, ROW, COL);
			if (sum == ROW*COL - COUNT)
			{

				printf("扫雷成功\n");
				return 0;
			}

			printf("请输入想要清除的坐标");
			scanf("%d", &x);
			scanf("%d", &y);
		}
	
	}

		printf("坐标不合法\n");
		return 0;
	
}

void  Extend(char arr[ROWS][COLS], char arr1[ROWS][COLS], int x, int y)
{
	int i = -1;
	int j = -1;
	for (i = -1; i < 2; i++) //边界
	{
		for (j = -1; j < 2; j++)
		{
			if (i != 0 || j != 0) // 避免排到自己注意此处的逻辑关系
			{
				if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL) //x y坐标是否合法
				{
					if (arr1[x + i][y + j] == '*'&&arr[x + i][y + j] != '1')
					{

						int count = num(arr, x + i, y + j);
						if (count != 0)
						{
							arr1[x + i][y + j] = count + '0';
							
						}
						else
						{
							arr1[x + i][y + j] = ' ';
							
							Extend(arr,arr1, x + i, y + j);
						}

					}

				}
			}
		}
	}


}



//void  Extend(char arr[ROWS][COLS], char arr1[ROWS][COLS], int x, int y)
//{
//	int n = 0;
//	if ((x<1) || (y<1) || (x>9) || (y>9))
//		return;
//	n = num(arr, x , y);
//	if (n == 0)
//	{
//
//		if (arr[x - 1][y] == '0')
//		{
//			if (arr1[x - 1][y] != ' ')
//			{
//
//				arr1[x - 1][y] = ' ';
//
//			}
//
//
//			Extend(arr, arr1, x - 1, y);
//		}
//		if (arr[x + 1][y] == '0')
//		{
//			if (arr1[x + 1][y] != ' ')
//			{
//
//				arr1[x + 1][y] = ' ';
//
//			}
//
//
//			Extend(arr, arr1, x + 1, y);
//		}
//		if (arr[x][y + 1] == '0')
//		{
//			if (arr1[x][y + 1] != ' ')
//			{
//
//				arr1[x][y + 1] = ' ';
//
//			}
//
//			Extend(arr, arr1, x, y + 1);
//		}
//		if (arr[x - 1][y + 1] == '0')
//		{
//			if (arr1[x - 1][y + 1] != ' ')
//			{
//
//				arr1[x - 1][y + 1] = ' ';
//
//			}
//
//			Extend(arr, arr1, x - 1, y + 1);
//		}
//		if (arr[x + 1][y + 1] == '0')
//		{
//			if (arr1[x + 1][y + 1] != ' ')
//			{
//
//				arr1[x + 1][y + 1] = ' ';
//
//			}
//
//			Extend(arr, arr1, x + 1, y + 1);
//		}
//		if (arr[x][y - 1] == '0')
//		{
//			if (arr1[x][y - 1] != ' ')
//			{
//
//				arr1[x][y - 1] = ' ';
//
//			}
//
//			Extend(arr, arr1, x, y - 1);
//		}
//		if (arr[x + 1][y - 1] == '0')
//		{
//			if (arr1[x + 1][y - 1] != ' ')
//			{
//
//				arr1[x + 1][y - 1] = ' ';
//
//			}
//
//			Extend(arr, arr1, x + 1, y - 1);
//		}
//		if (arr[x - 1][y - 1] == '0')
//		{
//			if (arr1[x - 1][y + 1] != ' ')
//			{
//
//				arr1[x - 1][y + 1] = ' ';
//
//			}
//
//			Extend(arr, arr1, x - 1, y - 1);
//		}
//	}
//	else
//		arr1[x][y] = n + '0';
//}


//void  Extend(char arr[ROWS][COLS], char arr1[ROWS][COLS], int x, int y)
//{
//	int a = 0;
//	int b = 0;
//	int n = 0;
//	a = x-1;
//	b = y;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		
//			
//		n = num(arr, a , b);
//		if (n == 0)
//		{
//
//			if (arr1[a ][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a ][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		a = a - 1;
//	}
//	a = x+1;
//	b = y;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		a = a + 1;
//	}
//	a = x;
//	b = y+1;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		b = b + 1;
//	}
//	a = x-1;
//	b = y+1;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		a = a - 1;
//		b = b + 1;
//	}
//	a = x+1;
//	b = y+1;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		a = a + 1;
//		b = b + 1;
//	}
//	a = x;
//	b = y-1;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		b = b - 1;
//	}
//	a = x-1;
//	b = y-1;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		a = a - 1;
//		b = b - 1;
//	}
//	a = x+1;
//	b = y-1;
//	while (!((a<1) || (b<1) || (a>9) || (b>9)))
//	{
//		n = num(arr, a, b);
//		if (n == 0)
//		{
//
//			if (arr1[a][b] != ' ')
//			{
//				arr1[a][b] = ' ';
//			}
//			else
//				break;
//		}
//		else
//		{
//
//			arr1[a][b] = n + '0';
//			Extend(arr, arr1, a, b);
//			break;
//		}
//		a = a +1;
//		b = b - 1;
//	}
//
//}

这里最后几个是我最初使用的函数,一直提示Stack Overflow,我在这里依然留下来,让大家看一下两者的区别,如果以后遇到问题的时候更改函数能有一个方向

 

最后附上一张程序运行图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值