C语言实现扫雷游戏(可标记雷)有三种难度等级!选中空白可开辟一片空间,高仿扫雷

源文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void menu()
{
    printf("***************************\n");
    printf("********  1.play  *********\n");
    printf("********  0.exit  *********\n");
    printf("***************************\n");
}
void menu1()
{
    printf("*** 1.简单 2.中等 3.困难 ***\n");
}
void game()
{
    //雷的信息存储
    //1.布置好的雷的信息
    char mine[ROWS][COLS] = { 0 };//11*11
    //2.排查的雷的信息
    char show[ROWS][COLS] = { 0 };
    //初始化
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');
    //打印棋盘
    DisplayBoard(show,ROW,COL);
    //布置雷
    SetMine(mine, ROW, COL);
    //DisplayBoard(mine, ROW, COL);     //显示排雷的结果(答案)
    //扫雷
    FindMine(mine,show,ROW,COL);

}
extern int win;
int ROW = 9;
int COL = 9;
int EASY_COUNT = 10;
int surplus = 0; //剩余雷的个数
void text()
{
    int input = 0;
    int input1 = 0;
    srand((unsigned int)time(NULL));
    do{
        menu();
        win = 0;
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            menu1();    //显示选择模式信息
            printf("请选择:>");
            scanf("%d", &input1);
            switch (input1)
            {
                case 1:
                    ROW = 9;
                    COL = 9;
                    EASY_COUNT = 10;
                    surplus = 10;
                    break;
                case 2:
                    ROW = 16;
                    COL = 16;
                    EASY_COUNT = 40;
                    surplus = 40;
                    break;
                case 3:
                    ROW = 16;
                    COL = 30;
                    EASY_COUNT = 99;
                    surplus = 99;
                    break;
            }
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("选择错误,重新选择!\n");
        }
    } while (input);
}
int main()
{
    text();
    return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    int i = 0, j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = set;
        }
    }
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    printf("剩余雷的个数:%d\n",surplus);
    int i = 0, j = 0;
    //打印列号
    for (i = 0; i <= col; i++)
    {
        printf("%3d", i);
    }
    printf("\n");
    for (i = 1; i <= row; i++)
    {
        printf("%3d ", i);
        for (j = 1; j <= col; j++)
        {
            printf(" %c ", board[i][j]);
        }
        printf("\n");
    }
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
    int count = EASY_COUNT;
    while (count)
    {
        int x = rand() % (row - 1) + 1;//1-row
        int y = rand() % (col - 1) + 1;//1-col
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
    }
}

int get_mine_count(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';
}
//展开的功能,递归
//1.坐标不是雷 2.该坐标周围也没有雷
//展开:他周围的坐标是否满足以上两个条件
int win = 0;
void disp(char mine[ROWS][COLS], char show[ROWS][COLS], int x1, int y1)
{
    int arr[8][2] = { { x1 - 1, y1 - 1 }, { x1 - 1, y1 }, { x1 - 1, y1 + 1 }, { x1, y1 - 1 },
    { x1, y1 + 1 }, { x1 + 1, y1 - 1 }, { x1 + 1, y1 }, { x1 + 1, y1 + 1 } };
    int i = 0;
    //遍历四周
    for (i = 0; i < 8; i++)
    {
        int x1 = arr[i][0], y1 = arr[i][1]; //一个棋子
        //如果它不是雷且没有被遍历过,那么就显示周围雷的个数
        if (x1 >= 1 && x1 <= ROW && y1 >= 1 && y1 <= COL && mine[x1][y1] == '0' && show[x1][y1] == '*')
        {
            int count = get_mine_count(mine, x1, y1);
            show[x1][y1] = count + '0'; //显示周围雷的个数,已遍历
            win++;
            //且如果他的周围没有雷,那么就展开
            if (mine[x1 - 1][y1 - 1] == '0' && mine[x1 - 1][y1] == '0' && mine[x1 - 1][y1 + 1] == '0' &&
                mine[x1][y1 - 1] == '0' && mine[x1][y1 + 1] == '0' &&
                mine[x1 + 1][y1 - 1] == '0' && mine[x1 + 1][y1] == '0' && mine[x1 + 1][y1 + 1] == '0')  //周围没有雷 
            {
                show[x1][y1] = ' '; //显示空格
                disp(mine, show, x1, y1);
            }
        }
    }
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int x = 0, y = 0,input=0;
    while (win < row*col-EASY_COUNT)
    {
        printf("请输入排查雷的坐标、排雷0 或 设置雷的标注1:>");
        scanf("%d%d%d", &x, &y,&input);
        if (x >= 1 && x <= row && y >= 1 && y <= col )      //坐标合法
        {
            if (input == 0)  //排雷
            {
                if (mine[x][y] == '1')  //踩雷
                {
                    printf("很遗憾你被炸死了\n");
                    DisplayBoard(mine, row, col);
                    break;
                }
                else  //非雷
                {
                    //计算x,y坐标周围有几个雷
                    int count = get_mine_count(mine, x, y);
                    if (count == 0)
                    {
                        show[x][y] = ' ';
                    }
                    else
                    {
                        show[x][y] = count + '0';
                    }
                    win++;
                    if (mine[x - 1][y - 1] == '0' && mine[x - 1][y] == '0' && mine[x - 1][y + 1] == '0' &&
                        mine[x][y - 1] == '0' && mine[x][y + 1] == '0' &&
                        mine[x + 1][y - 1] == '0' && mine[x + 1][y] == '0' && mine[x + 1][y + 1] == '0')
                    {
                        disp(mine, show, x, y);
                    }
                }
            }
            else if (input == 1)
            {
                if (show[x][y] == '*')
                {
                    show[x][y] = '!';
                    surplus--;
                }
                else if (show[x][y] == '!')
                {
                    show[x][y] = '*';
                    surplus++;
                }
            }
            DisplayBoard(show, row, col);
        }
        else
        {
            printf("输入坐标非法,请重新输入\n");
        }
    }
    if (win == row*col - EASY_COUNT)
    {
        printf("恭喜你,排雷成功\n");
        DisplayBoard(mine, row, col);
    }
}

game.h

extern int  ROW;
extern int COL;
extern int EASY_COUNT;//雷的个数
extern int surplus;//剩余雷的个数
#define ROWS 18
#define COLS 32

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

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

猜你喜欢

转载自blog.51cto.com/15080913/2673448