C语言扫雷的实现3(源码分享)

本文介绍了使用C语言实现的扫雷游戏,包括菜单设计、游戏流程(初始化、设置雷、计算雷数、找雷)、以及核心函数的详细代码。作者分享了源码并鼓励读者练习和提升编程技能。
摘要由CSDN通过智能技术生成

1.往期作品

c语言扫雷的实现-CSDN博客

C语言扫雷的实现2(优化功能)-CSDN博客

2.作者祝福

各位朋友元旦快乐!转眼间我们又度过了新的一年,希望大家在新的一年心想事成!最近有朋友私信我能不能把我的扫雷源码分享出来,我很高兴朋友们在背后默默支持我,因此我很乐意将源码分享给你们,希望大家多多练习,写出更好的代码!

3.代码展现

1.test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void menu()
{
    printf("****************************\n");
    printf("**    0.exit  1.play      **\n");
    printf("****************************\n");
    printf("please choose:\n");
}

void game()
{
    srand((unsigned)time(NULL));
    char mine[Rows][Cols] ;
    char show[Rows][Cols] ;
    Initboard(mine,Rows,Cols,'0');
    Initboard(show,Rows,Cols,'*');
    Setmine(mine);
#ifdef Debug
    Showboard(mine,Row, Col);//测试结果是否正确
#endif
    Findmine(mine,show);
}

int main()
{
    int input = 0;
    do
    {
        menu();
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("exit successfully\n");
            break;
        default:
            printf("invalid input,please input again\n");
            break;
        }
    } while (input);
    return 0;
}

2.game.h

#define _CRT_SECURE_NO_WARNINGS 1
#define Row 10
#define Col 10
#define Rows Row+2
#define Cols Col+2
#define Mine_number 2
//#define Debug 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void Initboard(char board[Rows][Cols], int row, int col, char flag);//初始化数组

void Showboard(char board[Rows][Cols], int row, int col);//在屏幕上显示

void Setmine(char mine[Rows][Cols]);//设置雷

void Caculate(char mine[Rows][Cols], char show[Rows][Cols], int x, int y);//递归计算雷的个数

int Getminenum(char mine[Rows][Cols],int x, int y);//计算周围的雷的个数

int Check_win(char mine[Rows][Cols], char show[Rows][Cols], int row, int col);//判断是否胜利

void Findmine(char mine[Rows][Cols], char show[Rows][Cols]);//找雷

3.game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void Initboard(char board[Rows][Cols], int row, int col, char flag)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            board[i][j] = flag;
        }
    }
}

void Showboard(char board[Rows][Cols], int row, int col)
{
    int i = 1,j = 1;
    printf("\n");
    for ( i = 1; i <= row; i++)
    {
        printf("%-2d ", i);
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf("---");
    }
    printf("\n");
    for ( i = 1; i <= row; i++)
    {
        for ( j = 1; j <= col; j++)
        {
            printf("%c  ", board[i][j]);
        }
        printf("| %d\n", i);
    }
}

void Setmine(char mine[Rows][Cols])
{
    int count = Mine_number;
    while (count)
    {
        int x = rand() % Row + 1;
        int y = rand() % Col + 1;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}

void Caculate(char mine[Rows][Cols],char show[Rows][Cols],int x,int y)
{
    int num = Getminenum(mine, x, y);
    show[x][y] = '0' + num;
    if (num == 0)
    {
        if (x >= 1 && x <= Row && y >= 1 && y <= Col)
        {
            if (show[x - 1][y] == '*')
            {
                Caculate(mine, show, x - 1, y);
            }
            if (show[x][y - 1] == '*')
            {
                Caculate(mine, show, x, y - 1);
            }
            if (show[x][y + 1] == '*')
            {
                Caculate(mine, show, x, y + 1);
            }
            if (show[x + 1][y] == '*')
            {
                Caculate(mine, show, x + 1, y);
            }
        }
    }
}

int Getminenum(char mine[Rows][Cols], int x, int y)
{
    return mine[x - 1][y - 1] +
        mine[x - 1][y] +
        mine[x - 1][y + 1] +
        mine[x][y - 1] +
        mine[x][y + 1] +
        mine[x + 1][y - 1] +
        mine[x + 1][y] +
        mine[x + 1][y + 1] - 8 * '0';    
}

int Check_win(char mine[Rows][Cols],char show[Rows][Cols],int row,int col)
{
    int count = 0;
    for (int i = 1; i <= row; i++)
    {
        for (int j = 1; j <= col; j++)
        {
            if (show[i][j] >= '0' && show[i][j] <= '8')
            {
                count++;
            }
        }
    }
    return count;
}

void Findmine(char mine[Rows][Cols], char show[Rows][Cols])
{
    int win = Row * Col - Mine_number;
    Showboard(show, Row, Col);
    while (1)
    {
        printf("please input x and y\n");
        int x, y;
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= Row && y >= 1 && y <= Col)
        {
            if (mine[x][y] == '1')
            {
                printf("game over\n");
                Showboard(mine, Row, Col);
                break;
            }
            else
            {
                Caculate(mine, show, x, y);
                Showboard(show, Row, Col);
                int count = Check_win(mine, show, Row, Col);
                if (count == win)
                {
                    break;
                }
            }
        }
        else
        {
            printf("please input correct x and y\n");
        }
    }
    if (Check_win(mine, show, Row, Col) == win)
    {
        printf("you win\n");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值