0

I am trying to make a game of tic tac toe. I have declared this global array of character type.

#include<stdio.h>
static char game[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};

Then I am trying to change the values of the array elements according to the entries of the game.

void input()
{
char row[1],column[1];
char move;
printf("\nEnter Position (Row×Column) and Your Move (X or O): 
\n");
scanf(" %c %c %c",&row[0],&column[0],&move);
temp++;
replace(row,column,move);
}
void replace(char r[1],char c[1],char m)
{
int i,j;
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
        if(game[i]==r && game[j]==c)
        {
            game[i][j]=m;
            break;
        }
    }
}
}

I changed the row and column variable to arrays because I was being shown the warning that 'iso c++ forbids comparison between pointer and integer fpermissive'. But I am still unable to see a change in the desired array element in the output. What am I doing wrong?

SaphiMoon
  • 19
  • 5

1 Answers1

0

You need if (i == r && j == c) not if(game[i]==r && game[j]==c). What you're doing is comparing if the the character at game[i][j] is equal to the int r and int c. You're not checking if the players desired location (r, c) to play is (i, j)

Additionally, your break; will not actually break you out of both loops. You need to have an if statement in each for loop to check if the location has been found, so adding a variable and additional check for this in your outer loop will work.

    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            if (i == r && j == c) {
                game[i][j] = m;
                found = 1;
                break;
            }
        }
        if (found) break;
    }
codyne
  • 552
  • 1
  • 9
  • Thank you so so much. I've been at it for hours and one mistake after another kept cropping up, but now it's done. Thank you – SaphiMoon Dec 17 '21 at 16:15