Need help with my simple memory puzzle game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RyanJZG

So I have been making small games for a week now, and I have decided to make a memory puzzle game. A game where there is 48 cards and 5 colors, and when you click one, it flips. You need to remember the color of the card when it flips. After seeing it’s color, you need to find another card with matching color. If the next one you clicked is not of the same color, the previous one is flip back. I’ve implemented everything is perfectly. Every card is being randomize to a random color. The matching system perfectly works. But after playing my game that I realize one major and obvious part of my game! Winning! Haha I was embarrassed when I played it. The problem I have is that I have to make sure that every card has a matching pair. I will change the number of the cards to 50 so that there will be 10 cards of each color. So guys, how do I do it? How do I make sure that every card has a match?

:bust_in_silhouette: Reply From: volzhs

how about this?

  1. put a color twice into array 24 times (to make 48 cards)
  2. shuffle it
  3. put it on screen according to the array
:bust_in_silhouette: Reply From: hungrymonkey

Use a dictionary

the algorithm is O(n)

here is a random pseudo code i did not test

basically the same type of problem

 func matching_pairs(l):
 var m =
  for  i in l:
          m.set(i,  !m.get(i))
        m.set(i, false)

  for k, v in m:
       if(v == false): #one card do not have a matching pairs 
          return false
  return true

note this algorithm counts 4 cards of the same as 2 matching parts. 3 card is 1 matching and 1 not matching which returns false.

Basically, use a dictionary or set or something.