C语言 井字游戏

2skhul33  于 2022-12-11  发布在  其他
关注(0)|答案(2)|浏览(154)

我正在学习c代码,做一个井字游戏。布尔问题已经解决了。现在的问题是循环printf(“没有空白!”);和prinf(“无效!!!”);在它取player 1的名字之后。我还想知道我用网格打印数组的那一行是否正确。

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

char space[3][3] = {
    {'1', '2', '3'},
    {'4', '5', '6'},
    {'7', '8', '9'},
};
int row;
int column;
char token = 'x';
bool tie = false;
char n1[256];
char n2[256];

void functionboard()
{
    char space[3][3] = {
        {'1', '2', '3'},
        {'4', '5', '6'},
        {'7', '8', '9'},
    };

    printf("        |        |    \n");
    printf("  ", space[0][0], "| ", space[0][1], "| ", space[0][2], "  \n");
    printf("______|________|_____\n");
    printf("        |        |    \n");
    printf("  ", space[1][0], "  | ", space[1][1], "  | ", space[1][2], "  \n");
    printf("______|________|_____\n");
    printf("        |        |    \n");
    printf("  ", space[2][0], "  | ", space[2][1], "  | ", space[2][2], "  \n");
    printf("      |        |    \n");
}

void functionOne()
{

    int dight;

    if (token == 'x')
    {
        printf(n1, "please enter");
        scanf("&d", &dight);
    }

    if (token == '0')
    {
        printf(n2, "please enter");
        scanf("&d", &dight);
    }

    if (dight == 1)
    {
        row = 0;
        column = 0;
    }

    if (dight == 2)
    {
        row = 0;
        column = 1;
    }

    if (dight == 3)
    {
        row = 0;
        column = 2;
    }

    if (dight == 4)
    {
        row = 1;
        column = 0;
    }

    if (dight == 5)
    {
        row = 1;
        column = 1;
    }

    if (dight == 6)
    {
        row = 1;
        column = 2;
    }
    if (dight == 7)
    {
        row = 2;
        column = 0;
    }

    if (dight == 8)
    {
        row = 2;
        column = 1;
    }

    if (dight == 9)
    {
        row = 2;
        column = 2;
    }

    else if (dight < 1 || dight > 9)
    {
        prinf("Invalid !!!");
    }

    if (token == 'x' && space[row][column] != 'x' && space[row][column] != '0')
    {
        space[row][column] = 'x';
        token = '0';
    }

    else if (token == '0' && space[row][column] != 'x' && space[row][column] != '0')
    {
        space[row][column] = '0';
        token = 'x';
    }
    else
    {
        printf("There is no empty space!");
        functionboard();
    }
    functionOne();
}

bool functionDraw()
{

    for (int i = 0; i < 3; i++)
    {
        if (space[i][0] == space[i][1] && space[i][0] == space[i][2] || space[0][i] == space[1][i] && space[0][i] == space[2][i])
            return true;
    }
    if (space[0][0] == space[1][1] && space[1][1] == space[2][2] || space[0][2] == space[1][1] && space[1][1] == space[2][0])
    {
        return true;
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if (space[i][j] != 'x' && space[i][j] != '0')
            {
                return false;
            }
        }
    }
    tie = true;
    return false;
}

int main()
{
    printf("Enter the name of the first player : \n");
    scanf("%c", n1);
    printf("Enter the name of the second player : \n");
    scanf("%c", n2);
    printf("%c is player1 so he/she will play first \n", n1);
    printf("%c is player2 so he/she will play first \n", n2);

    while (!functionDraw())
    {
        functionboard();
        functionOne();
        functionDraw();
    }

    if (token == 'x' && tie == false)
    {
        printf("%c Wins!!\n", n2);
    }
    else if (token == '0' && tie == false)
    {
        printf("%c Wins!!\n", n1);
    }
    else
    {
        printf("its a draw!!");
    }
}
qgzx9mmu

qgzx9mmu1#

如果您得到的错误是关于类型bool而不是变量本身,请参见this question:您还需要包含<stdbool.h>才能使用bool类型。

vnzz0bqm

vnzz0bqm2#

经典C语言中没有内置的bool类型。不过,您可以通过使用#include <stdbool.h>来使用它。另一种解决方案是将其替换为int,因为它可以像bool一样使用

相关问题