BINGO game in C , Using matrix with 3 dimensions?

Asked

Viewed 77 times

0

How can I generate some (detail: plural) random cards with numbers, but need to manipulate them as a three-dimensional matrix. Cartela[players] [n] [n] .. n = The dimension

My program generates only one card, how can I generate more cards ? The chart index will be defined in the players variable, which is the first dimension of the Matrix ..

#include<stdio.h>
#include<stdlib.h>
int n, soma;
int jogadores;
int menu;
int main () 
{
srand(123);

printf("Número de jogadores: \n");
do
{
scanf("%d",&jogadores);

} while (jogadores < 2 || jogadores > 10);


printf("Numero de jogadores salvo \n Escolha a dimensao das cartelas: \n");
do
{
scanf("%d",&n);

} while (n < 2 || n > 9);

printf("\n Dimensao das cartelas salva \n ");

//printf(" %d %d \n ",jogadores,n);

int value = 10*n;

 int cartela[n][n]; // I need to add the dimensiona _jogadores_ ==> cartela [jogadores][n][n]

//for (int q = 0; q <=jogadores;q++) // Loop number of players  
//{
  for(int i = 0; i <= n; i++) // Loop lines of the card
  {
    for (int j = 0; j < n; j++) // Loop rows of the card
    {
      do
        {
           soma = 0;
           cartela[i][j] = rand()%value; // Colocar a dimensão jogadores

       
           for (int l = 0; l < n; l++)
            {
              for (int c = 0; c < n; c++)
              {
                if(cartela[i][j] == cartela[l][c] && (i!= l && j!= c))
                {
                    soma++;
                }
              }
            
            }
        
        } while (soma != 0);  
        
     }
  }


  // for (int  j = 0; j < jogadores; j++)
  //{
    for (int l = 0;l<n;l++)
      {
    for(int c=0;c<n;c++)
    {
      printf("\t %d",cartela[l][c]);
     }
       printf("\n");
     }
  //} 




 //while(1)
//{




//}


return 0;
}
  • From what I understand in place of int cartela[n][n]; utilize int cartela[jogadores][n][n]; and in each reference to cartela use 3 indexes, the first being the player.

1 answer

1


There is no other way but to dynamically allocate space to n cards you may have. As the number of players and the size of the card are variable, you will allocate this space using malloc and then you can manipulate it in any way you see fit by structurally speaking.

Example:

void * Data;
int DataSize;

DataSize = jogadores * (n^2) * sizeof(int);
Data = malloc(DataSize);

Now you have allocated the size of the three-dimensional matrix you need, and you can access it with:

Data[jogador][i][j]

Browser other questions tagged

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