0 votes

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?

in Engine by (25 points)

2 Answers

+1 vote

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
by (9,736 points)
+1 vote

Use a dictionary

the algorithm is O(n)

here is a random pseudo code i did not test

https://www.geeksforgeeks.org/count-pairs-with-given-sum/

basically the same type of problem

 func matching_pairs(l):
 var m = Dictionary.new()
  for  i in l:
    if(m.has(i)):
          m.set(i,  !m.get(i))
    else:
        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.

by (429 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.