C语言 | 扫雷游戏

C语言实现扫雷游戏

1.游戏规则

给定一个棋盘,玩家需要排查出所有隐藏的雷,也就是选择出所有不是雷的地方。玩家选择位置,若此处有雷,玩家被炸死,游戏结束;

若此处无雷,此处提示周围一圈所含雷的个数。直到玩家排查出了所有的雷,玩家赢,游戏结束。

2. 需求分析

需要菜单,选择进入游戏或者退出游戏
用于布置雷的棋盘
用于和用户交互的棋盘(使雷的位置向玩家透明,用于提示玩家某处周围有多少雷需要注意)
接收用户输入的坐标,转化为棋盘对应位置,排查此处有无雷,若无雷,那么周围有几个雷等信息。

3. 准备工作

1-头文件:game.h 用于定义符号,函数,引用库等;
2-源文件:game.c 用于实现相关函数,test.c 用于实现测试功能

4. 需求实现

1-搭建骨架
int main(int argc, char **argv)
{
    srand((unsigned int)time(NULL));
    int input;
    do
    {
        menu(); //打印菜单
        printf("Please enter your choice(1/0):> ");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("Welcome to Minesweeper!\n");
            game(); // 进入游戏
            break;

        case 0:
            printf("Bye bye ~\n");
            break; 

        default:
            printf("Invalid input, please re-enter.\n");
            break;
        }
    } while (input);
    
    return 0;
}

2-打印菜单
void menu()
{
    printf("*********************\n");
    printf("****  1. Play  ******\n");
    printf("****  0. Exit  ******\n");
    printf("*********************\n");
}
3-玩游戏
void game()
{
    //二维数组存储棋盘
    char mine[ROWS][COLS] = {0}; //存放布置好的雷的信息
    char show[ROWS][COLS] = {0}; //存放排查出的雷的信息
    //初始化棋盘
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');
    //打印棋盘
    // PrintBoard(mine, ROW, COL);
    PrintBoard(show, ROW, COL);
    //布置雷
    SetMine(mine, ROW, COL);
    // PrintBoard(mine, ROW, COL);
    //扫雷
    FindMine(show, mine, ROW, COL);

}

下面是各个子功能的实现:

用二维数组就可以实现棋盘逻辑。
一个数组够不够?考虑一下玩游戏的过程就知道,答案是否定的。玩家看到的棋盘是看不到哪里有雷哪里没有雷的,而且选择一个位置若没有雷,需要显示此位置的一周有多少个雷。所以我们不妨将雷的分布单独用一个数组记录,显示给用户的棋盘用另一个相同大小的数组表示,这样就可以互不干扰地进行查询和修改等交互行为。

对于布置雷的数组,我们可以定义:0表示无雷,1表示有雷,初始化为全0,布置雷的操作封装为一个函数来实现;
对于排查雷的数组,我们初始化为*,表示全部待排,当用户选中一个位置,我们查询布置雷的数组,判断此处有没有雷,若没有雷,进一步判断其周围一圈有多少雷,同时更新此处内容。
上述初始化过程也可以封装为函数:

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

交互过程中,适时打印出当前棋盘,方便玩家观察:

需要注意的是,为了方便排查某位置周围一圈雷的个数,我们的数组原本大小比需要排雷的部分大一圈:

void PrintBoard(char board[ROWS][COLS], int row, int col)
{
    int i;
    for(i = 0; i <= col; i++)
    {
        printf("%c ", i + '0');
    }
    printf("\n");

    for(i = 1; i <= row; i++)
    {
        printf("%c ", i + '0');
        int j;
        for(j = 1; j <= col; j++)
        {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
}

布置雷可以随机生成设定好的个数的位置(没被占用的位置)

void SetMine(char board[ROWS][COLS], int row, int col)
{
    //某座标的值设为'1'代表设置雷
    int count = 0;
    while(count < EASY_COUNT)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (board[x][y] == '0')
            {
                board[x][y] = '1'; //布置雷
                count++;
            }
        }       
    }
    
}

扫雷逻辑:
用户输入坐标
查询布雷数组,该坐标处有没有雷?
有,提示用户out,游戏结束,回到菜单界面
没有,进一步查询布雷数组,该坐标周围一圈有几个雷,不妨假设有n个
接着更新排雷数组,该坐标内容修改为字符n(因为初始化为字符*,所以直接将数组类型定义为char类型)

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


void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
    int count = 0;
    while(count < row * col - EASY_COUNT)
    {
        printf("Please enter x and y:>");
        int x, y;
        scanf("%d %d", &x, &y);

        if (mine[x][y] == '1')
        {
            printf("Oops, here is a bomb, you out!\n");
            PrintBoard(mine, row, col);
            printf("\n\n\n");
            break;
        }else
        {
            int ret = get_mine(mine, x, y);
            show[x][y] = ret + '0';
            PrintBoard(show, row, col);
            count++;
        }
    }
    if (count == row * col - EASY_COUNT)
    {
        printf("Congratulations, you successfully cleared all the mines!\n");
        PrintBoard(mine, row, col);
        printf("\n\n\n");
    }
}

5. 功能测试

点击查看游戏试玩记录
*********************
****  1. Play  ******
****  0. Exit  ******
*********************
Please enter your choice(1/0):> 1
Welcome to Minesweeper!
0 1 2 3 4 5 6 7 8 9 
1 * * * * * * * * * 
2 * * * * * * * * * 
3 * * * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>1 1
0 1 2 3 4 5 6 7 8 9 
1 1 * * * * * * * * 
2 * * * * * * * * * 
3 * * * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>1 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 * * * * * * * 
2 * * * * * * * * * 
3 * * * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>1 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 * * * * * * * * * 
3 * * * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>2 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 * * 1 * * * * * * 
3 * * * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>3 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 * * 1 * * * * * * 
3 * 1 * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>2 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 * 1 * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>3 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 * * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>4 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 * * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>4 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>5 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>5 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>6 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 * * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>7 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 0 * * * * * * * * 
8 * * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>8 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 0 * * * * * * * * 
8 0 * * * * * * * * 
9 * * * * * * * * * 
Please enter x and y:>9 1
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 0 * * * * * * * * 
8 0 * * * * * * * * 
9 0 * * * * * * * * 
Please enter x and y:>9 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 0 * * * * * * * * 
8 0 * * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>8 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 0 * * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 * * * * * * * * 
7 0 1 * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 2
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 * * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 0 * * * * * * * 
7 0 1 * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>3 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 1 * * * * * * 
4 0 0 * * * * * * * 
5 0 0 * * * * * * * 
6 0 0 * * * * * * * 
7 0 1 * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>4 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 1 * * * * * * 
4 0 0 0 * * * * * * 
5 0 0 * * * * * * * 
6 0 0 * * * * * * * 
7 0 1 * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 1 * * * * * * 
4 0 0 0 * * * * * * 
5 0 0 0 * * * * * * 
6 0 0 * * * * * * * 
7 0 1 * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 1 * * * * * * 
4 0 0 0 * * * * * * 
5 0 0 0 * * * * * * 
6 0 0 0 * * * * * * 
7 0 1 * * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 * * * * * * 
2 1 * 1 * * * * * * 
3 1 1 1 * * * * * * 
4 0 0 0 * * * * * * 
5 0 0 0 * * * * * * 
6 0 0 0 * * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>1 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 * * * * * * 
3 1 1 1 * * * * * * 
4 0 0 0 * * * * * * 
5 0 0 0 * * * * * * 
6 0 0 0 * * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>2 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 * * * * * * 
4 0 0 0 * * * * * * 
5 0 0 0 * * * * * * 
6 0 0 0 * * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>3 4  
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 * * * * * * 
5 0 0 0 * * * * * * 
6 0 0 0 * * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>4 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 * * * * * * 
6 0 0 0 * * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 0 * * * * * 
6 0 0 0 * * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 0 * * * * * 
6 0 0 0 0 * * * * * 
7 0 1 1 * * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 0 * * * * * 
6 0 0 0 0 * * * * * 
7 0 1 1 2 * * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 0 * * * * * 
6 0 0 0 0 * * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 0 * * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 * * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>4 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 * * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>3 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 * * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>2 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 * * * * * 
2 1 * 1 0 0 * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>1 5
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 * * * * 
2 1 * 1 0 0 * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>1 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 * * * 
2 1 * 1 0 0 * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>1 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 * * 
2 1 * 1 0 0 * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>1 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 * 
2 1 * 1 0 0 * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>1 9
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 * * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>2 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 * * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>2 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 * * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>2 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 * 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>2 9
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 * * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>3 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 * * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>4 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 * * * 
5 0 0 0 0 0 * * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 * * * 
5 0 0 0 0 0 0 * * * 
6 0 0 0 0 0 * * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 * * * 
5 0 0 0 0 0 0 * * * 
6 0 0 0 0 0 1 * * * 
7 0 1 1 2 1 * * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 * * * 
5 0 0 0 0 0 0 * * * 
6 0 0 0 0 0 1 * * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>4 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 1 * * 
5 0 0 0 0 0 0 * * * 
6 0 0 0 0 0 1 * * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 1 * * 
5 0 0 0 0 0 0 1 * * 
6 0 0 0 0 0 1 * * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * * * 
4 0 0 0 0 0 1 1 * * 
5 0 0 0 0 0 0 1 * * 
6 0 0 0 0 0 1 2 * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>3 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 * * 
5 0 0 0 0 0 0 1 * * 
6 0 0 0 0 0 1 2 * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>4 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 * * 
6 0 0 0 0 0 1 2 * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 * 
6 0 0 0 0 0 1 2 * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>5 9
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * * 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>6 9
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * * * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 * 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>7 9
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * * * * * * * 
9 0 1 * * * * * * * 
Please enter x and y:>9 3
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * * * * * * * 
9 0 1 2 * * * * * * 
Please enter x and y:>8 4
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * * * * * 
9 0 1 2 * * * * * * 
Please enter x and y:>8 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * * 1 * * 
9 0 1 2 * * * * * * 
Please enter x and y:>8 6
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * 2 1 * * 
9 0 1 2 * * * * * * 
Please enter x and y:>8 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * 2 1 2 * 
9 0 1 2 * * * * * * 
Please enter x and y:>9 7
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * 2 1 2 * 
9 0 1 2 * * * 0 * * 
Please enter x and y:>9 8
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * 2 1 2 * 
9 0 1 2 * * * 0 1 * 
Please enter x and y:>9 9
0 1 2 3 4 5 6 7 8 9 
1 1 1 1 0 0 0 0 0 0 
2 1 * 1 0 0 1 1 2 1 
3 1 1 1 0 0 1 * 3 * 
4 0 0 0 0 0 1 1 3 * 
5 0 0 0 0 0 0 1 2 2 
6 0 0 0 0 0 1 2 * 1 
7 0 1 1 2 1 2 * 3 2 
8 0 1 * 3 * 2 1 2 * 
9 0 1 2 * * * 0 1 1 
Please enter x and y:>8 5
Oops, here is a bomb, you out!
0 1 2 3 4 5 6 7 8 9 
1 0 0 0 0 0 0 0 0 0 
2 0 1 0 0 0 0 0 0 0 
3 0 0 0 0 0 0 1 0 1 
4 0 0 0 0 0 0 0 0 1 
5 0 0 0 0 0 0 0 0 0 
6 0 0 0 0 0 0 0 1 0 
7 0 0 0 0 0 0 1 0 0 
8 0 0 1 0 1 0 0 0 1 
9 0 0 0 1 0 0 0 0 0 



*********************
****  1. Play  ******
****  0. Exit  ******
*********************
Please enter your choice(1/0):> 0
Bye bye ~

6. 完整代码

1-test.c

点击查看代码
#include "game.h"

void menu()
{
    printf("*********************\n");
    printf("****  1. Play  ******\n");
    printf("****  0. Exit  ******\n");
    printf("*********************\n");
}

void game()
{
    //二维数组存储棋盘
    char mine[ROWS][COLS] = {0}; //存放布置好的雷的信息
    char show[ROWS][COLS] = {0}; //存放排查出的雷的信息
    //初始化棋盘
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');
    //打印棋盘
    // PrintBoard(mine, ROW, COL);
    PrintBoard(show, ROW, COL);
    //布置雷
    SetMine(mine, ROW, COL);
    // PrintBoard(mine, ROW, COL);
    //扫雷
    FindMine(show, mine, ROW, COL);

}

int main(int argc, char **argv)
{
    srand((unsigned int)time(NULL));
    int input;
    do
    {
        menu();
        printf("Please enter your choice(1/0):> ");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("Welcome to Minesweeper!\n");
            game();
            break;

        case 0:
            printf("Bye bye ~\n");
            break; 

        default:
            printf("Invalid input, please re-enter.\n");
            break;
        }
    } while (input);
    
    return 0;
}

2-game.h

点击查看代码
//库函数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//定义字符
#define ROW 9
#define COL 9
#define EASY_COUNT 10
#define ROWS ROW+2
#define COLS COL+2

//定义函数

//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void PrintBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col);

3-game.c

点击查看代码
#include "game.h"

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

void PrintBoard(char board[ROWS][COLS], int row, int col)
{
    int i;
    for(i = 0; i <= col; i++)
    {
        printf("%c ", i + '0');
    }
    printf("\n");

    for(i = 1; i <= row; i++)
    {
        printf("%c ", i + '0');
        int j;
        for(j = 1; j <= col; j++)
        {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
    //某座标的值设为'1'代表设置雷
    int count = 0;
    while(count < EASY_COUNT)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (board[x][y] == '0')
            {
                board[x][y] = '1'; //布置雷
                count++;
            }
        }       
    }
    
}

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


void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
    int count = 0;
    while(count < row * col - EASY_COUNT)
    {
        printf("Please enter x and y:>");
        int x, y;
        scanf("%d %d", &x, &y);

        if (mine[x][y] == '1')
        {
            printf("Oops, here is a bomb, you out!\n");
            PrintBoard(mine, row, col);
            printf("\n\n\n");
            break;
        }else
        {
            int ret = get_mine(mine, x, y);
            show[x][y] = ret + '0';
            PrintBoard(show, row, col);
            count++;
        }
    }
    if (count == row * col - EASY_COUNT)
    {
        printf("Congratulations, you successfully cleared all the mines!\n");
        PrintBoard(mine, row, col);
        printf("\n\n\n");
    }
}
posted @ 2022-11-27 20:56  万国码aaa  阅读(155)  评论(0编辑  收藏  举报