Compare char in a matrix function

Asked

Viewed 424 times

1

I’m playing an old game and I need to compare the positions if they’re equal to check the winner, but it’s not working.

Here is the code:

#include <stdio.h>
#include <string.h>


void exibir();
int checaLocal(char matriz[3][3], int linha, int coluna);
void checarGanhador(char matriz[3][3], char caractere);


int main() {

int i, j;
char matriz[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
printf("Programa Jogo da Velha.\n\n");
exibir(matriz);
int vez = 0;

for(i=0; i<3; i++) {
    for(j=0; j<3; j++) {
        printf("Entre Linha: ");
        scanf("%d", &i);
        printf("Entre Coluna: ");
        scanf("%d", &j);
        vez++;
        if(matriz[i][j] != ' ' || validaLocal(matriz[3][3], i, j)) {
            printf("Posicao Invalido ou Ocupada\n");
            continue;
        }
        if(vez % 2 == 0) {
            matriz[i][j] = 'X';

        } else {
            matriz[i][j] = 'O';
        }
        exibir(matriz);

    }
}
exibir(matriz);


return 0;
}

void exibir(char matriz[3][3]) {

//EXIBE JOGO DA VELHA FORMATADO
   printf("\n");
   printf("%c  | %c | %c \n", matriz[0][0], matriz[0][1], matriz[0][2]);
   printf("---|---|---\n");
   printf("%c  | %c | %c \n", matriz[1][0],matriz[1][1], matriz[1][2]);
   printf("---|---|---\n ");
   printf("%c | %c | %c \n", matriz[2][0], matriz[2][1], matriz[2][2]);
   printf("\n\n");

}

int validaLocal(char matriz[3][3], int linha, int coluna) {
//Checa se o local a ser inserido e valido
    if(linha < 0 || linha > 2 || coluna < 0 || coluna > 2) {
        return 1; //Erro
    }
    else {
    return 0; //tudo ok
    }


}

I tried to compare like this in a function

void verificaGanhador(matriz[3][3], char o) {
if(matriz[0][0] && matriz[0][1] && matriz[0][2] == o)

to compare the first line, but return an error.

  • I don’t understand the void verificaGanhador. If your function is void, she will not return anything? How will you notify whether you are a winner or not? You should not return a boolean?

  • Yes it is the doubt, should return a value.

2 answers

1

if(matriz[0][0] && matriz[0][1] && matriz[0][2] == o)

matrix[0][0] verifies that returns null

matrix[0][1] verifies that returns null

matrix[0][2] == Should not be matrix[0][2] ='O';

/* não deveria ser algo mais ou menos assim? */
if(matriz[0][0] == 'O' && 
   matriz[0][1] == 'O' && 
   matriz[0][2] == 'O')

Solved the problem?

0

I managed to resolve, Rodgger I wanted to implement in a function to check, so it doesn’t work out of it, and I would have to rewrite it 2 times.

int  verificaGanhador(matriz[3][3], char *o) {
if(matriz[0][0] && matriz[0][1] && matriz[0][2] == *o)
    return 1 
else
    return 0
}

char op1 = 'X';

 if(vez % 2 == 0) {
            matriz[i][j] = op1;
           if(verificaGanhador(matriz, &op1));
           printf("Ganhou.\n");

        } 

So using pointer and passing reference worked.

Browser other questions tagged

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