dimanche 21 juin 2015

srand in C - with just one repetition

i'm trying to build a memory game and I want to ask how I can generate a randomic number with just one repetition. Like 1-1, 2-2, 3-3. I will paste here my function that I created and tell me if I have to create another function just to create a condition to create just a pair from numbers.

void preencher_mesa(int matriz[4][4], int dificuldade) //function to fulfill the table {

int i,j;
int lim_col, lim_linha; //limits of the matriz



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


if(dificuldade == 1)
{
    lim_col = 3;
    lim_linha = 2;

}
else if(dificuldade == 2)
{
    lim_col = 4;
    lim_linha = 2;
}
else if(dificuldade == 3)
{
    lim_col = 4;
    lim_linha = 4;
}

srand(time(NULL) );
for(i=0;i<lim_linha;i++)
{
    for(j=0; j<lim_col;j++)
    {
            if(dificuldade == 1)/dificulty ==1
            {
                matriz[i][j] = (rand()%3)+1;

            }
            else if(dificuldade == 2)//dificulty ==2
            {
                matriz[i][j] = (rand()%6)+1;


            }
            else if (dificuldade == 3) //dificulty ==3
            {
                matriz[i][j] = (rand()%8)+1;

            }



    }

}

 mostrar_mesa(matriz); //showtable

}




Aucun commentaire:

Enregistrer un commentaire