0

I have this code

    int Iminente(char tab[3][3], char comp, char jog, char str[3][3]){

    int i, j, X = 0, val;
    char col[4], diag[2][4];

    strcpy(diag[0], &tab[0][0]); // Diagonal E-D C-B (= \ )
    strcat(diag[0], &tab[1][1]);
    strcat(diag[0], &tab[2][2]);

    strcpy(diag[1], &tab[0][2]); // Diagonal D-E B-C (= / )
    strcat(diag[1], &tab[1][1]);
    strcat(diag[1], &tab[2][0]);

    for(i = 0; i < 3; i++){
      strcpy(col, &tab[0][i]); // Colunas
      strcat(col, &tab[1][i]);
      strcat(col, &tab[2][i]);

      for(j = 0; j < 3; j++){
        if(strcmp(str[j], tab[i]) == 0){ // Verifica linhas
          Jogar(tab, comp, InvPosicao(i, j));

          return 1;
        }

        if(strcmp(str[j], col) == 0){ // Verifica colunas
          Jogar(tab, comp, InvPosicao(i, j));

          return 1;
        }

        if(!X){ // Verifica diagonais
          if(strcmp(str[j], diag[0]) == 0){
            Jogar(tab, comp, InvPosicao(j, j));

            return 1;
          }else if(strcmp(str[j], diag[1]) == 0){
            val = 2 - j;
            Jogar(tab, comp, InvPosicao(val, j));

            return 1;
          }
        }
      }
      X = 1;
    }

    return 0;
  }

The error only occurs when we reach instruction return 0. I can't find where it is exactly. All I can say is that all information is given by me (not the user) and I defined the variables according to the length I predicted it would be.
This is part of the Tic-Tac-Toe game. here are the variables

tab - 3x3 table, each element is a char
comp - current computer char
jog - current player char
str - group of "strings" with 3 elements each with length 3 (null terminator not included)

i, j - iterators
X - "state" variable (not important)
val - not important
col - string with the current column
diag - group of "strings" with 2 elements each with length 4 (null terminator included)

Values:

possible values for `str`:
    char perder[3][3] = {{' ', jog, jog}, {jog, ' ', jog}, {jog, jog, ' '}};
    char ganhar[3][3] = {{' ', comp, comp}, {comp, ' ', comp}, {comp, comp, ' '}};

value for `tab`:
    char jogo[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; // Elements can be ' ', 'O', or 'X'

values for `jog` and `comp`:
    'O' or 'X'

Is it related to the strcat() and strcpy() functions?

DMaxter
  • 178
  • 5
  • 19
  • Are your arrays single characters or strings? You seem to be confused about that... – John3136 Feb 14 '18 at 00:10
  • I will add the real values to help – DMaxter Feb 14 '18 at 00:13
  • 1
    Your whole program is just wrong. `char tab[3][3]`; `strcpy(col, &tab[0][i]);` is taking the address of a single `char`, not a `char*` so it is full of undefined behavior. – John3136 Feb 14 '18 at 00:28
  • @John3136 you mean I must change the parameters in the function so that they can work as pointers? – DMaxter Feb 14 '18 at 00:29
  • @MoonWalker. What you have now is wrong. You seem confused about whether you need chars or strings (you use chars but you talk about null terminators and use string functions). My opinion? For tic-tac-toe a 3x3 array of `char` is all you need. No `char*` and therefore no `strcpy` or `strcmp` – John3136 Feb 14 '18 at 00:32
  • @John3136 i used the strings because it is easier to compare with. If i say I want to compare a line with "OOX" it is easier if it is all about strings – DMaxter Feb 14 '18 at 00:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165099/discussion-between-moonwalker-and-john3136). – DMaxter Feb 14 '18 at 00:37
  • @MoonWalker how does that make it easier for columns and diagonals? – Weather Vane Feb 14 '18 at 00:43
  • @WeatherVane It is not easier for that situation, but I tried to solve that by concatenating the values of the columns/diagonals – DMaxter Feb 14 '18 at 00:45
  • 1
    So you actually made it **harder**? – Weather Vane Feb 14 '18 at 00:48
  • @WeatherVane harder? – DMaxter Feb 14 '18 at 10:17

1 Answers1

0

As @John3136 mentioned, my code was a mess.
So, to solve the problem, I implemented a function to add a character to a string:

void AdicionaCar(char *s, char c){

  int length = strlen(s);
  s[length] = c;
  s[length+1] = '\0';

}

and replaced all instances of strcat and strcpy with this function.
Then initialized the variables diag and col with a null-terminator, so that they could become strings. Changed the formal parameters to pointers (not all of them) and now the function header looks like this:

int Iminente(char (*tab)[3], char comp, char jog, char (*str)[3])
DMaxter
  • 178
  • 5
  • 19