简单的C Tic Tac Toe输入错误

时间:2014-01-28 02:07:31

标签: c input tic-tac-toe

我目前正在尝试学习C,并决定制作一个简单的Tic Tac Toe“游戏”。

我不是想做任何花哨的事情,我只想把用户的位置和他们给定的角色放在我的网格中。我还没有开始使用数组或任何东西,所以我使用printf组合了一个网格。

当我尝试实际运行游戏时,问题出现了,角色无效。我可以键入正确的输入,但它只是使该位置变为空白。如何使这个switch语句将字符存储在我想要的位置?

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    // Initialize and define all variables and characters
    char c1='.', c2='.', c3='.', c4='.', c5='.', c6='.', c7='.', c8='.', c9='.'; // Board     characters
    char given; // User-input character
    int pos; // User-input position for the character
    int turnCount; // Gives a limit on game length

    for (turnCount = 0; turnCount < 9; turnCount++)
    {
        // Display lines
        printf("%c %c %c   1 2 3\n", c1, c2, c3); // First line
        printf("%c %c %c   4 5 6\n", c4, c5, c6); // Second line
        printf("%c %c %c   7 8 9\n", c7, c8, c9); // Third line

        // Asks for input, scans input for position and character to use
        printf("Choose a position and character, without spaces.\n");
        scanf_s("%i%c", &pos, &given);

        // Matches the input position with a character, defines the character
        switch (pos)
        {
        case 1: c1 = given; break;
        case 2: c2 = given; break;
        case 3: c3 = given; break;
        case 4: c4 = given; break;
        case 5: c5 = given; break;
        case 6: c6 = given; break;
        case 7: c7 = given; break;
        case 8: c8 = given; break;
        case 9: c9 = given; break;
        } // End switch
    } // End for - game should be over
    return 0;
}

2 个答案:

答案 0 :(得分:3)

scanf_s("%i%c", &pos, &given);替换为scanf("%i%c", &pos, &given);将解决您的问题。这是因为scanf_s需要字符输入的缓冲区大小,而scanf则不需要。另一种方法是包含缓冲区大小:scanf_s("%i%c", &pos, &given,1);

我已经对它们进行了测试,它们可以在我的Windows系统上运行。

编辑: 为了好玩,我使用数组实现了这一点。这种方式有点干净。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Initialize and define all variables and characters
    char gameBoard[3][3];
    char given; // User-input character
    int pos; // User-input position for the character
    int turnCount; // Gives a limit on game length
    int i;
    //set the gameboard to all '.'
    for(i=0;i<9;i++)
    {
        gameBoard[i/3][i%3] = '.';
    }

    for (turnCount = 0; turnCount < 9; turnCount++)
    {
        // Display lines
        printf("%c %c %c   1 2 3\n", gameBoard[0][0], gameBoard[0][1], gameBoard[0][2]); // First line
        printf("%c %c %c   4 5 6\n", gameBoard[1][0], gameBoard[1][1], gameBoard[1][2]); // Second line
        printf("%c %c %c   7 8 9\n", gameBoard[2][0], gameBoard[2][1], gameBoard[2][2]); // Third line

        // Asks for input, scans input for position and character to use
        printf("Choose a position and character, without spaces.\n");
        scanf_s("%i%c", &pos, &given,1);

        // sets the character on the gameboard.
        gameBoard[(pos-1)/3][(pos-1)%3] = given;
    } // End for - game should be over
    return 0;
}

答案 1 :(得分:3)

如果我将scanf_s更改为scanf,此代码似乎适用于Linux。我这里没有Windows系统来测试它,但我认为scanf_s函数调用没有问题。

根据这里:http://faculty.edcc.edu/paul.bladek/CS131/scanf_s.htm

Unlike scanf,  scanf_s  requires the buffer size to be specified for all input 
parameters of type c, C, s, S, or [. The buffer size is passed as an additional 
parameter immediately following the pointer to the buffer or variable

所以也许你应该尝试这样的scanf_s电话:

scanf_s("%i%c",&pos, &given, 1);

我现在无法验证这一点,但是......