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 (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[10]);

        if(valid_location(move[10]))
            mark_location(user1, move[10]);
            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[10]);

        if(valid_location(move[10]))
            mark_location(user2, move[10]);
            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");
}

void 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][j] == userU)
        return true;
    else if (board[1][j] == userU)
        return true;
    else if (board[2][j] == userU)
        return true;
    else if (board[i][0] == userU)
        return true;
    else if (board[i][1] == userU)
        return true;
    else if (board[i][2] == userU)
        return true;
    else
        return false;
}

这是编译器给我的错误:

tictactoe.c: In function ‘main’:
tictactoe.c:19: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
tictactoe.c:24: error: expected expression before ‘else’
tictactoe.c:115: error: expected declaration or statement at end of input
tictactoe.c:115: error: expected declaration or statement at end of input

最佳答案

 if(valid_location(move[10]))
        mark_location(user1, move[10]);
        display_board(board[3][3]);

你必须使用“{”和“}”,因为你有两行。

关于c - 井字游戏中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8872847/

相关文章:

c - 通过虚拟机使用gcc编译时出现问题

Windows 和 Linux 中二进制文件的 C 库校验和

C - int 不等于 printf?

c - 如何通过 sock_stream 一次发送一个字符的字符串

c - 运行 GeeksforGeeks 示例信号代码时仅接收父 printf

objective-c - Swift 数组与 C 的互操作性?

c - 将结构传递给 xv6 系统调用

c - 为什么使用 ioctl 在内核模块中会收到此编译警告?

c - 通过C代码访问打印机

c++ - 递归地将一个数除以 2