井字游戏

问题描述 投票:0回答:3

我正在学习 C 代码并正在制作一个井字游戏。布尔问题已修复。现在的问题是它正在循环 printf(“没有空白空间!”);和prinf(“无效!!!”);之后取玩家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!!");
    }
}
c tic-tac-toe
3个回答
1
投票

经典 C 中没有内置

bool
类型。不过您可以通过使用
#include <stdbool.h>
来使用它。另一种解决方案是将其替换为
int
,因为它可以像
bool

一样使用

0
投票

如果您收到的错误是关于类型

bool
而不是变量本身,请参阅 这个问题:您还需要包含
<stdbool.h>
才能使用
bool
类型。


0
投票

兄弟,您在每种情况下都使用递归,所以接下来会发生什么,因为它之后每次都会运行。 我将其放入 do/while 循环中,以便它是有条件的。

© www.soinside.com 2019 - 2024. All rights reserved.