Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我的井字游戏中的错误_C - Fatal编程技术网

C 我的井字游戏中的错误

C 我的井字游戏中的错误,c,C,这是我的井字游戏代码: #include <stdio.h> #include <stdbool.h> #include <string.h> int board[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }; int main (voi

这是我的井字游戏代码:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int board[3][3] = {
                        {0, 0, 0},
                        {0, 0, 0},
                        {0, 0, 0}
                  };

int main (void)
{
    int const user1 = 1;
    int const user2 = 2;
    char move[10];

    while (! all_locations_filled()) {
        printf("User-1, please enter your move:");
        scanf("%s", move);

        if(valid_location(move)) {
            mark_location(user1, move);
            display_board(board[3][3]);
        }
        else if(won_the_game(user1)) {
            printf("Congratulations User-1, You Won the Game!");
            break;
        }
        else {
            printf("Invalid Move");
        }

        printf("User-2, please enter your move:");
        scanf("%s", move);

        if(valid_location(move)) {
            mark_location(user2, move);
            display_board();
        }
        else if(won_the_game(user2) {
            printf("Congratulations User-2, You Won the Game!");
            break;
        }
        else {
            printf("Invalid Move");
        }
    }

    return 0;
}

bool valid_location(char str[10]) {
    int strcmp(x, y);

    if (strcmp(str[10], "upperLeft") == 0 || strcmp(str[10], "up") == 0 || strcmp(str[10], "upperRight") == 0 || strcmp(str[10], "left") == 0 || strcmp(str[10], "center") == 0 || strcmp(str[10], "right") == 0 || strcmp(str[10], "lowerLeft") == 0 || strcmp(str[10], "down") == 0 || strcmp(str[10], "lowerRight") == 0)
        return true;
}

void mark_location(int userU, char str[10]) {
    int strcmp(x, y);

    if (strcmp(str[10], "upperLeft") == 0)
        board[0][0] = userU;
    else if (strcmp(str[10], "up") == 0)
        board[0][1] = userU;
    else if (strcmp(str[10], "upperRight") == 0)
        board[0][2] = userU;
    else if (strcmp(str[10], "left") == 0)
        board[1][0] = userU;
    else if (strcmp(str[10], "center") == 0)
        board[1][1] = userU;
    else if (strcmp(str[10], "right") == 0)
        board[1][2] = userU;
    else if (strcmp(str[10], "lowerLeft") == 0)
        board[2][0] = userU;
    else if (strcmp(str[10], "down") == 0)
        board[2][1] = userU;
    else if (strcmp(str[10], "lowerRight") == 0)
        board [2][2] = userU;
}

char display_board(int array[][]) {
    int i, j;

    for (i=0; i<3; ++i)
        for (j=0; j<3; ++j)
            if (array[i][j] == 0)
                print("-");
            else if (array[i][j] == 1)
                print("x");
            else if (array[i][j] == 2)
                print("o");
}

bool all_locations_filled() {
    int i, j;

    for (i=0; i<3; ++i)
        for (j=0; j<3; ++j)
            if board[i][j] == 0
                return false;
    return true;
}

bool won_the_game(userU) {
    int i, j;

    if (board[0][0] == userU && board[0][1] == userU && board[0][2] == userU)
        return true;
    else if (board[1][0] == userU && board[1][1] == userU && board[1][2] == userU)
        return true;
    else if (board[2][0] == userU && board[2][1] == userU && board[2][2] == userU)
        return true;
    else if (board[0][0] == userU && board[1][0] == userU && board[2][0] == userU)
        return true;
    else if (board[0][1] == userU && board[1][1] == userU && board[2][1] == userU)
        return true;
    else if (board[0][2] == userU && board[1][2] == userU && board[2][2] == userU)
        return true;
    else if (board[0][0] == userU && board[1][1] == userU && board[2][2] == userU)
        return true;
    else if (board[2][2] == userU && board[1][1] == userU && board[2][0] == userU)
        return true;
    else
        return false;
}
这个错误在主函数的末尾,但我不确定我做错了什么

tictactoe.c:52: error: nested functions are disabled, use -fnested-functions to re-enable
我不知道我使用了嵌套函数

tictactoe.c:53: warning: parameter names (without types) in function declaration
这是指int strcmp(x,y)

我把strcmp弄错了什么


如果有人能帮我,我将不胜感激。

这里缺少一个右括号(第40行):

应该是:

else if(won_the_game(user2)) {

您在strcmp方面也有一些问题

strcmp(str[10], "upperRight")
编译器正在抱怨第一个参数
str[10]
。一个问题是,这会从字符串中选择一个字符,而不是整个字符串。另一个问题是,在大小为10的数组中,位置编号为0..9,因此甚至没有位置10


此外,类似于
“upperRight”
的字符串文本包含10个可见字符加上一个额外的零字符作为终止符。因此,当它存储在
str

中时,它需要11个位置。如何使strcmp识别整个字符串而不仅仅是一个字符?通过传递整个字符串:-)使用
strcmp(str,“some value”)
但确保
str
数组足够大,以容纳字符串值。我收到了相同的错误消息。仍然说传递'strcmp'的参数1使指针从整数变为不带强制转换的整数。这到底是什么意思?我的意思是你已经解释过了,但是为什么我会收到相同的错误消息?在C中,字符被当作一个小整数处理。它的值是它包含的字符的(ASCII)代码。在调用strcmp时,两个参数都需要是指针,而不是整数。这就是编译器试图告诉您的。您发布的代码中有很多调用-您修复了所有调用吗?以及其他已识别的问题,您有
bool-valid_-location(char-str[10]){int-strcmp(x,y);
我假设这是试图声明
strcmp()
;你不需要这样做,因为你包含了
,即使你想这样做,你也不能这样做(因为
x
y
都不是类型,关于原因的肮脏细节;如果你真的想声明函数,你应该写
int-strcmp(const-char*s1,const-char*s2);
else if(won_the_game(user2) {
else if(won_the_game(user2)) {
strcmp(str[10], "upperRight")