English 中文(简体)
Tic-Tac-Toe 游戏中的错误
原标题:Errors in Tic-Tac-Toe Game
  • 时间:2012-01-15 19:56:18
  •  标签:
  • c

我曾试图在C中形成一种tic-tac-toe游戏,但我不理解我会发现一些错误。 我知道,这仍然需要一些工作,但现在我只想在我加入之前管理该方案。 谁能帮助我? 我的守则如下:

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

你必须使用“i”和“}”,因为你有两条线。

  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.)

我发现了一些错误......

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

你想在这里做些什么? 如果你想读一下,使用

scanf("%s", move );

如果你只读10个阵列的位置,则使用

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

请注意,您的阵列被宣布为移动[10],因此其立场从移动[0]移至移动[9]。 职位调动[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");

也许意味着:

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

在此:

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

你在“如果”中想到(如果)。 应当:

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

此外,你的职责必须在你打电话之前宣布。 因此,在主事前宣布职能无效。

你不必在那里执行,只是宣布。 例如:

void all_locations_filled();

int main (void)
{
...
}

在最后一项职能中:

bool won_the_game(userU)

你们必须界定“用户U”的类型。

你们还想在主食的末尾关闭“}”。

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”,搬迁); notscanf(“%s”) 移动[10];

if(valid_position(move) not

mark_place(user1,举动); not mark_ place(user1, transport[10]);

if (strcmp(str, “upperLeft”) = 0) not

等等。

您在C中不设阵列,在每一组阵列使用之后都加上方括号。 你在基本上两种情况下使用方括号,你宣布括号内的阵列包含阵体大小,你正在使用阵列的一个要素,在这种情况下,括号中包含索引。

你们也许会 won想听,但是,你们的法典还有许多错误。 或许需要阅读一本书,开始比较简单。

在你处理你的编辑错误之后,你可能想看一下所赢得的“_加梅”功能,后者正在读一读的变量一和三分,并可能给你“出入违约”的错误,因为一、三分之一有可能脱离约束。

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





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签