【C】#井字棋#

井字棋

https://www.nowcoder.com/practice/0375c1d206ef48a3894f72aa07b2fdde

#include <stdio.h>

char isWin(char board[3][3]) {
    int i, j, flag = 1;
    //判断行
    for (i = 0; i < 3; i++) {
        flag = 1;
        for (j = 0; j < 2; j++) {
            if (board[i][j] == board[i][j + 1] && board[i][j] != 'O') {
                ;
            } else {
                flag = 0;
                break;
            }
        }
        if (flag) return board[i][j];
    }

    //判断列
    for (i = 0; i < 3; i++) {
        flag = 1;
        for (j = 0; j < 2; j++) {
            if (board[j][i] == board[j + 1][i] && board[j][i] != 'O') {
                ;
            } else {
                flag = 0;
                break;
            }
        }
        if (flag) return board[j][i];
    }

    //判断对角线
    //判断主对角线
    flag = 1;
    for (i = 0; i < 2; i++) {
        if (board[i][i] == board[i + 1][i + 1] && board[i][i] != 'O') {
            ;
        } else {
            flag = 0;
            break;
        }

    }
    if (flag) return board[i][i];

    //判断副对角线
    flag = 1;
    for (j = 0; j < 2; j++) {
        if (board[j][2 - j] == board[j + 1][1 - j] && board[j][2 - j] != 'O') {
            ;
        } else {
            flag = 0;
            break;
        }

    }
    if (flag) return board[j][2 - j];

    //棋盘满了,没有人获胜
    return 'O';
}

int main() {
    char board[3][3] = {0};
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%c ", &board[i][j]);
        }
    }
    if (isWin(board) == 'K') printf("KiKi wins!");
    else if (isWin(board) == 'B') printf("BoBo wins!");
    else printf("No winner!");
    return 0;
}

全部评论

相关推荐

赏个offer求你了:友塔HR还专门加我告诉我初筛不通过😂
点赞 评论 收藏
分享
耀孝女:就是你排序挂了
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务