Home > Mobile >  Tic Tac Toe game
Tic Tac Toe game

Time:12-11

I was working on learning c code and was making a tic-tac-toe game. The Boolean issue was fixed. Now the issue is that it is looping the printf("There is no empty space!"); and prinf("Invalid !!!"); after it take the player1 name. I also wanted to know if the line where I printed the array with the grid is correct or not.

#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!!");
    }
}

CodePudding user response:

If the error you're getting is about the type bool and not the variable itself, see this question: you need to also include <stdbool.h> to be able to use the bool type.

CodePudding user response:

There's no built-in bool type in classic C. You can use it though by using #include <stdbool.h>. The other solution is replacing it with an int since it can be used like a bool

  • Related