扫雷小游戏C语言实现

main.c

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

void show_menu()
{
	printf("****************************\n");
	printf("***0.退出游戏  1.进入游戏****\n");
	printf("****************************\n");
}

void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		show_menu();
		printf("请输入指令>>\n");
		scanf_s("%d", &input);
		switch (input)
		{
		case 0:
			printf("已退出<<\n");
			break;
		case 1:
			printf("进入扫雷>>\n");
			game();
			break;
		default:
			printf("输入无效指令,请重新输入>>\n");
			break;
		}
	} while (input);

}

int main()
{
	test();
	return 0;
}

game.c

#include "game.h"


void game()
{
	char burymine[ROWS][COLS] = { 0 };
	char showmine[ROWS][COLS] = { 0 };
	mine_init(burymine, ROWS, COLS, '0');
	mine_init(showmine, ROWS, COLS, '*');
	//display_mine(burymine, ROW, COL);
	display_mine(showmine, ROW, COL);
	bury_mine(burymine, ROW, COL);
	//检测雷是否埋下
	//display_mine(burymine, ROW, COL);
	sweep_mine(burymine, showmine,ROW,COL);

}

void mine_init(char mine[ROWS][COLS], int rows, int cols,char ch)
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			mine[i][j] = ch;
		}
	}
}

//@brief 打印数组[1][1]至[9][9]
void display_mine(char mine[ROWS][COLS], int row, int col)
{
	for (int i = 0; i < row + 1 ; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i < row + 1; i++)
	{
		printf("%d ", i);
		for (int j = 1; j < col + 1; j++)
		{
			printf("%c ", mine[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void bury_mine(char burymine[ROWS][COLS], int row, int col)
{
	int minecount = MINECOUNT;
	while (minecount)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (burymine[x][y] == '0')
		{
			burymine[x][y] = '1';
			minecount--;
		}
	}
}

void sweep_mine(char burymine[ROWS][COLS], char showmine[ROWS][COLS], int row,int col)
{
	int x = 0, y = 0;
	while (1)
	{
		printf("请输入要扫雷的坐标>>\n");
		scanf_s("%d%d", &x, &y);
		if (x > 0 && x < row + 1 && y>0 && y < col + 1)
		{
			if (showmine[x][y] != '*')
			{
				printf("该点坐标已经扫过了,请重新输入坐标>>\n");
				printf("\n");
			}
			else
			{
				if (burymine[x][y] == '1')
				{
					printf("抱歉,你被炸死了,游戏结束<<\n");
					display_mine(burymine, ROW, COL);
					printf("\n");
					while (1)
					{
						printf("请按'1'键继续...\n");
						int temp = 0;
						scanf_s("%d", &temp);
						if (temp == 1)
						{
							system("cls");
							break;
						}
						else
						{
							printf("你进行了无效操作\n");
						}
					}
					break;

				}
				else
				{
					char ret = get_mine_count(burymine, x, y) + '0';
					showmine[x][y] = ret;
					display_mine(showmine, ROW, COL);
					int ret_count = 0;
					ret_count = is_win(showmine, row, col);
					if (ret_count == true)
					{
						printf("恭喜,你赢了!\n");
						printf("\n");
						while (1)
						{
							printf("请按'1'键继续...\n");
							int temp = 0;
							scanf_s("%d", &temp);
							if (temp == 1)
							{
								system("cls");
								break;
							}
							else
							{
								printf("你进行了无效操作\n");
							}
						}
						break;
					}
				}
			}

		}
		else
		{
			printf("输入坐标有误,请重新输入>>\n");
		}
	}

}

int get_mine_count(char burymine[ROWS][COLS], int x,int y)
{
	return (burymine[x - 1][y - 1] + burymine[x][y - 1] + burymine[x + 1][y - 1] + burymine[x + 1][y] + burymine[x + 1][y + 1] + burymine[x][y + 1] + burymine[x - 1][y + 1] + burymine[x - 1][y] - 8*'0');
}

bool is_win(char showmine[ROWS][COLS], int row, int col)
{
	int count = 0;
	for (int i = 1; i < row + 1; i++)
	{
		for (int j = 1; j < col + 1; j++)
		{
			if (showmine[i][j] != '*')
			{
				count++;
			}
		}
	}
	if (count == (row * col - MINECOUNT))
	{
		return true;
	}
	return false;
}

game.h

#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<stdbool.h>

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

void game();
void mine_init(char mine[ROWS][COLS], int rows, int cols, char ch);
void display_mine(char mine[ROWS][COLS], int row, int col);
void bury_mine(char burymine[ROWS][COLS], int row, int col);
void sweep_mine(char burymine[ROWS][COLS], char showmine[ROWS][COLS], int row, int col);
int get_mine_count(char mine[ROWS][COLS], int row, int col);
bool is_win(char showmine[ROWS][COLS], int row, int col);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值