Le Rui
Le Rui

Reputation: 3

Can't print from a matrix

Trying to make a tictactoe game, I'm generally new to programming, and keep getting SIGBUS while printing the game map on the console

void draw(MATRIX m){/**
 *  x | o | x            
  ____|___|_____              
 *    | o |                
  ____|___|_____                
      |   |     
      | o |                       
 */
char inttox[3];
inttox[0]=' ';inttox[1]='x';inttox[2]='o';
printf("  %c | %c | %c   \n____|___|_____\n  %c | %c | %c   \n____|___|_____\n    |   |     \n  %c | %c | %c   \n",
inttox[m.im[0][2]],inttox[m.im[1][2]],inttox[m.im[2][2]]
,inttox[m.im[0][1]],inttox[m.im[1][1]],inttox[m.im[2][1]]
,inttox[m.im[0][0]],inttox[m.im[1][0]],inttox[m.im[2][0]]);
}

When I run gdb I get

    Player 1's turn: 1 0

Program received signal SIGBUS, Bus error.
0x000055555555528b in draw (m=...) at galo.c:39
39      ,inttox[m.im[0][0]],inttox[m.im[1][0]],inttox[m.im[2][0]]);

inttox is a vector with 3 positions, which the index is players input, that goes from 0-no one played here, 1/2- respective player played here, and translates that to the char that is drawn on the map.

m is the matrix that contains the vector, since I found it easier to understand than to deal with how to correctly implement a string matrix.

The full code is here:

#include<stdio.h>

typedef struct {
    int im[3][3];
}MATRIX;

int logic(int p, MATRIX m){
    if(
    (m.im[0][0])==(m.im[1][1])==(m.im[2][2])||//diagonals
    (m.im[0][2])==(m.im[1][1])==(m.im[2][0])||

    (m.im[0][0])==(m.im[0][1])==(m.im[0][2])||//columns
    (m.im[1][0])==(m.im[1][1])==(m.im[1][2])||
    (m.im[2][0])==(m.im[2][1])==(m.im[2][2])||

    (m.im[0][0])==(m.im[1][0])==(m.im[2][0])||//lines
    (m.im[0][1])==(m.im[1][1])==(m.im[2][1])||
    (m.im[0][2])==(m.im[1][2])==(m.im[2][2])){
        return 1;
    }
    
    p++;
    if(p>2)p=1;
    return 0;
}
void draw(MATRIX m){/**
 *  x | o | x            
  ____|___|_____              
 *    | o |                
  ____|___|_____                
      |   |     
      | o |                       
 */
char inttox[3];
inttox[0]=' ';inttox[1]='x';inttox[2]='o';
printf("  %c | %c | %c   \n____|___|_____\n  %c | %c | %c   \n____|___|_____\n    |   |     \n  %c | %c | %c   \n",
inttox[m.im[0][2]],inttox[m.im[1][2]],inttox[m.im[2][2]]
,inttox[m.im[0][1]],inttox[m.im[1][1]],inttox[m.im[2][1]]
,inttox[m.im[0][0]],inttox[m.im[1][0]],inttox[m.im[2][0]]);
}
MATRIX input(int p,MATRIX m){
    if(p>2||p<1){
        printf("###player invalid###");
    }
    int a,b;
    printf("Player %d's turn: ",p);
    scanf("%d %d", &a, &b);
    if(a>2||b>2){
        printf("Insert a correct position (0-2, 0-2)");
        return input(p,m);
    }
    m.im[a][b]=p;
    return m;
}

int main(){
    MATRIX matriz;
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            matriz.im[i][j]=0;
        }
    }
    int p=1;
    draw(matriz);
    matriz=input(p,matriz);
    logic(p,matriz);
}

Upvotes: 0

Views: 41

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You have 3 positions, but you initialized only 2 positions here:

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            matriz.im[i][j]=0;
        }
    }

The variable matriz is declared as non-static local variable, so its value is indeterminated until being initialized.

Initialize all elements like this to fix:

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            matriz.im[i][j]=0;
        }
    }

Upvotes: 1

Related Questions