0

我正在用 C 语言创建一个简单的 tictactoe 游戏。但是我在我的 main 函数中不断收到这个错误,我不知道在我的 else 语句之前它想要什么预期的表达式。该程序的工作原理是我从两个玩家那里获得符号,然后他们开始游戏。

错误:

tictac.c: In function ‘main’:
tictac.c:31: error: expected expression before ‘else’
tictac.c: At top level:
tictac.c:49: warning: conflicting types for ‘print’
tictac.c:30: warning: previous implicit declaration of ‘print’ was here
tictac.c:63: error: conflicting types for ‘check’
tictac.c:63: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
tictac.c:29: error: previous implicit declaration of ‘check’ was here
tictac.c:89: warning: conflicting types for ‘move’
tictac.c:28: warning: previous implicit declaration of ‘move’ was here

代码:

char board[3][3];

int main(void)
{
    int first;
    char player1, player2;

    printf("Player 1: Choose your symbol: \n");
    player1 = getchar();

    printf("Player 2: Choose your symbol: \n");
    player2 = getchar();

    int i=0;
    int win;
    char turn;
    while(win == 0)
    {
        if((i%2) == 0)
            turn = player1;
        move(player1);
        win = check(player1);
        print();
        else
            turn = player2;
        move(player2);
        i++;
    }

    if (i == 8)
        printf("its a tie");
    else
        printf("the winner is %c", turn);

    return 0;
}

/*printing the board that takes in a placement int*/
void print(void)
{
    int r;
    printf("\n");
    for (r = 0; r < 3; r++){
        printf(" %c | %c | %c \n" , board[r][0], board[r][2], board[r][3]);
        if (r != 2)
            printf("___________\n");
    }
    printf("\n");
    return;
}

/*check to see if someone won*/
int check(char player)
{
    int r, c;

    for ( r = 0 ; r <3 ; r++)
    {
        if ((board[r][0] == player) && (board[r][1] == player) && (board[r][2] == player))
            return 1;
    }

    for ( c = 0 ; c <3 ; c++)
    {
        if ((board[0][c] == player) && (board[1][c] == player) && (board[2][c] == player))
            return 1;
    }

    if((board[0][0] == player) && (board[1][1] == player) && (board[2][2] == player))
        return 1;

    if((board[0][2] == player) && (board[1][1] == player) && (board[2][0] == player))
        return 1;

    return 0;
}

void move(char player)
{
    int place;
    printf("player1, enter placement: \n");
    scanf("%d", &place);

    if (place == 1)
        board[0][0] = player;
    else if (place == 2)
        board[0][1] = player;
    else if (place == 3)
        board[0][2] = player;

    else if (place == 4)
        board[1][0] = player;
    else if (place == 5)
        board[1][1] = player;
    else if (place == 6)
        board[1][2] = player;

    else if (place == 7)
        board[2][0] = player;
    else if (place == 8)
        board[2][1] = player;
    else if (place == 9)
        board[2][2] = player;
}
4

3 回答 3

3

你有两类问题。

首先,您需要函数的原型。在您的 s 下方#include,但在您的 s 上方int main(void),您需要包含其他函数的声明(但不是定义):

void print(void);
int check(char player);
void move(char player);

这是必要的,因为 C 的设计使得它很容易一次编译,通过文件。如果编译器在使用这些函数之前不知道它们,那么您可能会遇到一些问题。

您的第二个问题是您在一些地方缺少大括号。在这里,例如:

if((i%2) == 0)
    turn = player1;
    move(player1);
    win = check(player1);
    print();
else
    turn = player2;
    move(player2);

如果一个if语句(或一个for或一个while或几个其他语句)需要在其主体中包含多个语句,则必须用大括号将主体括起来:

if((i%2) == 0)
{
    turn = player1;
    move(player1);
    win = check(player1);
    print();
}
else
{
    turn = player2;
    move(player2);
}

它在其他地方工作的唯一原因,比如这里:

if (i == 8)
    printf("its a tie");
else
    printf("the winner is %c", turn);

…是只有一个声明:调用printf. 多个语句需要大括号。

于 2013-04-20T04:58:14.717 回答
0

你在这段代码中有错误

        if((i%2) == 0)
                turn = player1;
                move(player1);
                win = check(player1);
                print();
        else
                turn = player2;
                move(player2);        
        i++;

control flow (if,else-if..)当你在or中写多行 body 时iteration looping,你必须 put{}来定义 body。

您的代码必须是

 if((i%2) == 0){
                turn = player1;
                move(player1);
                win = check(player1);
                print();
       }
        else{
                turn = player2;
                move(player2); 
        }       
        i++;

必须为print, check,声明原型函数move。否则它将采用默认原型。

于 2013-04-20T05:00:07.883 回答
0

如果 if/else 后面只有一行,则只能省略花括号。你的错误在这里:

        if((i%2) == 0)
            turn = player1;
            move(player1);
            win = check(player1);
            print();
    else
            turn = player2;
            move(player2);      

你需要花括号。也只是一个提示,在底部而不是 9 个 if 语句,您可以执行以下操作:

board[(place-1)/3][(place+2) % 3] = player;

这应该等同于您的 move() 函数中的 9 个 if 语句。

于 2013-04-20T05:07:38.627 回答