C语言:简单的无界面化操作扫雷游戏

扫雷是生活中接触比较多的一块小游戏,那么我们该如何通过C语言来实现呢?
姐先来的代码能够实现基本的扫雷功能,只是缺少了界面化,鼠标操作等,日后有待改善,不过基本功能还是可以实现的,比如:第一步绝对不会踩到地雷的哦!
接下来是代码展示:

直接下颚在一个文件里,虽然功能可以实现,却并不是一个好习惯。可以借鉴书籍《高质量程序设计指南C++/C语言》

#define  _CRT_SECURE_NO_WARNINGS 10
#include
#include
#include
#define ROWS 12
#define COLS 12
#define NUM 15

enum op{
    EXIT,
    PLAY
};

void meau()
{
    printf("****************************************************\n");
    printf("***************      1、play        ****************\n");
    printf("***************      0、exit        ****************\n");
    printf("****************************************************\n");
}

void init_board(char board[ROWS][COLS])
{
    int i = 0;
    int j = 0;
    char x = ' ';
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            board[i][j] = x;
        }
    }
}

void init_mine(char mine[ROWS][COLS])
{
    int i = 0;
    int j = 0;
    char x = ' ';
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            mine[i][j] = x;
        }
    }
}

void get_mine(char mine[ROWS][COLS])
{
    int x = 0;
    int y = 0;
    int mine_num = NUM;
    while (mine_num)
    {
    loop:
        x = rand() % (ROWS - 3 + 1) + 1;
        y = rand() % (COLS - 3 + 1) + 1;
        if (mine[x][y] != '*')
        {
            mine[x][y] = '*';
        }
        else
        {
            goto loop;
        }
        mine_num = mine_num - 1;
    }
}

void print_board(char board[ROWS][COLS])
{
    int i = 0;
    int j = 0;
    printf("  ");
    for (i = 1; i < ROWS - 1; i++)
    {
        printf("  %d ", i);
    }
    printf("\n");
    for (i = 1; i < ROWS - 1; i++)
    {

        printf("  ");
        for (j = 1; j < COLS - 1; j++)
        {
            printf("|---", board[i][j]);
        }
        printf("|\n");
        printf("%2d", i);
        for (j = 1; j < COLS - 1; j++)
        {
            printf("| %c ", board[i][j]);
        }
        printf("|\n");
    }
    printf("  ");
    for (j = 1; j < COLS - 1; j++)
    {
        printf("|---", board[i][j]);
    }
    printf("|\n");
}

char is_mine_num(char mine[ROWS][COLS], char board[ROWS][COLS], int x, int y)
{
    int count = 0;
    if (mine[x - 1][y - 1] == '*')count++;
    if (mine[x - 1][y] == '*')count++;
    if (mine[x - 1][y + 1] == '*')count++;
    if (mine[x][y - 1] == '*')count++;
    if (mine[x][y + 1] == '*')count++;
    if (mine[x + 1][y - 1] == '*')count++;
    if (mine[x + 1][y] == '*')count++;
    if (mine[x + 1][y + 1] == '*')count++;
    board[x][y] = (count + '0');
    return board[x][y];
}

void play_game(char mine[ROWS][COLS], char board[ROWS][COLS])
{

    int count = (ROWS - 2)*(COLS - 2) - NUM;
    int x = 0;
    int y = 0;
    char ret = 0;
    while (count)
    {
    loop:
        printf("玩家:\n请输入你的选择坐标>:");
        scanf("%d %d", &x, &y);
        if ((x<1 || x>ROWS - 2) || (y > ROWS - 2 || y < 1))
        {
            printf("输入坐标无效!请重新输入。\n");
            goto loop;
        }
        if (mine[x][y] == '*')
        {
            int i = 0;
            int j = 0;
            while (count == ((ROWS - 2)*(COLS - 2) - NUM))
            {
            rand:
                i = rand() % (ROWS - 3 + 1) + 1;
                j = rand() % (COLS - 3 + 1) + 1;
                if (mine[i][j] == ' ')
                {
                    mine[i][j] = '*';
                    mine[x][y] = ' ';
                    count--;
                    board[x][y] = is_mine_num(mine, board, x, y);
                    print_board(board);
                    print_board(mine);
                    goto loop;
                }
                else
                {
                    goto rand;
                }
            }
            printf("踩地雷了!\n");
            return;
        }
        else
        {
            board[x][y] = is_mine_num(mine, board, x, y);
            print_board(board);
        }
        //print_board(mine);
        count--;
    }
    if (count == 0)
        printf("恭喜你扫雷完成\n");
}


void play()
{

    int input = 0;
    char board[ROWS][COLS];
    char mine[ROWS][COLS];
    srand((unsigned)time(NULL));
    do
    {
        meau();
    loop:
        printf("请选择>>:\n");
        scanf("%d", &input);
        if (input != 0 && input != 1)
        {
            printf("选择错误,请重新选择。\n");
            goto loop;
        }
        switch (input)
        {
        case PLAY:
            printf("游戏开始:\n");
            init_mine(mine);
            init_board(board);
            get_mine(mine);
            print_board(board);
            print_board(mine);
            play_game(mine, board);
            break;
        case EXIT:
            printf("退出游戏!");
            break;
        }
    } while (input);
}

int main()
{
    play();
    system("pause");
    return 0;
}

你可能感兴趣的:(C语言)