1

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.

Gabriel
  • 763
  • 1
  • 10
  • 28
  • Be more specific about your doubt, tell us the part of your code we should look and what happens when you try to compile. Doing this, it will be easier to help you. – Gabriel Jun 26 '15 at 00:14
  • 1
    I'm having troubles with take this numbers of vector and put this randomic on the matrix @gm_fernandes below for(i=0;i – Lucca Mello Jun 26 '15 at 00:20
  • 1
    I'm sorry, it's too hard to understand what you are asking. Maybe just a grammar issue. But (at least) these terms are not standard and it is not clear what they mean: "two a two", "pairs of [4][4]", "fulfill the matrix" (what is fulfill in this context?). Perhaps some examples might make things clearer. – kaylum Jun 26 '15 at 00:33
  • @AlanAu i want that the numbers of my vector complete the matrix with those restrictions described at the questions. example vector {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8} matrix[4][4] on the difficulty 1 I want that 3 pairs ex: {1,1}{3,3}{6,6} ,of this vector be in six positions ont this matrix[4][4] – Lucca Mello Jun 26 '15 at 00:36
  • @LuccaMello I sorry, but I'm not sure if I got your point, write in Portuguese for me, I'm also from Brazil, so maybe I can understand the situation and help you to improve your question. – Gabriel Jun 26 '15 at 00:47
  • @gm_fernandes preciso encaixar as posições que eu gerei {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,} das cartas dentro da matriz de forma aleatória. Se a matriz for dificulldade = 1, essa matriz [4][4] vai ter 6 cartas somente dessas geradas, que serão 3 pares. Pensa em um jogo de memória de cartas que vai fazer sentido, tentar achar os pares e etc – Lucca Mello Jun 26 '15 at 00:53
  • This looks like a duplicate question of yours: [`srand()` in C with just one repetition](http://stackoverflow.com/questions/30970418/). You've not gotten back to me with any more question about the answer I gave. What don't you understand? – Jonathan Leffler Jun 26 '15 at 01:00
  • @JonathanLeffler, it is not duplicate question. There I was needing to reading the vector randomly. Now I need to complete the matrix with this values. – Lucca Mello Jun 26 '15 at 01:02
  • @LuccaMello tudo bem, então se eu escolher a dificuldade 1 a matriz terá 3 pares (total de 6 números) e terá as dimensões [2][3] ? Assim como a matriz da dificuldade 2 seria [2][4]? Ou algo nesse sentido, correto? – Gabriel Jun 26 '15 at 01:03
  • OK; the relationship is very close, though. (And you could indicate that you've got the help you needed on the last one by accepting an answer.) Why are only three pairs listed for the '4 pairs' part of the question? What is there in this question that is distinctly novel compared to the previous one? You need to explain a bit more what you're up to here. – Jonathan Leffler Jun 26 '15 at 01:04
  • @gm_fernandes exatamente, mas na verdade a matriz vai ser sempre a mesma [4][4] o resto eu posso apenas completar com zeros, do que não foi usado – Lucca Mello Jun 26 '15 at 01:09
  • @LuccaMello Ok, vou tentar melhorar a sua pergunta e colocar uma seta para cima, assim o pessoal irá se interessar mais em responde-la. – Gabriel Jun 26 '15 at 01:12
  • @JonathanLeffler, I am just learning how stackoverflow works, so, sorry for that, but the question is different. The answers on the previous question, do not fitted my doubts.I forgot to list another pair on the question, it was my fault. But choose any pair of the list as an example. – Lucca Mello Jun 26 '15 at 01:15
  • @LuccaMello I had made some changes in your question, it should take some minutes to upload. Hope that it makes things crystal clear. – Gabriel Jun 26 '15 at 01:35
  • @gm_fernandes I noticed and it is approved =D thank you so much for clarify the question for this community – Lucca Mello Jun 26 '15 at 01:37

1 Answers1

1

Consider DIFFICULTY LEVEL 1: You first desire to determine a random selection of three numbers from the set {1,2,3,4,5,6,7,8}, and then given a pair each of those three numbers desire to randomly place them in a 4x4 matric.

The number of ways in which 3 numbers an be chosen from 8 is known as 3 chosen from 8 and is evaluated as

8! / (3! * (8-3)!)

which reduces to

8 * 7 * 6 / (3 * 2 * 1) = 56

Therefore the pairs of digits for DIFFICULTY LEVEL 1 an be determined by enumerating these 56 possibilities and making a random selection from the integers from 1 to 56.

Now given 3 pairs of numbers, one wishes to randomly place them in a 4x4 matrix. There are:

  • 16 * 15 / 2 = 120 ways to place the first pair;
  • 14 * 13 / 2 = 91 ways to place the second pair;
  • 12 * 11 / 2 = 66 ways to place the third pair; and
  • 3 * 2 * 1 = 6 ways to select the order of the three pairs

so this means there are 120 * 91 * 66 / 6 = 120,120 ways to place the three pairs in an empty 4x4 matrix. Again, by determining an enumeration of these 120,120 possibilities and making a random selection from the integers from 1 to 120,120 one can complete the matrix determination.

Likewise for the other Difficulty Levels.

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52