You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言井字棋(Tic Tac Toe)程序异常:任意输入均判定Player X获胜

C语言井字棋程序问题排查:无论输入均判定Player X获胜

在Putty环境中完成C语言井字棋作业时遇到问题:无论输入什么内容,程序都会返回“Player X Won!”。以下是函数代码、头文件代码及主函数代码:

函数代码

#include <stdio.h>
#include "tictac.h"
#define BOARD_SIZE 3 // size of the board
void print_theboard(char board[BOARD_SIZE][BOARD_SIZE]) {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            printf(" %c ", board[i][j]);
            if (j < BOARD_SIZE - 1) {
                printf("|");
            }
        }
        printf("\n");
        if (i < BOARD_SIZE - 1) {
            printf("---+---+---\n");
        }

    }
}


int check_whowon(char board[BOARD_SIZE][BOARD_SIZE]) {
    //Gewinn in den Reihen
    for (int i = 0; i < BOARD_SIZE; i++) {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
            return 1;
    }

    //Gewinn in den Spalten
    for (int j = 0; j < BOARD_SIZE; j++) {
        if (board[0][j] == board[1][j] && board[1][j] == board[2][j])
            return 1;
    }

  //Gewinn in den Diagonalen
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
        return 0;
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
        return 1;

    return 0;
}

头文件代码

void print_theboard();
int check_whowon();
int check_draw();

主函数代码

#include <stdio.h>
#include "tictac.h"

#define BOARD_SIZE 3 // size of the boad

int main() {
    char board[BOARD_SIZE][BOARD_SIZE];
    int row, col, game_over = 0;
    char player = 'X';

    // initialize the board with empty spaces
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = ' ';
        }
    }

    while (!game_over) {
        // display the current status of the board
        print_theboard(board);

        printf("Player %c, enter the row and column (e.g. 0 2): ", player);
        scanf("%d %d", &row, &col);

        // validate the input
        if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE) {
            board[row][col] = player;


            // check if the game is won or drawn
            if (check_whowon(board)) {
                printf("Player %c wins!\n", player);
                game_over = 1;
            }
            else {
            printf("Invalid input. Please try again.\n");
        } if(player='X')
            player='O';
                else
                player='X';


    }

    return 0;
}
}

问题根源及修复方案

1. check_whowon函数核心错误

  • 未排除空格判断:初始化棋盘时所有格子都是空格' ',函数仅判断三个格子相等,未检查是否为非空格字符。导致程序刚运行就因三个空格相等直接返回1,触发获胜判定。
  • 获胜逻辑混乱:对角线判断返回值不一致,且未区分X/O获胜,返回值无明确语义。

修复后的check_whowon函数:

#include <stdio.h>
#include "tictac.h"
#define BOARD_SIZE 3

void print_theboard(char board[BOARD_SIZE][BOARD_SIZE]) {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            printf(" %c ", board[i][j]);
            if (j < BOARD_SIZE - 1) {
                printf("|");
            }
        }
        printf("\n");
        if (i < BOARD_SIZE - 1) {
            printf("---+---+---\n");
        }
    }
}

int check_whowon(char board[BOARD_SIZE][BOARD_SIZE]) {
    // 检查行
    for (int i = 0; i < BOARD_SIZE; i++) {
        if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
            return (board[i][0] == 'X') ? 1 : 2; // 1=X赢,2=O赢
        }
    }

    // 检查列
    for (int j = 0; j < BOARD_SIZE; j++) {
        if (board[0][j] != ' ' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
            return (board[0][j] == 'X') ? 1 : 2;
        }
    }

    // 检查对角线
    if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        return (board[0][0] == 'X') ? 1 : 2;
    }
    if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        return (board[0][2] == 'X') ? 1 : 2;
    }

    return 0; // 无人获胜
}

2. 头文件声明不匹配

头文件中函数声明缺少参数,与实际定义不符,会导致编译警告。修复后的tictac.h:

#define BOARD_SIZE 3
void print_theboard(char board[BOARD_SIZE][BOARD_SIZE]);
int check_whowon(char board[BOARD_SIZE][BOARD_SIZE]);
int check_draw(char board[BOARD_SIZE][BOARD_SIZE]); // 补充参数,若后续实现

3. 主函数逻辑错误

  • 输入验证分支错误:原代码中else分支的“无效输入”提示逻辑错误,仅当输入不合法时才应触发该提示。
  • 玩家切换的赋值错误:if(player='X')是赋值操作,不是比较,应改为if(player == 'X')。
  • 代码结构错误:原主函数存在多余的闭合大括号,导致语法错误。

修复后的主函数:

#include <stdio.h>
#include "tictac.h"

int main() {
    char board[BOARD_SIZE][BOARD_SIZE];
    int row, col, game_over = 0;
    char player = 'X';

    // 初始化棋盘
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = ' ';
        }
    }

    while (!game_over) {
        print_theboard(board);

        printf("Player %c, enter the row and column (e.g. 0 2): ", player);
        scanf("%d %d", &row, &col);

        // 验证输入合法性
        if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == ' ') {
            board[row][col] = player;

            // 判断获胜
            int result = check_whowon(board);
            if (result == 1) {
                printf("Player X wins!\n");
                game_over = 1;
            } else if (result == 2) {
                printf("Player O wins!\n");
                game_over = 1;
            } else {
                // 切换玩家
                player = (player == 'X') ? 'O' : 'X';
            }
        } else {
            printf("Invalid input or cell already occupied. Please try again.\n");
        }
    }

    return 0;
}

内容的提问来源于stack exchange,提问作者wiwi weaam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 13:34:38