Memory Game

How could you refactor this function?