jeudi 25 juin 2015

How to comple the Matrix[4][4] randomly with the numbers I already have?

I am with a doubt about the memory game that I need to create. I made a fixed matrix of the size [4][4]. The number of cards have to be increased according to the difficulty the user had choosen. So here is an example of the logic:

In DIFFICULTY 1 -> There will only be 3 equal pairs into the matrix like:{1,1}{4,4}{5,5}, these numbers have to be randomized but in pairs, so after that I can complete the matrix.

DIFFICULTY 2 -> There will only be 3 equal pairs into the matrix - {1,1}{4,4}{5,5}{3,3} - and again it need to me random numbers in pairs, so after that I can complete the matrix.

DIFFICULTY 3 -> complete the matrix, of the size [4][4], with pair of numbers randomly.

Probably you should take a look at the

for(i=0;i<lim_linha;i++)

my code:

void preencher_mesa(int matriz[4][4], int dificuldade)

{

    int i,j;
    int cartas[7][1]; //cards
    int cont =1; //count
    int lim_col, lim_linha; //limit_colunm //limit_line

    for(i=0; i<4; i++)
        for(j=0;j<4;j++)
            matriz[i][j] = 0;

    if(dificuldade == 1)   //difficulty == 1
    {
        lim_col = 3;                       //estabelecendo limites para que não ultrapaasse o valor da mesa
        lim_linha = 2;
    }
    else if(dificuldade == 2)
    {
        lim_col = 4;
        lim_linha = 2;
    }
    else if(dificuldade == 3)
    {
        lim_col = 4;
        lim_linha = 4;
    }

    for(i=0;i<lim_linha;i++)                        //setando os numeros aleatórios para dentro das
    {                                               // matrizes de acordo com o nivel de dificuldade
        for(j=0; j<lim_col;j++)
        {
                if(dificuldade == 1)
                {

                      int aux=0;
                       while(cont>=1 && cont <=8)
                    {
                        cartas[i][0] = cont;
                        cartas[i][1] = cartas[i][0];                                        
                        cont++;
                        printf("[%d]\n",(rand()%1 +(cartas[i][0])));
                        printf("[%d]\n",(rand()%1 +(cartas[i][1])));
                    }
                 }
             }
}

//Gerando numeros aleatórios em pares por isso [0] e [1]
//sorting numbers in pairs to stabilish the numbers of cards 
//{1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8}

PS: the range of random number should be 1-8 and I cannot have two, or more, same pairs of numbers.




Aucun commentaire:

Enregistrer un commentaire