Home > Back-end >  Formula To have 2D array index Definite To Replace The Value Inside the Array (Tic-Tac-Toe) in C
Formula To have 2D array index Definite To Replace The Value Inside the Array (Tic-Tac-Toe) in C

Time:11-28

So I am writing a game of Tic-tac-toe for 2 player in C but I don't know what formula to input so when I print the array a second time the The 'X' or the 'O' replaced the Number that were there before here is the program :

 #include <stdio.h>

void main(){

char plateau[3][3]={
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'}
};
int t=0;
char choix1;
char choix2;
int i , j , k;

printf("-------------------------------------------------------------------\n");

for(t=0;t<9;t  ){
    if(t%2==0){
        for(int i=0;i<3;i  ){
        for(int j=0;j<3;j  ){
            printf(" %c ",plateau[i][j] );
            }
            printf("\n");
        }
            
            printf("Turn > Player n1 what case do you want to play?");
            scanf(" %c",&choix1);
            printf("\n");
            int i=;
            int j=;
            plateau[i][j]='X';
        }
    
    if(t%2!=0){
            for(int i=0;i<3;i  ){
            for(int j=0;j<3;j  ){
                printf(" %c ",plateau[i][j] );
                }
            printf("\n");
        }
        printf("\nTurn > Player n2 what case do you want to play?");
        scanf(" %c",&choix2);
        printf("\n");
        }
}

}

so this

 int i=;
                int j=;
                plateau[i][j]='X';
            }

Like i need a formula that for int i if choix1 is {1||2||3} then i would be 0 , if choix1 is {4||5||6} then it would be 1 . Same thing with j .

Here is like what it should look like (mind that i predetermined where X was gonne be it is not from the input) :

  1  2  3
 4  5  6
 7  8  9
Turn > Player n1 what case do you want to play?1

 1  2  3
 4  5  6
 X  8  9

Turn > Player n2 what case do you want to play?^C

I tried to do it with if statement but with no success here is my attempt:

printf("Turn > Player n1 what case do you want to play?");
            scanf(" %c",&choix1);
            printf("\n");

            if(choix1 ==1||choix1==2||choix1==3){
            int i=0;}

            if(choix1 ==4||choix1==5|choix1==6){
            int i=1;}

            if(choix1 ==7||choix1==8||choix1==9){
            int i=2;}

            if(choix1 ==1||choix1==4||choix1==7){
            int j=0;}

            if(choix1 ==2||choix1==5|choix1==8){
            int j=1;}

            if(choix1 ==3||choix1==6||choix1==9){
            int j=2;}

            plateau[i][j]='X';
        }

If someone could help me or oriented me in a way to find answer it will be gladly appreciated.

CodePudding user response:

Your attempt is pretty close to working Your main problem is that you are writing int extra times.

When you write int i=0;, that is a definition of a new variable named i that will only be visible inside the block where it was defined.

If you want to change the value of an existing variable (also called "assigning" a value to the variable), just do this:

i = 0;

Also, I highly recommend cleaning up your indentation and whitespace.

After you take this advice, your code would look something like:

int i = 0, j = 0;

if (choix1 == 1 || choix1 == 2 || choix1 == 3) {
  i = 0;
}
...
plateau[i][j] = 'X';

Your big list of if statements should work fine. But if you want help simplifying it after you get it working, you could ask on Code Review.

  •  Tags:  
  • c
  • Related