第二个小游戏,扫雷

game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

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

void InitBoard(char board[ROWS][COLS], int row, int col, char ret);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
char GetMineCount(char mine[ROWS][COLS], int x, int y);
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS]);
void RmoveMine(char mine[ROWS][COLS], int x, int y);
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
#define EASY_COUNT 10
game.c
#include"game.h"
void InitMine(char mine[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	/*char set;
	scanf("%c",&set);*/
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			mine[i][j] = '0';
		}
	}
}
void InitShow(char show[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			show[i][j] = '*';
		}
	}
}
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int x = 0,y=0;
	for (x = 0; x < rows; x++)
	{
		for (y = 0; y < cols; y++)
		{
			board[x][y] = ret;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0, j = 0;
	printf("  ");
	for (i = 0; i < row - 1; i++)
	{
		printf(" %d", i);
	}
	printf("\n");
	for (i = 0; i < row - 1; i++)
	{
		printf("%2d ", i);
		for (j = 0; j < col - 1; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int minecount = EASY_COUNT;
	srand((unsigned)time(NULL));
	while (minecount)
	{
		x = rand() % (ROW)+1;
		y = rand() % (COL)+1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			minecount--;
		}
	}
}
char GetMineCount(char mine[ROWS][COLS], int x, int y)  //统计周围雷的个数函数  
{
	int minecount = 0;
	if (mine[x - 1][y] == '1')
		minecount++;
	if (mine[x - 1][y + 1] == '1')
		minecount++;
	if (mine[x - 1][y + 1] == '1')
		minecount++;
	if (mine[x][y + 1] == '1')
		minecount++;
	if (mine[x + 1][y + 1] == '1')
		minecount++;
	if (mine[x + 1][y] == '1')
		minecount++;
	if (mine[x + 1][y - 1] == '1')
		minecount++;
	if (mine[x][y - 1] == '1')
		minecount++;
	if (mine[x - 1][y - 1] == '1')
		minecount++;
	return minecount;
}	
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS]) //排雷函数   
{
	int x = 0;
	int y = 0;
	int clear = 0;  //已经排雷个数  
	int minecount = 0;    //周围雷数  
	while (clear < (ROW)*(COL)-EASY_COUNT)
	{
		printf("开始排雷,请输入坐标:\n");
		scanf("%d%d", &x, &y);
		if (mine[x][y] == '1')
		{
			if (clear == 0) //第一次踩雷移走  
			{
				RmoveMine(mine, x, y);
				DisplayBoard(mine, ROWS, COLS);
				printf("\n");
				minecount = GetMineCount(mine, x, y);
				if (minecount == 0)
				{
					show[x][y] = ' ';
					clear++;
					Expand(mine, show, x, y);
					DisplayBoard(show, ROWS, COLS);
				}
				else
				{
					show[x][y] = minecount + '0';
					DisplayBoard(show, ROWS, COLS);
				}
			}
			else
			{
				printf("很遗憾,你被炸死了!\n");
				DisplayBoard(mine, ROWS, COLS);
				break;
			}
		}
		else if (mine[x][y] != '1')
		{
			minecount = GetMineCount(mine, x, y);
			if (minecount == 0)
			{
				show[x][y] = ' ';
			}
			else
			{
				show[x][y] = minecount + '0';
			}
			clear++;
			Expand(mine, show, x, y);
			DisplayBoard(show, ROWS, COLS);
		}
		if (clear == (ROW)*(COL)-EASY_COUNT)
		{
			printf("扫雷成功\n");
		}
	}
}
void RmoveMine(char mine[ROWS][COLS], int x, int y)//第一次踩雷 移走  
{
	mine[x][y] = '0';
	GetMineCount(mine, x, y);
	while (1)
	{
		int x1 = rand() % (ROW)+1;
		int y1 = rand() % (COL)+1;
		if (mine[x1][y1] != '1' && ((x1 != x) && (y1 != y)))
		{
			mine[x1][y1] = '1';
			break;
		}
	}
}
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//展开函数  
{
	if ((x >= 1) && (y >= 1) && (x <= ROW) && (y <= COL))
	{
		if (GetMineCount(mine, x, y) == 0)  //周围没有雷  
		{
			show[x][y] = ' ';    //置为空白  
			if (show[x - 1][y - 1] == '*')
			{
				Expand(mine, show, x - 1, y - 1);
			}
			if (show[x - 1][y] == '*')
			{
				Expand(mine, show, x - 1, y);
			}
			if (show[x - 1][y + 1] == '*')
			{
				Expand(mine, show, x - 1, y + 1);
			}
			if (show[x][y + 1] == '*')
			{
				Expand(mine, show, x, y + 1);
			}
			if (show[x + 1][y + 1] == '*')
			{
				Expand(mine, show, x + 1, y + 1);
			}
			if (show[x + 1][y] == '*')
			{
				Expand(mine, show, x + 1, y);
			}
			if (show[x + 1][y - 1] == '*')
			{
				Expand(mine, show, x + 1, y - 1);
			}
			if (show[x][y - 1] == '*')
			{
				Expand(mine, show, x, y - 1);
			}
		}
	}
}
text.c
#include"game.h"
void menu()
{
	printf("        《扫雷游戏》        \n");
	printf("       1 . play game        \n");
	printf("       0 . exit game        \n");
	printf("请选择\n");
}
void game()
{
	int x = 0;
	int y = 0;
	int count = 0;
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	InitBoard(show, ROWS, COLS, '*');
	DisplayBoard(show, ROWS, COLS);
	printf("\n");
	InitBoard(mine, ROWS, COLS, '0');
	/*DisplayBoard(mine,ROWS,COLS);*/
	SetMine(mine, ROW, COL);
	/*DisplayBoard(mine,ROWS,COLS);*/
	printf("\n");
	SweepMine(mine ,show);
}
int main()
{
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入有误,请重新输入\n");
			break;
		}
	} while (input);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值