繁体   English   中英

主要功能的Tictactoe C程序错误

[英]Tictactoe c program error with main function

我正在用C创建一个简单的tictactoe游戏。但是,我的主函数不断出现此错误,在我的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;
}

您有两类问题。

首先,您需要功能原型。 #include的下面,但在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语句(或forwhile或其他一些语句)的主体中需要有多个语句,则必须用花括号将主体括起来:

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 多个语句需要大括号。

您在这段代码中有错误

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

当您在control flow (if,else-if..)iteration looping编写多行正文时,必须输入{}来定义正文。

您的代码必须是

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

必须声明原型函数以进行printcheckmove 否则将采用默认原型。

如果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语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM