Ash
Ash

Reputation: 141

tic tac toe in C-error in comparing data

When I run my program, the if statements inside the else-if function carries out for any input by user.When I enter 'x' in the slot [0][0] player 1 wins :(, i did even try board[0][0]!=' ' it did not work.In the if function I compared the boards diagonal inputs and the boards horizontal and vertical inputs but i'm not sure what the mistake is.

#include<stdio.h>
int main()
{
    char xo = 0,board[3][3]={
        {' ',' ',' '},
        {' ',' ',' '},
        {' ',' ',' '},
    };
    int row,col,i,win=0,player=0;

    for(i=0; i<9;i++)
    {   
        printf("   1   2   3\n");
        printf("1| %c | %c | %c |\n",board[0][0],board[0][1],board[0][2]);
        printf("2| %c | %c | %c |\n",board[1][0],board[1][1],board[1][2]);
        printf("3| %c | %c | %c |\n",board[2][0],board[2][1],board[2][2]);

        player = player%2+1;

        jump:
        printf("Enter 'x' , 'o' or 'q'(to quit): ");
        scanf(" %c", &xo);

        if( xo == 'q' || xo == 'Q')
        {
            printf("Thank you for playing!!\n");
            return 0;
        }
        else if( xo =='X' || xo =='x'||xo == 'o'||xo == 'O')
        {
            printf("Enter row: ");
            scanf("%i",&row);
            printf("Enter column: ");
            scanf("%i",&col);

            board[row-1][col-1] = xo;
            /*these functions and statements dont work according to my inputs*/
            if((board[0][0]==board[1][1] && board[1][1]==board[2][2])
            ||(board[0][2]==board[1][1]&&board[1][1]==board[2][0]))
            {
                printf("Player %i wins\n\n",player);
                printf("   1   2   3\n");
                printf("1| %c | %c | %c |\n",board[0][0],board[0][1],board[0][2]);
                printf("2| %c | %c | %c |\n",board[1][0],board[1][1],board[1][2]);
                printf("3| %c | %c | %c |\n",board[2][0],board[2][1],board[2][2]);
            }
            else
            {
                for(i=0;i<3;i++)
                {
                    if((board[0][i]==board[1][i] && board[1][i]==board[2][i])||
                        (board[i][0]==board[i][1] && board[i][1]==board[i][2]))
                    {
                        printf("Player %i wins\n\n",player);
                        printf("   1   2   3\n");
                        printf("1| %c | %c | %c |\n",board[0][0],board[0][1],board[0][2]);
                        printf("2| %c | %c | %c |\n",board[1][0],board[1][1],board[1][2]);
                        printf("3| %c | %c | %c |\n",board[2][0],board[2][1],board[2][2]);
                    }
                }
            }
        }
        else
        {
            printf("Invalid input!!! Please re-enter,\n");
            goto jump;
        }
    }

    return 0;
}

Upvotes: 1

Views: 85

Answers (1)

user6245072
user6245072

Reputation: 2161

||(board[0][2]==board[1][1]&&board[1][1]==board[2][0]))

This line is true even if no one entered a cross or a circle inside these cells, because

' ' == ' ' == ' '

Instead try initializing the cells with a to i characters, as in

char board[3][3] = {
    {'a', 'b', 'c'},
    {'d', 'e', 'f'},
    {'g', 'h', 'i'}
};

So that when the if-statement runs,

'c' != 'e' != 'g'

Upvotes: 1

Related Questions