C语言实现一个简易的扫雷2

//game.h
#ifndef _GAME_H_
#define _GAME_H_
#define CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

#define rows 11
#define cols 11
#define Count 10

void Init_board(char mine[rows][cols], char show[rows][cols]);//初始化棋盘
void Set_mine(char mine[rows][cols]);//设置雷的位置
void Display(char show[rows][cols]);//打印棋盘
int Sweep(char mine[rows][cols], char show[rows][cols]);//开始扫雷
int Get_num(char mine[rows][cols], int x, int y);//计算雷个数

#endif
//game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

//初始化棋盘
void Init_board(char mine[rows][cols], char show[rows][cols])
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows - 1; i++)
    {
        for (j = 0; j < cols - 1; j++)
        {
            mine[i][j] = '0';
            show[i][j] = '*';
        }
    }
}
//设置雷位置
void Set_mine(char mine[rows][cols])
{
    int count = Count;
    int x = 0;
    int y = 0;
    while (count)
    {
        x = rand() % 9 + 1;
        y = rand() % 9 + 1;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}

//打印界面
void Display(char show[rows][cols])
{
    int i = 0;
    int j = 0;
    printf("   ");
    for (i = 1; i < cols - 1; i++)
    {
        printf("%d  ", i);
    }
    printf("\n\n");
    for (i = 1; i < rows - 1; i++)
    {
        printf("%d  ", i);
        for (j = 1; j < rows - 1; j++)
        {
            printf("%c  ", show[i][j]);
        }
        printf("\n\n");
    }
}

//计算雷个数
int Get_num(char mine[rows][cols], int x, int y)
{
    int count = 0;
    if (mine[x - 1][y - 1] == '1')//左上方
    {
        count++;
    }
    if (mine[x][y - 1] == '1')//左方
    {
        count++;
    }
    if (mine[x + 1][y - 1] == '1')//左下方
    {
        count++;
    }
    if (mine[x - 1][y] == '1')//上方
    {
        count++;
    }
    if (mine[x + 1][y] == '1')//下方
    {
        count++;
    }
    if (mine[x - 1][y + 1] == '1')//右上方
    {
        count++;
    }
    if (mine[x][y + 1] == '1')//右方
    {
        count++;
    }
    if (mine[x + 1][y + 1] == '1')//右下方
    {
        count++;
    }
    return count;
}

//开始扫雷
int Sweep(char mine[rows][cols], char show[rows][cols])
{
    int count = 0;
    int x = 0;
    int y = 0;
    static int fflag = 0;
    int a1 = 0;
    int b1 = 0;
    while (count != ((rows - 2) * (cols - 2) - Count))
    {
        printf("请输入坐标(用空格隔开):\n");
        scanf("%d%d", &x, &y);
        if (mine[x][y] == '1' && fflag == 0)//首次点中雷,不被炸死,挪动雷位置
        {
            mine[x][y] = '0';
            a1 = x;
            b1 = y;
            while (1)
            {
                x = rand() % 9 + 1;
                y = rand() % 9 + 1;
                if (mine[x][y] == '0' && x != a1 && y != b1)
                {
                    mine[x][y] = '1';
                    Display(mine);//显示新的雷的位置
                    break;
                }
            }
            fflag = 1;
        }
        else if (mine[x][y] == '1' && fflag > 0)
        {
            printf("你已踩雷,被炸死了!\n");
            return 0;
        }
        else if (mine[x][y] == '0')
        {
            count++;
            count = Get_num(mine, x, y);
            show[x][y] = count + '0';
            //空白展开
            count = Get_num(mine, x - 1, y - 1);
            show[x - 1][y - 1] = count + '0';
            count = Get_num(mine, x, y - 1);
            show[x][y - 1] = count + '0';
            count = Get_num(mine, x + 1, y - 1);
            show[x + 1][y - 1] = count + '0';
            count = Get_num(mine, x -1 , y);
            show[x - 1][y] = count + '0';
            count = Get_num(mine, x + 1, y );
            show[x + 1][y] = count + '0';
            count = Get_num(mine, x - 1, y + 1);
            show[x - 1][y + 1] = count + '0';
            count = Get_num(mine, x, y + 1);
            show[x][y + 1] = count + '0';
            count = Get_num(mine, x + 1, y + 1);
            show[x + 1][y + 1] = count + '0';
            Display(show);
            fflag = 1;
        }
    }
    printf("恭喜你赢了!\n");
    Display(mine);
    return 0;
} 

//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//菜单
void menu()
{
    printf("***************************\n");
    printf("******** 扫雷小游戏 *******\n");
    printf("********* 1.play **********\n");
    printf("********* 0.exit **********\n");
    printf("***************************\n");
}

//游戏主体
void game()
{
    char mine[rows][cols];
    char show[rows][cols];
    Init_board(mine, show);
    Set_mine(mine);
    //Display(mine);//显示雷的位置
    printf("----------------------------\n");
    Display(show);
    Sweep(mine, show);
}

//执行测试
void test()
{
    int input = 0;
    do{
        menu();
        printf("请选择:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("进入游戏:\n");
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        defalt:
            printf("输入错误,请重新选择");
            break;
        }
    } while (input);
}

int main()
{
    srand((unsigned)time(NULL));//生成随机数
    test();
    system("pause");
    return 0;
}

运行显示:
打印雷位置和初始化棋盘
这里写图片描述
当首次踩雷,不显示炸开,将雷的位置移开,并显示移开后的雷区
这里写图片描述
显示踩雷
这里写图片描述

猜你喜欢

转载自blog.csdn.net/zhang21722668/article/details/80031757