samedi 28 janvier 2023

how to get my for loop to stop numbers from repeating more than twice, c++ [duplicate]

I made a card game where its a matching game using 6 cards.the cards are hard coded in place and I have the values set to random between the 6 cards, Im not sure how to make it so it displays 2 of each of the cards for the game. Right now when I run it, the 12 cards pop up all with random values, sometimes repeating multiple times. Any help would be appreciated.

 void randomize(int arr[], int size)
 {
 srand(time(0));
 for (int n = 0; n < size; n++)
     {
 int random = rand() % 6;
 arr[n] = random;
     }

I'm thinking i'll need a counter variable and maybe adding a while statement so that when counter >= 2 it won't repeat, but im not advanced enough to figure out how to write the logic of it.

 void main ()
 {
 const unsigned int size = 12; 
 int cardValues[size];
 randomize(cardValues, size);
 }

I'm trying to write a for loop with a nested while statement, and using a counter variable to keep track of how many times a number is used and set it <= 2 but I'm not sure how to write it out.




Aucun commentaire:

Enregistrer un commentaire