C語言掃雷-創(chuàng)新互聯(lián)

一、游戲規(guī)則

一張9×9的棋盤上,有若干位置不明的地雷。玩家通過輸入坐標(biāo),就會(huì)顯示該坐標(biāo)周圍8個(gè)格子的地雷數(shù);如果該坐標(biāo)周圍沒有地雷,電腦會(huì)自動(dòng)選擇該坐標(biāo)周圍的格子,將范圍擴(kuò)大到周圍有地雷的格子處。周圍沒有地雷的格子不顯示數(shù)字,用空格來表示。如果玩家成功排除所有地雷(即剩下的格子全是地雷),則玩家勝利;反則,玩家失敗,同時(shí)顯示棋盤上的所有地雷。

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁制作,對(duì)成都高空作業(yè)車租賃等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站營銷優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。二、效果圖

如果選擇的坐標(biāo)處沒有地雷,會(huì)自動(dòng)擴(kuò)大到周圍有地雷的坐標(biāo)。

三、主要思路流程介紹
  1. 創(chuàng)建菜單

  1. 創(chuàng)建board來顯示棋盤

  1. 創(chuàng)建mine來設(shè)置地雷

  1. 玩家輸入坐標(biāo)

  1. 判定游戲是否結(jié)束

  1. 沒有踩雷,則繼續(xù)游戲

………………

  1. 當(dāng)棋盤上剩下的全部是地雷后,判定玩家勝利

四、代碼實(shí)現(xiàn)

這個(gè)部分主要顯示各個(gè)函數(shù)

該游戲的主要函數(shù)
#define ROW 9//定義掃雷的行數(shù)
#define COL 9//定義掃雷的列數(shù)
#define NUM 10//定義地雷數(shù)量
void init_board(char board[ROW + 2][COL + 2], int row, int col);//初始化游戲界面
void init_mine(char mine[ROW + 2][COL + 2], int row, int col);//初始化地雷數(shù)組
void set_mine(char mine[ROW + 2][COL + 2], int row, int col);//設(shè)置地雷,用 * 表示地雷
void show_board(char board[ROW + 2][COL + 2], int row, int col);//顯示游戲界面
void show_mine(char mine[ROW + 2][COL + 2], int row, int col);//顯示地雷
//計(jì)算地雷數(shù)
int count_mine(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y);
//玩家進(jìn)行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col);
//遞歸運(yùn)算,玩家每輸入一個(gè)坐標(biāo),電腦會(huì)自動(dòng)擴(kuò)大區(qū)域到周圍有地雷的坐標(biāo)
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2]);
1、游戲菜單
void menu()//菜單
{
    printf("****************************\n");
    printf("********   1.play   ********\n");
    printf("********   0.exit   ********\n");
    printf("****************************\n");
}
2、game函數(shù)
void game()
{
    //可以定義兩個(gè)二維數(shù)組,一個(gè)用來顯示游戲界面,數(shù)字字符(周圍的地雷數(shù));另一個(gè)用來保存地雷位置
    //將9×9擴(kuò)大為11×11,可以簡化9×9的邊界處理問題
    char board[ROW + 2][COL + 2];//游戲界面
    char mine[ROW + 2][COL + 2];//記錄地雷位置
    init_board(board, ROW, COL);//初始化游戲界面
    init_mine(mine, ROW, COL);//初始化地雷數(shù)組
    set_mine(mine, ROW, COL);//設(shè)置地雷,用 * 表示地雷
    show_board(board, ROW, COL);//顯示游戲界面
    //show_mine(mine, ROW, COL);//顯示地雷
    player(board, mine, ROW, COL);//玩家進(jìn)行操作
}
3、init_board函數(shù)
//用 # 來初始化游戲棋盤
void init_board(char board[ROW + 2][COL + 2], int row, int col)
{
    for (int i = 0; i< row + 2; i++)
    {
        for (int j = 0; j< col + 2; j++)
        {
            board[i][j] = ' ';
        }
    }
    for (int i = 1; i<= row; i++)
    {
        for (int j = 1; j<= col; j++)
        {
            board[i][j] = '#';
        }
    }
}
4、init_mine函數(shù)
//初始化地雷數(shù)組
void init_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
    for (int i = 0; i< row + 2; i++)
    {
        for (int j = 0; j< col + 2; j++)
        {
            mine[i][j] = ' ';
        }
    }
}
5、set_mine函數(shù)
//設(shè)置地雷,用 * 表示地雷
void set_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
    srand((unsigned int)time(NULL));
    int num = 0;
    while (num< NUM)
    {
        int r = rand() % 9 + 1;
        int c = rand() % 9 + 1;
        if (mine[r][c] == ' ')
        {
            mine[r][c] = '*';
            num++;
        }
    }
}
6、show_board函數(shù)
//顯示棋盤
void show_board(char board[ROW + 2][COL + 2], int row, int col)
{
    printf("0  1   2   3   4   5   6   7   8   9\n");
    for (int i = 1; i<= row; i++)
    {
        if (i == 1)
        {
            printf(" ");
            for (int j = 1; j<= col; j++)
            {
                printf("----");
            }
            printf("--->y\n");
        }
        printf("%d", i);
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("| %c |",board[i][j]);
            }
            else
            {
                printf("| %c ", board[i][j]);
            }
        }
        printf("\n");
        printf(" ");
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("|---|");
            }
            else
            {
                printf("|---");
            }
        }
        printf("\n");
    }
    printf(" |\n");
    printf(" %c\n", 25);
    printf(" x\n");
}
7、show_mine函數(shù)
//顯示地雷
void show_mine(char mine[ROW + 2][COL + 2], int row, int col)
{

    printf("0  1   2   3   4   5   6   7   8   9\n");
    for (int i = 1; i<= row; i++)
    {
        if (i == 1)
        {
            printf(" ");
            for (int j = 1; j<= col; j++)
            {
                printf("----");
            }
            printf("--->y\n");
        }
        printf("%d", i);
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("| %c |", mine[i][j]);
            }
            else
            {
                printf("| %c ", mine[i][j]);
            }
        }
        printf("\n");
        printf(" ");
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("|---|");
            }
            else
            {
                printf("|---");
            }
        }
        printf("\n");
    }
    printf(" |\n");
    printf(" %c\n", 25);
    printf(" x\n");
}
8、player函數(shù)
//玩家進(jìn)行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col)
{
    int x, y;
    while (1)
    {
        while (1)
        {
            printf("請(qǐng)選擇坐標(biāo)(x,y)>:");
            scanf("%d %d", &x, &y);
            if (x >= 1 && x<= 9 && y >= 1 && y<= 9 && board[x][y] == '#')
            {
                break;
            }
            else
            {
                printf("警告:坐標(biāo)非法,請(qǐng)重新輸入\n");
            }
        }
        if (mine[x][y] == '*')
        {
            for (int i = 1; i<= row; i++)
            {
                for (int j = 1; j<= col; j++)
                {
                    if (mine[i][j] == '*')
                    {
                        board[i][j] = '*';
                    }
                }
            }
            system("cls");
            //show_mine(mine, ROW, COL);
            show_board(board, ROW, COL);
            printf("你被炸死了\n");
            break;
        }
        else if (mine[x][y] != '*')
        {
            board[x][y] = count_mine(board,mine,x,y);
            //mine[x][y]= count_mine(board, mine, x, y);
            system("cls");
            int arr[ROW + 2][COL + 2] = { 0 };
            if (board[x][y] == ' ')
            {
                arr[x][y] = 1;
                fit_play(board, mine, x, y, arr);
            }
            show_board(board, row, col);
        }
        int num = 0;
        for (int i = 1; i<= row; i++)
        {
            for (int j = 1; j<= col; j++)
            {
                if (board[i][j] == '#')
                {
                    num++;
                }
            }
        }
        if (num == NUM)
        {
            system("cls");
            for (int i = 1; i<= row; i++)
            {
                for (int j = 1; j<= col; j++)
                {
                    if (mine[i][j] == '*')
                    {
                        board[i][j] = '*';
                    }
                }
            }
            show_board(board, ROW, COL);
            printf("游戲結(jié)束,你贏了\n");
            break;
        }
    }
}
9、count_mine函數(shù)
//計(jì)算地雷數(shù)
int count_mine(char board[ROW+2][COL+2],char mine[ROW + 2][COL + 2], int x, int y)
{
    int count = (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] - 32 * 8) / 10 + 48;
    if (count == 48)
    {
        return 32;
    }
    return count;
}
10、fit_play函數(shù)
//遞歸運(yùn)算,玩家每輸入一個(gè)坐標(biāo),電腦會(huì)自動(dòng)擴(kuò)大區(qū)域到周圍有地雷的坐標(biāo)
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2])
{
    while (x >= 1 && x<= ROW && y >= 1 && y<= COL)
    {
        if (count_mine(board,mine, x - 1, y - 1) == 32 && arr[x - 1][y - 1] == 0)
        {
            board[x - 1][y - 1] = count_mine(board, mine, x - 1, y - 1);
            while (arr[x - 1][y - 1] == 0)
            {
                arr[x - 1][y - 1] = 1;
                fit_play(board, mine, x - 1, y - 1, arr);
            }
        }
        else if (count_mine(board,mine, x - 1, y - 1) != 32)
        {
            board[x - 1][y - 1] = count_mine(board,mine, x - 1, y - 1);
        }
        if (count_mine(board,mine, x - 1, y) == 32 && arr[x - 1][y] == 0)
        {
            board[x - 1][y] = count_mine(board, mine, x - 1, y);
            while (arr[x - 1][y] == 0)
            {
                arr[x - 1][y] = 1;
                fit_play(board, mine, x - 1, y, arr);
            }
        }
        else if (count_mine(board,mine, x - 1, y) != 32)
        {
            board[x - 1][y] = count_mine(board,mine, x - 1, y);
        }
        if (count_mine(board,mine, x - 1, y + 1) == 32 && arr[x - 1][y + 1] == 0)
        {
            board[x - 1][y + 1] = count_mine(board, mine, x - 1, y + 1);
            while (arr[x - 1][y + 1] == 0)
            {
                arr[x - 1][y + 1] = 1;
                fit_play(board, mine, x - 1, y + 1, arr);
            }
        }
        else if (count_mine(board,mine, x - 1, y + 1) != 32)
        {
            board[x - 1][y + 1] = count_mine(board,mine, x - 1, y + 1);
        }
        if (count_mine(board,mine, x, y - 1) == 32 && arr[x][y - 1] == 0)
        {
            board[x][y - 1] = count_mine(board, mine, x, y - 1);
            while (arr[x][y - 1] == 0)
            {
                arr[x][y - 1] = 1;
                fit_play(board, mine, x, y - 1, arr);
            }
        }
        else if (count_mine(board,mine, x, y - 1) != 32)
        {
            board[x][y - 1] = count_mine(board,mine, x, y - 1);
        }
        if (count_mine(board,mine, x, y + 1) == 32 && arr[x][y + 1] == 0)
        {
            board[x][y + 1] = count_mine(board, mine, x, y + 1);
            while (arr[x][y + 1] == 0)
            {
                arr[x][y + 1] = 1;
                fit_play(board, mine, x, y + 1, arr);
            }
        }
        else if (count_mine(board,mine, x, y + 1) != 32)
        {
            board[x][y + 1] = count_mine(board,mine, x, y + 1);
        }
        if (count_mine(board,mine, x + 1, y - 1) == 32 && arr[x + 1][y - 1] == 0)
        {
            board[x + 1][y - 1] = count_mine(board, mine, x + 1, y - 1);
            while (arr[x + 1][y - 1] == 0)
            {
                arr[x + 1][y - 1] = 1;
                fit_play(board, mine, x + 1, y - 1, arr);
            }
        }
        else if (count_mine(board,mine, x + 1, y - 1) != 32)
        {
            board[x + 1][y - 1] = count_mine(board,mine, x + 1, y - 1);
        }
        if (count_mine(board,mine, x + 1, y) == 32 && arr[x + 1][y] == 0)
        {
            board[x + 1][y] = count_mine(board, mine, x + 1, y);
            while (arr[x + 1][y] == 0)
            {
                arr[x + 1][y] = 1;
                fit_play(board, mine, x + 1, y, arr);
            }
        }
        else if (count_mine(board,mine, x + 1, y) != 32)
        {
            board[x + 1][y] = count_mine(board,mine, x + 1, y);
        }
        if (count_mine(board,mine, x + 1, y + 1) == 32 && arr[x + 1][y + 1] == 0)
        {
            board[x + 1][y + 1] = count_mine(board, mine, x + 1, y + 1);
            while (arr[x + 1][y + 1] == 0)
            {
                arr[x + 1][y + 1] = 1;
                fit_play(board, mine, x + 1, y + 1, arr);
            }
        }
        else if (count_mine(board,mine, x + 1, y + 1) != 32)
        {
            board[x + 1][y + 1] = count_mine(board,mine, x + 1, y + 1);
        }
        break;
    }
}
五、總代碼
#define _CRT_SECURE_NO_WARNINGS
#include#include#include#define ROW 9//定義掃雷的行數(shù)
#define COL 9//定義掃雷的列數(shù)
#define NUM 10//定義地雷數(shù)量
void init_board(char board[ROW + 2][COL + 2], int row, int col);//初始化游戲界面
void init_mine(char mine[ROW + 2][COL + 2], int row, int col);//初始化地雷數(shù)組
void set_mine(char mine[ROW + 2][COL + 2], int row, int col);//設(shè)置地雷,用 * 表示地雷
void show_board(char board[ROW + 2][COL + 2], int row, int col);//顯示游戲界面
void show_mine(char mine[ROW + 2][COL + 2], int row, int col);//顯示地雷
//計(jì)算地雷數(shù)
int count_mine(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y);
//玩家進(jìn)行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col);
//遞歸運(yùn)算,玩家每輸入一個(gè)坐標(biāo),電腦會(huì)自動(dòng)擴(kuò)大區(qū)域到周圍有地雷的坐標(biāo)
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2]);
void menu()//菜單
{
    printf("****************************\n");
    printf("********   1.play   ********\n");
    printf("********   0.exit   ********\n");
    printf("****************************\n");
}
void game()
{
    //可以定義兩個(gè)二維數(shù)組,一個(gè)用來顯示游戲界面,數(shù)字字符(周圍的地雷數(shù));另一個(gè)用來保存地雷位置
    //將9×9擴(kuò)大為11×11,可以簡化9×9的邊界處理問題
    char board[ROW + 2][COL + 2];//游戲界面
    char mine[ROW + 2][COL + 2];//記錄地雷位置
    init_board(board, ROW, COL);//初始化游戲界面
    init_mine(mine, ROW, COL);//初始化地雷數(shù)組
    set_mine(mine, ROW, COL);//設(shè)置地雷,用 * 表示地雷
    show_board(board, ROW, COL);//顯示游戲界面
    //show_mine(mine, ROW, COL);//顯示地雷
    player(board, mine, ROW, COL);//玩家進(jìn)行操作
}
int main()
{
    int choice;
    do
    {
        menu();
        printf("請(qǐng)選擇>:");
        scanf("%d", &choice);
        printf("\n");
        switch (choice)
        {
        case 1:
            system("cls");
            game();
            system("pause");
            system("cls");
            break;
        case 0:
            break;
        default:
            printf("輸入錯(cuò)誤,請(qǐng)重新輸入\n");
            system("pause");
            system("cls");
        }
    } while (choice);
    return 0;
}
//用 # 來初始化游戲棋盤
void init_board(char board[ROW + 2][COL + 2], int row, int col)
{
    for (int i = 0; i< row + 2; i++)
    {
        for (int j = 0; j< col + 2; j++)
        {
            board[i][j] = ' ';
        }
    }
    for (int i = 1; i<= row; i++)
    {
        for (int j = 1; j<= col; j++)
        {
            board[i][j] = '#';
        }
    }
}
//初始化地雷數(shù)組
void init_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
    for (int i = 0; i< row + 2; i++)
    {
        for (int j = 0; j< col + 2; j++)
        {
            mine[i][j] = ' ';
        }
    }
}
//設(shè)置地雷,用 * 表示地雷
void set_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
    srand((unsigned int)time(NULL));
    int num = 0;
    while (num< NUM)
    {
        int r = rand() % 9 + 1;
        int c = rand() % 9 + 1;
        if (mine[r][c] == ' ')
        {
            mine[r][c] = '*';
            num++;
        }
    }
}
//顯示棋盤
void show_board(char board[ROW + 2][COL + 2], int row, int col)
{
    printf("0  1   2   3   4   5   6   7   8   9\n");
    for (int i = 1; i<= row; i++)
    {
        if (i == 1)
        {
            printf(" ");
            for (int j = 1; j<= col; j++)
            {
                printf("----");
            }
            printf("--->y\n");
        }
        printf("%d", i);
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("| %c |",board[i][j]);
            }
            else
            {
                printf("| %c ", board[i][j]);
            }
        }
        printf("\n");
        printf(" ");
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("|---|");
            }
            else
            {
                printf("|---");
            }
        }
        printf("\n");
    }
    printf(" |\n");
    printf(" %c\n", 25);
    printf(" x\n");
}
//顯示地雷
void show_mine(char mine[ROW + 2][COL + 2], int row, int col)
{

    printf("0  1   2   3   4   5   6   7   8   9\n");
    for (int i = 1; i<= row; i++)
    {
        if (i == 1)
        {
            printf(" ");
            for (int j = 1; j<= col; j++)
            {
                printf("----");
            }
            printf("--->y\n");
        }
        printf("%d", i);
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("| %c |", mine[i][j]);
            }
            else
            {
                printf("| %c ", mine[i][j]);
            }
        }
        printf("\n");
        printf(" ");
        for (int j = 1; j<= col; j++)
        {
            if (j == col)
            {
                printf("|---|");
            }
            else
            {
                printf("|---");
            }
        }
        printf("\n");
    }
    printf(" |\n");
    printf(" %c\n", 25);
    printf(" x\n");
}
//計(jì)算地雷數(shù)
int count_mine(char board[ROW+2][COL+2],char mine[ROW + 2][COL + 2], int x, int y)
{
    int count = (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] - 32 * 8) / 10 + 48;
    if (count == 48)
    {
        return 32;
    }
    return count;
}
//遞歸運(yùn)算,玩家每輸入一個(gè)坐標(biāo),電腦會(huì)自動(dòng)擴(kuò)大區(qū)域到周圍有地雷的坐標(biāo)
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2])
{
    while (x >= 1 && x<= ROW && y >= 1 && y<= COL)
    {
        if (count_mine(board,mine, x - 1, y - 1) == 32 && arr[x - 1][y - 1] == 0)
        {
            board[x - 1][y - 1] = count_mine(board, mine, x - 1, y - 1);
            while (arr[x - 1][y - 1] == 0)
            {
                arr[x - 1][y - 1] = 1;
                fit_play(board, mine, x - 1, y - 1, arr);
            }
        }
        else if (count_mine(board,mine, x - 1, y - 1) != 32)
        {
            board[x - 1][y - 1] = count_mine(board,mine, x - 1, y - 1);
        }
        if (count_mine(board,mine, x - 1, y) == 32 && arr[x - 1][y] == 0)
        {
            board[x - 1][y] = count_mine(board, mine, x - 1, y);
            while (arr[x - 1][y] == 0)
            {
                arr[x - 1][y] = 1;
                fit_play(board, mine, x - 1, y, arr);
            }
        }
        else if (count_mine(board,mine, x - 1, y) != 32)
        {
            board[x - 1][y] = count_mine(board,mine, x - 1, y);
        }
        if (count_mine(board,mine, x - 1, y + 1) == 32 && arr[x - 1][y + 1] == 0)
        {
            board[x - 1][y + 1] = count_mine(board, mine, x - 1, y + 1);
            while (arr[x - 1][y + 1] == 0)
            {
                arr[x - 1][y + 1] = 1;
                fit_play(board, mine, x - 1, y + 1, arr);
            }
        }
        else if (count_mine(board,mine, x - 1, y + 1) != 32)
        {
            board[x - 1][y + 1] = count_mine(board,mine, x - 1, y + 1);
        }
        if (count_mine(board,mine, x, y - 1) == 32 && arr[x][y - 1] == 0)
        {
            board[x][y - 1] = count_mine(board, mine, x, y - 1);
            while (arr[x][y - 1] == 0)
            {
                arr[x][y - 1] = 1;
                fit_play(board, mine, x, y - 1, arr);
            }
        }
        else if (count_mine(board,mine, x, y - 1) != 32)
        {
            board[x][y - 1] = count_mine(board,mine, x, y - 1);
        }
        if (count_mine(board,mine, x, y + 1) == 32 && arr[x][y + 1] == 0)
        {
            board[x][y + 1] = count_mine(board, mine, x, y + 1);
            while (arr[x][y + 1] == 0)
            {
                arr[x][y + 1] = 1;
                fit_play(board, mine, x, y + 1, arr);
            }
        }
        else if (count_mine(board,mine, x, y + 1) != 32)
        {
            board[x][y + 1] = count_mine(board,mine, x, y + 1);
        }
        if (count_mine(board,mine, x + 1, y - 1) == 32 && arr[x + 1][y - 1] == 0)
        {
            board[x + 1][y - 1] = count_mine(board, mine, x + 1, y - 1);
            while (arr[x + 1][y - 1] == 0)
            {
                arr[x + 1][y - 1] = 1;
                fit_play(board, mine, x + 1, y - 1, arr);
            }
        }
        else if (count_mine(board,mine, x + 1, y - 1) != 32)
        {
            board[x + 1][y - 1] = count_mine(board,mine, x + 1, y - 1);
        }
        if (count_mine(board,mine, x + 1, y) == 32 && arr[x + 1][y] == 0)
        {
            board[x + 1][y] = count_mine(board, mine, x + 1, y);
            while (arr[x + 1][y] == 0)
            {
                arr[x + 1][y] = 1;
                fit_play(board, mine, x + 1, y, arr);
            }
        }
        else if (count_mine(board,mine, x + 1, y) != 32)
        {
            board[x + 1][y] = count_mine(board,mine, x + 1, y);
        }
        if (count_mine(board,mine, x + 1, y + 1) == 32 && arr[x + 1][y + 1] == 0)
        {
            board[x + 1][y + 1] = count_mine(board, mine, x + 1, y + 1);
            while (arr[x + 1][y + 1] == 0)
            {
                arr[x + 1][y + 1] = 1;
                fit_play(board, mine, x + 1, y + 1, arr);
            }
        }
        else if (count_mine(board,mine, x + 1, y + 1) != 32)
        {
            board[x + 1][y + 1] = count_mine(board,mine, x + 1, y + 1);
        }
        break;
    }
}
//玩家進(jìn)行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col)
{
    int x, y;
    while (1)
    {
        while (1)
        {
            printf("請(qǐng)選擇坐標(biāo)(x,y)>:");
            scanf("%d %d", &x, &y);
            if (x >= 1 && x<= 9 && y >= 1 && y<= 9 && board[x][y] == '#')
            {
                break;
            }
            else
            {
                printf("警告:坐標(biāo)非法,請(qǐng)重新輸入\n");
            }
        }
        if (mine[x][y] == '*')
        {
            for (int i = 1; i<= row; i++)
            {
                for (int j = 1; j<= col; j++)
                {
                    if (mine[i][j] == '*')
                    {
                        board[i][j] = '*';
                    }
                }
            }
            system("cls");
            //show_mine(mine, ROW, COL);
            show_board(board, ROW, COL);
            printf("你被炸死了\n");
            break;
        }
        else if (mine[x][y] != '*')
        {
            board[x][y] = count_mine(board,mine,x,y);
            //mine[x][y]= count_mine(board, mine, x, y);
            system("cls");
            int arr[ROW + 2][COL + 2] = { 0 };
            if (board[x][y] == ' ')
            {
                arr[x][y] = 1;
                fit_play(board, mine, x, y, arr);
            }
            show_board(board, row, col);
        }
        int num = 0;
        for (int i = 1; i<= row; i++)
        {
            for (int j = 1; j<= col; j++)
            {
                if (board[i][j] == '#')
                {
                    num++;
                }
            }
        }
        if (num == NUM)
        {
            system("cls");
            for (int i = 1; i<= row; i++)
            {
                for (int j = 1; j<= col; j++)
                {
                    if (mine[i][j] == '*')
                    {
                        board[i][j] = '*';
                    }
                }
            }
            show_board(board, ROW, COL);
            printf("游戲結(jié)束,你贏了\n");
            break;
        }
    }
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

標(biāo)題名稱:C語言掃雷-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.muchs.cn/article28/dgiejp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、面包屑導(dǎo)航網(wǎng)站導(dǎo)航、用戶體驗(yàn)、域名注冊(cè)、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化