c语言实现扫雷游戏源代码

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void meun()
{
	printf("******************************\n");
	printf("*******    1:play    *********\n");
	printf("*******    0:exit    *********\n");
	printf("******************************\n");
	printf("请输入:\n");
}

void game()
{
	char mine[ROWS][LEFS] = { 0 };
	char out[ROWS][LEFS] = { 0 };
	initialize(mine, ROWS, LEFS, '0');//初始化
	initialize(out, ROWS, LEFS, '#');
	//print(mine, ROW, LEF);
	print(out, ROW, LEF);
	Lay_mines(mine, ROW, LEF, Easy_Count);//埋雷
	//print(mine, ROW, LEF);
	Find_mines(mine, out, ROW, LEF, Easy_Count);//扫雷
}


void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		
		meun();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("输入格式错误,请重新输入!\n");
			break;
		}
	} while (input);


}




int main()
{
	test();
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void initialize(char mine[ROWS][LEFS], int row, int lef, int key)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < lef; j++)
		{
			mine[i][j] = key;
		}
	}
}

void print(char mine[ROWS][LEFS], int row, int lef)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	printf("-------------------\n");
		//printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d|", i);
		for (j = 1; j <= lef; j++)
		{
			printf("%c ", mine[i][j]);
		}
		printf("\n");
	}
}

void Lay_mines(char mine[ROWS][LEFS], int row, int lef, int count)
{
	int x = 0;
	int y = 0;
	int m = 0;
	while (m < count)
	{
		x = rand() % row + 1;
		y = rand() % lef + 1;
		if (x > 0 && x <= row && y > 0 && y <= lef && mine[x][y] != '1')
		{
			mine[x][y] = '1';
			m++;
		}
	}
}

int count_mines(char mine[ROWS][LEFS], int row, int lef, int x, int y)
{
	char minecopy[ROWS][LEFS] = { 0 };
	initialize(minecopy, ROWS, LEFS, '0');
	int i = 0;
	int j = 0;
	for (i = 0; i < ROWS; i++)
	{
		for (j = 0; j < LEFS; j++)
		{
			if (mine[i][j] == '$')
			{
				minecopy[i][j] = '0';
			}
			else
			{
				minecopy[i][j] = mine[i][j];
			}
		}
	}
	return minecopy[x - 1][y] +
				minecopy[x - 1][y - 1] +
				minecopy[x][y - 1] +
		        minecopy[x + 1][y - 1] +
		        minecopy[x + 1][y] +
		        minecopy[x + 1][y + 1] +
		        minecopy[x][y + 1] +
		        minecopy[x - 1][y + 1] - 8 * '0';

}

void count_plus(char mine[ROWS][LEFS], char out[ROWS][LEFS], int row, int lef, int x, int y, int ret)
{
	if (mine[x][y] != '$')
	{
		ret = count_mines(mine, ROW, LEF, x, y);
		out[x][y] = ret + '0';
		mine[x][y] = '$';
			if (ret == 0)
			{
				if (x == 1)
				{
					if (y == 1)
					{
						count_plus(mine, out, ROW, LEF, x + 1, y, ret);
						count_plus(mine, out, ROW, LEF, x + 1, y + 1, ret);
						count_plus(mine, out, ROW, LEF, x, y + 1, ret);
					}
					else if (y == lef)
					{
						count_plus(mine, out, ROW, LEF, x, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x + 1, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x + 1, y, ret);
					}
					else
					{
						count_plus(mine, out, ROW, LEF, x, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x + 1, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x + 1, y, ret);
						count_plus(mine, out, ROW, LEF, x + 1, y + 1, ret);
						count_plus(mine, out, ROW, LEF, x, y + 1, ret);

					}
				}
				else if (x == row)
				{
					if (y == 1)
					{
						count_plus(mine, out, ROW, LEF, x - 1, y, ret);
						count_plus(mine, out, ROW, LEF, x, y + 1, ret);
						count_plus(mine, out, ROW, LEF, x - 1, y + 1, ret);
					}
					else if (y == lef)
					{
						count_plus(mine, out, ROW, LEF, x - 1, y, ret);
						count_plus(mine, out, ROW, LEF, x - 1, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x, y - 1, ret);
					}
					else
					{
						count_plus(mine, out, ROW, LEF, x - 1, y, ret);
						count_plus(mine, out, ROW, LEF, x - 1, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x, y - 1, ret);
						count_plus(mine, out, ROW, LEF, x, y + 1, ret);
						count_plus(mine, out, ROW, LEF, x - 1, y + 1, ret);
					}
				}
				else if (y == 1)
				{
					count_plus(mine, out, ROW, LEF, x - 1, y, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y + 1, ret);
					count_plus(mine, out, ROW, LEF, x, y + 1, ret);
					count_plus(mine, out, ROW, LEF, x - 1, y + 1, ret);
				}
				else if (y == lef)
				{
					count_plus(mine, out, ROW, LEF, x - 1, y, ret);
					count_plus(mine, out, ROW, LEF, x - 1, y - 1, ret);
					count_plus(mine, out, ROW, LEF, x, y - 1, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y - 1, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y, ret);
				}
				else if (x > 1 && x < row && y > 1 && y < lef)
				{
					count_plus(mine, out, ROW, LEF, x - 1, y, ret);
					count_plus(mine, out, ROW, LEF, x - 1, y - 1, ret);
					count_plus(mine, out, ROW, LEF, x, y - 1, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y - 1, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y, ret);
					count_plus(mine, out, ROW, LEF, x + 1, y + 1, ret);
					count_plus(mine, out, ROW, LEF, x, y + 1, ret);
					count_plus(mine, out, ROW, LEF, x - 1, y + 1, ret);
				}
			}
	}
	
}


void Find_mines(char mine[ROWS][LEFS], char out[ROWS][LEFS], int row, int lef, int count)
{
	int x = 0;
	int y = 0;

	
	do
	{
		while (1)
		{
			printf("请输入坐标:\n");
			scanf("%d%d", &x, &y);
			if (x > 0 && x <= row && y > 0 && y <= lef)
			{
				break;
			}
			else
			{
				printf("输入格式错误,请重新输入!\n");
			}
		}
		if (mine[x][y] == '1')
		{
			printf("你踩雷噶了!!!\n");
			out[x][y] = '*';
			print(out, ROW, LEF);
			break;
		}
		else
		{
			int ret = 0;
			count_plus(mine, out, ROW, LEF, x, y, ret);
			print(out, ROW, LEF);
			int i = 0;
			int j = 0;
			count = 1;
			for (i = 1; i <= row; i++)
			{
				for (j = 1; j <= lef; j++)
				{
					if (out[i][j] == '#')
						count++;
				}
			}
		}
	} while (count != 11);
	if (count == 11)
	{
		printf("恭喜你赢了!!!!!\n");
		
	}
	
}
#pragma once

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

#define ROW 9
#define LEF 9
#define ROWS ROW+2
#define LEFS LEF+2
#define Easy_Count 10

void initialize(char mine[ROWS][LEFS], int row, int lef, int key);
void print(char mine[ROWS][LEFS], int row, int lef);
void Lay_mines(char mine[ROWS][LEFS], int row, int lef, int count);
void Find_mines(char mine[ROWS][LEFS], char out[ROWS][LEFS], int row, int lef, int count);
void count_plus(char mine[ROWS][LEFS], char out[ROWS][LEFS], int row, int lef, int x, int y, int ret);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值