C中的井字游戏

fhity93d  于 2023-01-12  发布在  其他
关注(0)|答案(2)|浏览(151)

对于一个任务,我需要在C语言中用Putty编写游戏Tic Tac Toe。我找不到程序的错误。无论我输入什么,程序都返回“玩家X赢了!”
有人能看出问题所在吗?
下面是这些函数的代码

#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;
}
~

下面是.h文件的代码

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;
}
}

无论我输入什么,程序都会返回“玩家X赢了!”

ckocjqey

ckocjqey1#

我猜这个检查很奇怪。无论你在第一轮把你的代币放在哪里,你仍然会有两行是空的。因为你用空格字符初始化你的棋盘,你的行检查

board[i][0] == board[i][1] && board[i][1] == board[i][2]

将评估为

' ' == ' ' && ' ' == ' '

这是真的。你应该另外检查行/列/对角线是否真的有一个标记。

euoag5mw

euoag5mw2#

电路板初始化为空格:

// 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] = ' ';
        }
    }

变量player用'X'初始化,因为X首先播放:

char player = 'X';

但是当第一次调用**check_whowon(board)**函数时,它检查水平线、垂直线或对角线是否有相同的字符(而不是它们是否有特定的'X'或'O'),所以它总是返回true,因为初始化了空格字符。

要解决此问题,请编辑checkwhowon()函数以忽略空格字符或专门检查“X”和“O”。

相关问题