Mr.Ak Mr.Ak - 1 month ago 14
C# Question

c# memory game, need to add message at the end of game

I have created a memory game using windows form (c#), the game is complete but I am having difficulty adding the last part, which that when all cards are matched a message box need to show to the user e.g. "Well Done! all cards have been matched".

Here is a section of the code where I think the code for

MessageBox.Show
will be inserted:

private void card1_Click(object sender, EventArgs e)
//if the first slot of pendingImages is available put this card there for comparison
{
//turn card over
card1.Image = Properties.Resources.Image1;
//if this is the first card to be turned over, save its image
if (pendingImage1 == null)
{
pendingImage1 = card1;
}
//else check if pendingImage 2 is available then store the card here for comparison
else if(pendingImage1 != null && pendingImage2 == null)
{
pendingImage2 = card1;
}
//if both pendingImage slots are filled then compare the cards
if (pendingImage1 != null && pendingImage2 != null)
{
if (pendingImage1.Tag == pendingImage2.Tag)
{
//clear the variables to be used again
pendingImage1 = null;
pendingImage2 = null;
//once the cards are matched and turned permanentaly, disable the card to make it unclickable
card1.Enabled = false;
dupCard1.Enabled = false;
//add 10 points to the score evry time cards match
scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);
}
else
{
flipDuration.Start();
}
}
}

private void dupCard1_Click(object sender, EventArgs e)
{
dupCard1.Image = Properties.Resources.Image1;
if (pendingImage1 == null)
{
pendingImage1 = dupCard1;
}
else if (pendingImage1 != null && pendingImage2 == null)
{
pendingImage2 = dupCard1;
}
if (pendingImage1 != null && pendingImage2 != null)
{
if (pendingImage1.Tag == pendingImage2.Tag)
{
pendingImage1 = null;
pendingImage2 = null;
card1.Enabled = false;
dupCard1.Enabled = false;
scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);
}
else
{
flipDuration.Start();
}
}
}


These 2 are the 1st 2 of total 18 cards, but this is the code which is for all of them only Properties.Resources.Image1 is changed from Image.1 to Image.2, .3, .4 etc.

I'm not sure what code will allow me to have the game display a message box once all 9 (total 18 cards) pair of cards are matched.

Any help will be much appreciated.

Answer

Not sure if this is the "best" way to handle this, but you can keep track of the total number of cards that have been matched and the number of matches required to win. Create a global variable say, called "winCount" and another variable called "currentMatches" winCount can be manually set in the code to be 9 int winCount = 9 and the game displays the "You Win" pop up when currentMatches == winCount.

For example:

int winCount = 9;
int currentMatches = 0;

private void card1_Click(object sender, EventArgs e)
     //if the first slot of pendingImages is available put this card there for comparison
{
    //turn card over
    card1.Image = Properties.Resources.Image1;
   //if this is the first card to be turned over, save its image 
    if (pendingImage1 == null)
    {
        pendingImage1 = card1;
    }
    //else check if pendingImage 2 is available then store the card here for comparison
    else if(pendingImage1 != null && pendingImage2 == null)
    {
        pendingImage2 = card1;
    }
    //if both pendingImage slots are filled then compare the cards
    if (pendingImage1 != null && pendingImage2 != null)
    {
        if (pendingImage1.Tag == pendingImage2.Tag)
        {
            //clear the variables to be used again
            pendingImage1 = null;
            pendingImage2 = null;
            //once the cards are matched and turned permanentaly, disable the card to make it unclickable
            card1.Enabled = false;
            dupCard1.Enabled = false;
            //add 10 points to the score evry time cards match
            scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);

            //NEW CODE
            currentMatches ++;
            if(currentMatches == winCount)
            {
                 MessageBox.Show("Congratulations! You Win!)
                 return;
            }
        }            
        else
        {
            flipDuration.Start();
        }     
    }
}

private void dupCard1_Click(object sender, EventArgs e)
{
    dupCard1.Image = Properties.Resources.Image1;
    if (pendingImage1 == null)
    {
        pendingImage1 = dupCard1;
    }
    else if (pendingImage1 != null && pendingImage2 == null)
    {
        pendingImage2 = dupCard1;
    }
    if (pendingImage1 != null && pendingImage2 != null)
    {
        if (pendingImage1.Tag == pendingImage2.Tag)
        {
            pendingImage1 = null;
            pendingImage2 = null;
            card1.Enabled = false;
            dupCard1.Enabled = false;
            scoreSheet.Text = Convert.ToString(Convert.ToInt32(scoreSheet.Text) + 10);
        }
        else
        {
            flipDuration.Start();
        }
    }
}