扫雷小游戏升级版

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

#define ROW  9
#define COL  9

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


#define MINENUM 10  //初始10个雷

void InitBoard(char board[][COLS], int rows, int cols, char set);//初始化棋盘
void ShowBoard(char board[][COLS], int rows, int cols);//打印棋盘

void SetMine(char mine[][COLS], int row, int col);//设雷

char FindMine(char mineInfo[][COLS], char mine[][COLS], int row, int col);//统计所下坐标雷的个数

void menu()
{
	printf("*****************\n");
	printf("*****0.exit*******\n");
	printf("*****1.play******\n");
	printf("*****************\n");
}
//初始化棋盘
void InitBoard(char board[][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
//打印棋盘
void ShowBoard(char board[][COLS], int rows, int cols)
{
	int i = 0;
	int j = 0;
	printf("======================\n");
	for (i = 0; i < rows - 1; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i < rows - 1; i++)
	{
		printf("%d ", i);
		for (j = 1; j < cols - 1; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("======================\n");
}
//随机产生雷
void SetMine(char mine[][COLS], int row, int col)//9  9
{
	int count = MINENUM;
	int x = 0;
	int y = 0;
	while (count != 0)
	{
		x = rand() % row + 1;//[0-9)  [1,10)
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//得到雷的个数
static int GetMine(char mine[][COLS], int x, int y)
{
	//'1' - '0'  = 1
	// 1 + '0' = '1'

	//return mine[x-1][y-1]-'0'+
	//mine[x-1][y]-'0'+
	//mine[x-1][y+1]-'0'+
	//mine[x][y+1]-'0'+
	//mine[x+1][y+1]-'0'+
	//mine[x+1][y]-'0'+
	//mine[x+1][y-1]-'0'+
	//mine[x][y-1]-'0';
	return mine[x - 1][y - 1] +
		mine[x - 1][y] +
		mine[x - 1][y + 1] +
		mine[x][y + 1] +
		mine[x + 1][y + 1] +
		mine[x + 1][y] +
		mine[x + 1][y - 1] +
		mine[x][y - 1] - 8 * '0';
}
//判断扫雷成功,当只剩下10个*时则全是雷,意味着扫雷成功
char win(char mineInfo[][COLS],int row, int col)
{
	int i = 0, j = 0; int count = 0;
	for (i = 1; i <= 9; i++)
	{
		for (j = 1; j <= 9; j++)
		{
			if (mineInfo[i][j] == '*')
				count++;
		}
	}
	if (count == 10)
	{
		printf("扫雷成功");
		return 'A';
		
	}
}
//将输入的坐标周围展开
void zhankai(char mineInfo[][COLS], char mine[][COLS], int row, int col,int x,int y)
{
	int i, j;  int sum = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = y - 1; j <= y + 1; j++)
		{
			if (mine[i][j] != '1')
			{
				int mineNum = GetMine(mine, i, j);

				mineInfo[i][j] = mineNum + '0';

			}
		}
	}
}
//你要输入的地方以及雷的信息
char FindMine(char mineInfo[][COLS], char mine[][COLS], int row, int col)
{
	SetMine(mine, ROW, COL);
	int x = 0;
	int y = 0;
	int count = 0;
	//    
	//ShowBoard(mine, ROWS, COLS);
	while (1)
	{
		while (1)
		{
			printf("请输入你的坐标:");
			scanf("%d%d", &x, &y);//如果玩家输入棋盘之外或者输入已经下过的会重新输入
			if ((mineInfo[x][y] == '*') && (x >= 1 && x <= 9 && y >= 1 && y <= 9))
			{
				break;
			}
			else
				printf("该位置不合法或已下过\n");
		}

		if (count == 0)//第一次下没有雷不会被炸死//如果第一次下的位置有雷,则在产生雷的棋盘相应位置
			          //改为0,(1为有雷)再随机产生一个没有雷的位置替换成有雷
		{                             //如果你第一次还是触到了炸弹,只能说明你运气太好
			if (mine[x][y] == '1')
			{
				mine[x][y] = '0';
				count++;
				
				while (1)
				{
					int m, n;
					m = rand() % row + 1;//[0-9)  [1,10)
					n= rand() % col + 1;
					if (mine[m][n] == '0')
					{

						mine[m][n] = '1';
						break;
					}
				}
			}
		}

		if (mine[x][y] == '1')
		{
			printf("你被炸死了\n");
			break;
		}
		zhankai(mineInfo, mine, row, col, x, y);
		ShowBoard(mineInfo, ROWS, COLS);
		
		char ch = win(mineInfo, row, col);
		if (ch == 'A')
		{
			break;
		}
		
	}
}
	


void game()
{
	char ch = 0;
	char mineInfo[ROWS][COLS];//*
	char mine[ROWS][COLS];//'0'

	InitBoard(mineInfo, ROWS, COLS, '*');
	InitBoard(mine, ROWS, COLS, '0');

	ShowBoard(mineInfo, ROWS, COLS);
	//ShowBoard(mine, ROWS, COLS);
	FindMine(mineInfo, mine, ROW, COL);


}
int main()
{
	
	int input = 0;
	srand((unsigned int) time (NULL) );
	do
	{
		menu();
		printf("请输入你的操作:");
		scanf("%d",&input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("游戏退出\n");
			break;
		default:
			break;
		}
	}while(input);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值