Tic-tac-toe in C. The program ends before

Asked

Viewed 303 times

0

I am trying to make an old game in C language but in player 1’s first move depending on where to put the 'X' the program is already ending declaring the player a winner. Following this my code:

#include <stdio.h>
#include <string.h>
int main(){
    int x, y, cont;
    char m[3][3], velha[3][3];
    for(x=0; x<3; x++){
        for(y=0; y<3; y++){
            m[x][y]='.';
        }

    }
    for(x=0; x<3; x++){
        for(y=0; y<3; y++){
            printf("%c\t", m[x][y]);
        }
        printf("\n");
    }



        while(cont<9){

        if(cont%2==0){
            printf("Jogador 1\n");
            printf("Digite qual a linha preencher ");
            scanf("%d", &x);
            printf("Digite qual coluna preencher");
            scanf("%d", &y);
            m[x][y]='X';
            velha[x][y]='X';

            if(velha[0][0]==velha[1][1] && velha[1][1]==velha[2][2]  || 
            velha[0][0]==velha[0][1]&& velha[0][1]==velha[0][2] || 
            velha[1][0]==velha[1][1] && velha[1][1]==velha[1][2] ||
             velha[2][0]==velha[2][1] && velha[2][1]==velha[2][2]||
         velha[0][0]==velha[1][0] && velha[1][0]==velha[2][0] || 
             velha[0][1]==velha[1][1] && velha[1][1]==velha[1][2] || 
            velha[0][2]==velha[1][2] && velha[1][2]==velha[2][2] || velha[0][2]==velha[1][1] && velha[1][1]==velha[3][0] ){
                printf("Jogador 1 venceu \n");
                cont=10;
            }



        }

            else if(cont%2!=0){
            printf("Jogador 2\n");
            printf("Digite qual linha preencher ");
            scanf("%d", &x);
            printf("Digite qual coluna preencher ");
            scanf("%d", &y);
            m[x][y]='0';
            velha[x][y]='0';
            if(velha[0][0]==velha[1][1] && velha[1][1]==velha[2][2]  || 
            velha[0][0]==velha[0][1]&& velha[0][1]==velha[0][2] || 
            velha[1][0]==velha[1][1] && velha[1][1]==velha[1][2] ||
             velha[2][0]==velha[2][1] && velha[2][1]==velha[2][2]||
         velha[0][0]==velha[1][0] && velha[1][0]==velha[2][0] || 
             velha[0][1]==velha[1][1] && velha[1][1]==velha[1][2] || 
            velha[0][2]==velha[1][2] && velha[1][2]==velha[2][2] || velha[0][2]==velha[1][1] && velha[1][1]==velha[3][0] ){
                printf("Jogador 2 venceu \n");
                cont=10;
            }
        }
        cont++;
        for(x=0; x<3; x++){
            for(y=0; y<3; y++){
            printf("%c\t", m[x][y]);
        }
        printf("\n");
    }
}
if(cont==9){
    printf("Nao teve vencedores ");

}












}
  • You do not start the value of cont before arriving at the while. Since you didn’t start this value, it can come with any memory junk. Do cont = 0 before you get into the loop

  • Another thing, on the board print, inside the while, it will print the size of the X as distinct lines, while the Y will be the columns. Usually it is the other way around, if you are aware of this behavior and it is desired, it is great

1 answer

3


The cont variable had not been started and its if was very wrong. I fixed it for you, just copy and paste. I also recommend doing a check function, not to get so big the condition.

#include <stdio.h>
#include <string.h>
void pedirechecar();
int main(){
    int x, y;
    int cont = 0;
    char m[3][3], velha[3][3];
    for(x=0; x<3; x++){
        for(y=0; y<3; y++){
            m[x][y]='.';
        }

    }
    for(x=0; x<3; x++){
        for(y=0; y<3; y++){
            printf("%c\t", m[x][y]);
        }
        printf("\n");
    }



        while(cont<9){

        if(cont%2==0){
            printf("Jogador 1\n");
            pedirechecar();
            m[x][y]='X';
            velha[x][y]='X';

            if((velha[0][0]=="X" && velha[1][1]=="X" && velha[2][2]=="X") || 
            (velha[0][0]=="X" && velha[0][1]=="X" && velha[0][2]=="X") || 
            (velha[1][0]=="X" && velha[1][1]=="X" && velha[1][2]=="X") ||
            (velha[2][0]=="X" && velha[2][1]=="X" && velha[2][2]=="X") ||
            (velha[0][0]=="X" && velha[1][0]=="X" && velha[2][0]=="X") || 
            (velha[0][1]=="X" && velha[1][1]=="X" && velha[1][2]=="X") || 
            (velha[0][2]=="X" && velha[1][2]=="X" && velha[2][2]=="X") || 
            (velha[0][2]=="X" && velha[1][1]=="X" && velha[3][0]=="X")){
                printf("Jogador 1 venceu \n");
                cont=10;
            }



        }

            else if(cont%2!=0){
            printf("Jogador 2\n");
            pedirechecar();
            m[x][y]='0';
            velha[x][y]='0';
         if((velha[0][0]=="X" && velha[1][1]=="X" && velha[2][2]=="X") || 
            (velha[0][0]=="X" && velha[0][1]=="X" && velha[0][2]=="X") || 
            (velha[1][0]=="X" && velha[1][1]=="X" && velha[1][2]=="X") ||
            (velha[2][0]=="X" && velha[2][1]=="X" && velha[2][2]=="X") ||
            (velha[0][0]=="X" && velha[1][0]=="X" && velha[2][0]=="X") || 
            (velha[0][1]=="X" && velha[1][1]=="X" && velha[1][2]=="X") || 
            (velha[0][2]=="X" && velha[1][2]=="X" && velha[2][2]=="X") || 
            (velha[0][2]=="X" && velha[1][1]=="X" && velha[3][0]=="X")){
                printf("Jogador 2 venceu \n");
                cont=10;
            }
        }
        cont++;
        for(x=0; x<3; x++){
            for(y=0; y<3; y++){
            printf("%c\t", m[x][y]);
        }
        printf("\n");
    }
}
if(cont==9){
    printf("Nao teve vencedores ");

}
}

void pedirechecar(){
    printf("Digite qual linha preencher ");
    scanf("%d", &x);
    printf("Digite qual coluna preencher ");
    scanf("%d", &y);
    if (velha[x][y] == "X")
    {
        printf ("Esse lugar já está marcado!");
        pedirechecar();
    }
}

To check if it is already filled you can do an if this way:

if (velha[x][y] == "X")
{
    printf ("Esse lugar já está marcado!");
}
  • It worked!! Thanks. Agr as q do to check when the player fills some space but that seat is already filled?

  • I edited my answer with the solution for this. Don’t forget to mark the answer as correct!

  • But if I put it like that, the player will lose the play later

  • I edited my answer, understand the function pedirechecar(), I don’t know if it’ll work well, I don’t know C very well.

  • It worked. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.