Title: c# how to make matching cards stay turned on grid? (memory game)
Tags: c#;wpf
Question: I have a problem with my matching/memory game that i'm making for a school project.
I want two of the same cards to stay after they're clicked, and when two different cards are clicked they will return to the backside image of the card.

I'm a total beginner so if someone could explain this in a really easy way to me, that would help me a lot. Currently i'm stuck on this. 

This is my code thus far for loading the images from a list and placing them on the grid:

```private void AddImage()
    {

        List<ImageSource&gt; images = GetImagesList();

        Random random = new Random();


        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                Image ImageOnBacksideOfCard = new Image();
                ImageOnBacksideOfCard.Source = new BitmapImage(new Uri("project/achterkant.png", UriKind.Relative));

                //Ruimte tussen kaartjes
                Thickness margin = ImageOnBacksideOfCard.Margin;
                margin.Top = 10;
                ImageOnBacksideOfCard.Margin = margin;

                ImageOnBacksideOfCard.MouseDown += new MouseButtonEventHandler(CardClick);

                //Randomize

                positie1 = random.Next(images.Count);
                ImageOnBacksideOfCard.Tag = images[positie1];
                images.RemoveAt(positie1);

                Grid.SetColumn(ImageOnBacksideOfCard, col);
                Grid.SetRow(ImageOnBacksideOfCard, row);
                grid.Children.Add(ImageOnBacksideOfCard);


            }
        }


//Load pictures
private List<ImageSource&gt; GetImagesList()
    {
        List<ImageSource&gt; images = new List<ImageSource&gt;();

        for (int i = 0; i < 16; i++)
        {
            int imageNr = i % 8 + 1;
            ImageSource source = new BitmapImage(new Uri("project/" + imageNr + ".jpg", UriKind.Relative));
            images.Add(source);
        }
        return images;
    }

 //Cards turn on click
    private void CardClick(object sender, MouseButtonEventArgs e)
    {
        Image card = (Image)sender;
        ImageSource front = (ImageSource)card.Tag;
        card.Source = front;
    }



    //Reset
    public void Reset()
    {
        grid.Children.Clear();
        AddImage();
    }
```

Comment: https://github.com/Tosker/MemoryGame see, how other people do it and then write your own.

Comment: Putting aside the fact that this is code behind and not MVVM, from the code you provided all I see is the initial grid generation and no attempt at solving the issue. Do you have some more code (mouse click events turning a card over, dictionary keeping track of which cards have been turned, comparison function determining whether the cards are a match) where you can specify what exactly your problem is, or do you need general guidelines to how to approach implementing such an application?

Comment: WPF/UWP and XAML were designed with the MVVM pattern in mind. While you can use other approaches, doing so misses about 90% of it's power and runs into problems at every other turn. That does not look remotely like MVVM. If it was MVVM, you had a randomized array of elements. And each of them had a boolean "revealed" property. They would also have a Command "Reveal", wich would flip that bool. And the bool would stay on true if all the cards of that group ahve been "matched". The game ends when all card groupds are matched or all cards are revealed (depending on wich you prefer).



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:10/17/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><wpf>', 'Title': 'c# how to make matching cards stay turned on grid? (memory game)', 'ViewCount': 708, 'id': 52855485, 'user_AboutMe': None, 'user_AccountId': 14562997.0, 'user_DisplayName': 'Jorn', 'user_Reputation': 11.0, 'user_Views': 2.0, 'user_id': 10518565.0}}