Elas
Elas

Reputation: 21

a matrix not printing correctly in c

I don't know why mat[7][8]='A' prints A in the whole column. The procedure is a game grid where we should put pions of gamers and maintain all their previous pions on the grid. I don't know why changing the value of this matrix outside a for loop doesn't do anything right as it supposed to be.

Here is the code:

#include "Go_1.h"
#define DIMENSION 9
#define SPACELEFT 2

/* cette donction permet d'affichier la matrice à l'état initial, de placer le pion du joueur à
l'intersection en analysant si la place est vide ou pas, et d'afficher la grille avec les pions
des joueurs*/
void grille(char  mat[][TAILLE_MATRICE], int *x, int *y,char jeton,char *joueur,char lettres[],char nombres[])
  {
    int a=0; /* servira de compteur pour afficher tous les caracteres de A--I en utilsant le code ASCII*/
    int b=1;
    const int LIGNE_COLONNE=(SPACELEFT-1)*(DIMENSION-1)+ DIMENSION;/* = 25// nb de lignes et colonnes où
    on doir afficher les valeurs de la matrice stockées dans LIGNE_COLONNE(25) lignes et colonnes*/
    printf("\t");
    printf("  ");   /*2 spaces*/
    /*affichage des lettres délimitant le goban horizontalement*/
    for(int j=0;j<DIMENSION;j++)
    {
           printf("%c", 'A'+a);
           a++;
           for (int k=0; k<(2*SPACELEFT-1);k++)
           {
              printf(" "); /*5 spaces  5= 2*SPACELEFT-1 */
           }
    }
    printf("\n");

     /*Remplissage de la matrice*/
    for (int i=0;i<LIGNE_COLONNE;i+=SPACELEFT)
    {
        for (int j=0;j<LIGNE_COLONNE;j+=SPACELEFT)
        {
            mat[i][j]='0';
        }
    }

    mat[7][8]='A';

   /*affichage des nombres délimitant le goban verticalement*/
    for (int i=0;i<LIGNE_COLONNE;i++)  /* LIGNE_COLONNE in this case= 25*/
    {
        printf("\t");
        if (i%SPACELEFT==0)
        {
            printf("%d ",b);
            b++;
        }
        else
            printf("  ");
        for (int j=0; j<LIGNE_COLONNE;j++)
        {
            if ((i%SPACELEFT==0)&&(j%SPACELEFT==0)) /* le cas où mat[i][j] est une intersection*/
            {
               /*if (i==*x && j==*y) /*tester si les indices coincident avec ceux choisis par le joueur
                {
                  if (mat[*x][*y]=='0') /*tester si l'intersection ets vide cad contient '0'*
                  {
                      mat[*x][*y]=jeton;
                  }
                  else
                  {
                      printf("\tLa place est deja occupee!");
                      choix_pion(joueur,jeton,x,y,lettres,nombres);/*on lui demande de réexprimer
                      son choix où placer son pion
                      break;
                  }
                } */
                printf("%c ",mat[i][j]);
            }
            else if ((i%SPACELEFT==0)&&(j%SPACELEFT!=0))
                printf("* ");
            else if ((i%SPACELEFT!=0)&&(j%SPACELEFT!=0))
                printf("  ");
            else if ((i%SPACELEFT!=0)&&(j%SPACELEFT==0))
                printf("* ");

        }
        printf("\n");
    }


    return;
   }

Upvotes: 0

Views: 54

Answers (1)

C-Nut
C-Nut

Reputation: 26

Well, I refactored the code a bit and I get the following output:

A B C D E F G H I

Since I did not know how to define TAILLE_MATRICE I just assigned 64

That's the code for anyone wanting to try it out:

#include<stdio.h>

#define DIMENSION 9
#define SPACELEFT 2
#define TAILLE_MATRICE 64

int main(char  mat[][TAILLE_MATRICE], int *x, int *y,char jeton,char 
*joueur,char lettres[],char nombres[])
  {
    int a=0;
int b=1;
const int LIGNE_COLONNE=(SPACELEFT-1)*(DIMENSION-1)+ DIMENSION;
printf("\t");
printf("  ");
for(int j=0;j<DIMENSION;j++)
{
       printf("%c", 'A'+a);
       a++;
       for (int k=0; k<(2*SPACELEFT-1);k++)
       {
          printf(" "); /*5 spaces  5= 2*SPACELEFT-1 */
       }
}
printf("\n");

for (int i=0;i<LIGNE_COLONNE;i+=SPACELEFT)
{
    for (int j=0;j<LIGNE_COLONNE;j+=SPACELEFT)
    {
        mat[i][j]='0';
    }
}

mat[7][8]='A';

for (int i=0;i<LIGNE_COLONNE;i++)
{
    printf("\t");
    if (i%SPACELEFT==0)
    {
        printf("%d ",b);
        b++;
    }
    else
        printf("  ");
    for (int j=0; j<LIGNE_COLONNE;j++)
    {
        if ((i%SPACELEFT==0)&&(j%SPACELEFT==0))
        {
            printf("%c ",mat[i][j]);
        }
        else if ((i%SPACELEFT==0)&&(j%SPACELEFT!=0))
            printf("* ");
        else if ((i%SPACELEFT!=0)&&(j%SPACELEFT!=0))
            printf("  ");
        else if ((i%SPACELEFT!=0)&&(j%SPACELEFT==0))
            printf("* ");

    }
    printf("\n");
}
return 0;
}

Upvotes: 1

Related Questions