2016-11-02 8 views
2

Ich habe ein Memory-Spiel mit Windows-Form (C#) erstellt, ist das Spiel abgeschlossen, aber ich habe Schwierigkeiten, den letzten Teil hinzufügen, die, wenn alle Karten übereinstimmen ein Meldungsfeld muss dem Benutzer z "Gut gemacht! Alle Karten wurden abgestimmt".C# Memory-Spiel, muss am Ende des Spiels Nachricht hinzufügen

ist hier ein Abschnitt des Codes, wo ich den Code für MessageBox.Show denken, wird eingefügt:

 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(); 
      } 
     } 
    } 

Diese 2 sind die ersten zwei von insgesamt 18 Karten, aber das ist der Code, der für alle ist sie nur Properties.Resources.Image1 wird von Image.1 zu Image.2, .3, .4 usw. geändert.

Ich bin mir nicht sicher, welchen Code es mir erlauben wird, das Spiel eine Nachricht alle 9 einmal anzuzeigen (insgesamt 18 Karten) Paar Karten sind abgestimmt.

Jede Hilfe wird sehr geschätzt.

+0

Ich sehe nicht, warum zwei Methoden notwendig sind. Ich würde nur eine Methode erstellen und diese zu allen Bildern hinzufügen. –

+0

Ich verstehe Ihre Frage nicht, aber ich denke, Sie möchten wissen, wann alle Karten gepaart wurden. Sie können das scoreSheet jedes Mal überprüfen, wenn ein Zug gemacht wird. wenn es 10 Kartenpaare gibt, wenn die Punktzahl 100 ist, weißt du, dass alle gepaart wurden ... – Pikoh

+0

'else if (pendingImage1! = null' <-' pendingImage1' ist immer nicht-null hier –

Antwort

0

nicht sicher, ob dies der „beste“ Weg, dies zu handhaben, aber Sie können den Überblick über die Gesamtzahl der Karten zu halten, die angepasst wurden, und die Anzahl der Spiele erforderlich gewinnen. Erstellen Sie eine globale Variable namens "winCount" und eine andere Variable namens "currentMatches". winCount kann im Code manuell auf 9 int winCount = 9 gesetzt werden und das Spiel zeigt das Popup "You Win" an, wenn currentMatches == winCount.

Zum Beispiel:

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(); 
     } 
    } 
} 
+0

thanks that worked .. .. ich habe versucht, es zu tun, aber es in alle dupCards, aber nichts passierte, dann habe ich es einfach in alle 18 Karten eingefügt und es hat funktioniert! –

+0

Awesome! Sie können wahrscheinlich den Code auf eine ganze Methode umgestalten, aber wenn Sie werden alle Ihre Variablen jedes Mal zurückgesetzt, wenn Sie eine Karte umdrehen, müssen Sie die Werte speichern, die Sie nicht zurücksetzen möchten, außerhalb der Methoden :) – Rinktacular

+0

@ Mr.Ak und vergessen Sie nicht, richtige Antworten zu stimmen! : D – Rinktacular

0

Sie müssen die Übereinstimmungen zählen. Jedes Mal, wenn der Spieler zwei Bilder gepaart hat, sollte ein Übereinstimmungszähler erhöht werden.

würde ich es diesen Code setzen hier:

if (pendingImage1.Tag == pendingImage2.Tag) 
{ 
    /* your existing code */ 

    matches++; 

    if (matches == 9) 
    { 
     MessageBox.Show("Congratulations. You've successfully paired all the images."); 
    } 
} 

Teil.

Vielleicht ist dies nicht ein Copy-Paste-Code für Sie, aber ich hoffe, Sie bekommen die Idee. :)

0

Versuchen Sie, diese

using System.Windows.Forms; 
    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); 
      int TotalPoints = Convert.ToInt32(scoreSheet.Text); 
      if(TotalPoints >= 100){ 
        MessageBox.Show("Your message"); 
      } 
     } 
     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); 
      int TotalPoints = Convert.ToInt32(scoreSheet.Text); 
      if(TotalPoints >= 100){ 
        MessageBox.Show("Your message"); 
      } 
     } 
     else 
     { 
      flipDuration.Start(); 
     } 
    } 
} 
Verwandte Themen