简体   繁体   中英

Errors in Tic-Tac-Toe Game

I've been trying to code a tic-tac-toe game in C except I've gotten some errors I don't understand. I know this still needs some work but right now I just want to run the program before I add to it. Can someone help me? Here's my code:

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

Here are the errors the compiler gives me:

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]);

you have to use "{" and "}" because you have 2 lines.

  1. Use move instead of move[10] in your scanf statement, and when you're passing it to functions. move refers to the array, move[10] just means the 10th position in that array.
  2. Put braces {} around the code in your if / else blocks if they're more than a single line of code (or preferably always, but that's a style issue.)

I found some errors...

scanf("%s", move[10]);

What do you want to do here? If you want to read a string, use

scanf("%s", move );

If you want to read only one character in the 10th position of the array, use

scanf("%c", &move[9] );

Note that your array was declared as move[10], so it's positions go from move[0] to move[9]. Position move[10] is not valid.

Here:

    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");

You probably meant:

    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");

And here:

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

You forgot the () in the "if". It should be:

if (board[i][j] == 0)

Also, your functions must be declared before you call them. So, declare de functions before main.

You don't have to implement it there, just declare. For example:

void all_locations_filled();

int main (void)
{
...
}

In the last function:

bool won_the_game(userU)

you have to define the type of "userU".

You also forgot to close the brace "}" in the end of the main's while.

You are trying to scan an integer but the scanf argument expects an string (char array). Try %d instead of %s . Thats the Formatstring for Decimal numbers.

If you want to put more then one instruction after an if, you should close it in {} . Otherwise, the compiler think that only the first is in-condition, and the rest should be done anyway.

scanf("%s", move); not scanf("%s", move[10]);

if(valid_location(move)) not if(valid_location(move[10]))

mark_location(user1, move); not mark_location(user1, move[10]);

if (strcmp(str, "upperLeft") == 0) not if (strcmp(str[10], "upperLeft") == 0)

etc. etc.

You don't do arrays in C by putting square brackets after each and every use of an array. You use the square bracket in basically two situations, you are declaring the array in which case the brackets contain the size of the array, you are accessing an element of the array in which case the brackets contain the index.

You probably won't want to hear this but there's plenty else wrong with your code. You probably need to read a book, and start a bit simpler.

After you've dealt with your compiler errors you might want to look at the function won_the_game which is reading uninitialised variables i and j and will probably give you "access violation" errors as i and j are likely to be out of bounds.

In addition your logic is wrong since obviously you don't win by just occupying one position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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