[Math] How many turns, on average, does it take for a perfect player to win Concentration

card-gamesprobability

Let's say a person with perfect memory is playing the card-matching game Concentration. He/she randomly lays out 2n cards (ie. n card pairs) sorted randomly and repeatedly takes turns consisting of flipping over two cards, without looking at the first card. When the cards match they are removed from play. When cards do not match, they are turned back over. The player wins when all pairs are found and all cards are removed.

A perfect game is played in just n turns, but even a perfect player (one with perfect memory) couldn't hope to come close to a perfect game as there would still be a lot of guessing required.

How to you calculate the expected number of turns required to win for player with perfect memory?


EDIT 2 — clarifying the rules

Since all the early answers seem to read this question as you CANNOT look at the first card, I'm editing that into the rules.

I originally was thinking you CAN look at the first card (since that's how I'd learned the game and, to my knowledge, that is how it is usually played.

For that rule adjustment, I've posted a new question here: How many turns, on average, does it take for a perfect player to win Concentration (adjusted)?


EDIT — Checking answers against a simulation

The strategy proposed in AlgorithmsX's answer seems to make sense to me – that is, the game takes place in two distinct stages.

Stage 1: flip all cards to learn where they are, and…

Stage 2: clean up/perfectly find all matches (since you know where they are).

Thus improving on a $2n$ turn game is pure chance; how many matches do you find in Stage 1?

To check a given answer, I wrote the below code to simulate the problem. It just randomly shuffles an array with pairs from $0$ to $n-1$ and checks for adjacent pairs that would come up in the sweep during Stage 1 (so a 0-1 pair is a match, but 1-2 is not). This seems like a good way to check answers because (for me personally, at least) it's easier to reason about the simulation than the math.

Currently, AlgorithmsX's answer for $n=3$ results in $5.6$, but I would expect it to be $5.4$ barring any errors in my simulation.

Some simulated results (1 million trials)

n | 2 | 3 | 4 | 5 | 6 |

turns | 3.33 | 5.40 | 7.43 | 9.44 | 11.45 |

Code to simulate answer given $n$

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var simulation = function(n, nTrials){

  // make array from 0 to n-1 with pairs
  var cards = Array.apply(null, {length: n}).map(Number.call, Number);
  cards = cards.concat(cards);

  var totalTurns = 0; 
  var trialsLeft = nTrials;

  while(trialsLeft>0){
    cards = shuffle(cards)
    var matches = 0;
    for(var i=0; i<n; i++){
      if(cards[2*i]===cards[2*i+1]){
        matches++
      }
    }
    totalTurns += 2*n-matches;
    trialsLeft--
  }
  return totalTurns/nTrials;
}

Best Answer

For $n=2$ there is a better strategy. Flip two cards. If they match ($p=\frac 13$) you are done in two turns. If they don't match, flip one old and one new card. The improvement comes because these might match and if not, you know where all the cards are and are done in four turns. Now you have $\frac 13$ chance of being done in three turns, for an average of $3$. This carries over to longer games. Clearly the last turn in the discovery phase you should turn one of the unmatched old cards and one new card. You will match with probability $\frac 12$. If there are no unmatched old cards, turn the two new ones and they will match.

You can write a recurrence. Define $N(n,k)$ as the expected number of flips to deal with $n$ pairs when you know the locations of $k$ unmatched cards. From each flip you have the outcomes: they match, neither matches a known card, one matches a known card, both match known cards. I believe the best strategy is to flip two unkowns until there are only two, then flip one unknown and one known.

Related Question