2016-03-30 6 views
0

私はschoolproject用のメモリゲームを作成しています。プレーヤーがゲームに勝ったときにメッセージボックスを表示するという問題があります。 "誰にも解決策がありますか?コードのC#すべてのpictureboxに値があると警告を発する方法

一方の部分:

private void Card1_Click(object sender, EventArgs e) 
{ 
    Card1.Image = Properties.Resources.car1; 
    if(usedCard1 == null) 
    { 
     usedCard1 = Card1; 
    } 
    else if(usedCard1 != null && usedCard2 == null) 
    { 
     usedCard2 = Card1; 
    } 
    if(usedCard1 != null && usedCard2 != null) 
    { 
     if(usedCard1.Tag == usedCard2.Tag) 
     { 
      usedCard1 = null; 
      usedCard2 = null; 
      Card1.Enabled = false; 
      Dupcard1.Enabled = false; 
      points = Convert.ToInt32(ScoreCounter.Text); 
      points = points + 10; 
      ScoreCounter.Text = Convert.ToString(points); 
     } 
     else 
     { 
      points = Convert.ToInt32(ScoreCounter.Text); 
      points = points - 10; 
      ScoreCounter.Text = Convert.ToString(points); 
      timer4.Start(); 
     } 
    } 
} 

private void Dupcard1_Click(object sender, EventArgs e) 
{ 
    Dupcard1.Image = Properties.Resources.car1; 
    if (usedCard1 == null) 
    { 
     usedCard1 = Dupcard1; 
    } 
    else if (usedCard1 != null && usedCard2 == null) 
    { 
     usedCard2 = Dupcard1; 
    } 
    if (usedCard1 != null && usedCard2 != null) 
    { 
     if (usedCard1.Tag == usedCard2.Tag) 
     { 
       usedCard1 = null; 
       usedCard2 = null; 
       Card1.Enabled = false; 
       Dupcard1.Enabled = false; 
       points = Convert.ToInt32(ScoreCounter.Text); 
       points = points + 10; 
       ScoreCounter.Text = Convert.ToString(points); 
     } 
     else 
     { 
       points = Convert.ToInt32(ScoreCounter.Text); 
       points = points - 10; 
       ScoreCounter.Text = Convert.ToString(points); 
       timer4.Start(); 
     } 
    } 
} 

private void Win() 
{ 
    foreach(PictureBox picture in cardsHolder.Controls) 
    { 
     if(picture != null) 
     { 

     } 
    } 
    MessageBox.Show("You've matched all cards", "Congratulations"); 
    Close(); 
} 

PS:cardsHolderパネルの名前であり、勝利プレイヤーがゲームに勝利した場合、メッセージをトリガする方法です。

+0

どのカードが一致したかをどのように把握していますか? – Jens

+0

@Robinあなたが必要とするものをよりよく理解できるように、ゲームのルールを投稿できますか? –

答えて

1

PictureBoxに画像を表示しないと、PictureBox.Imageプロパティをnullに設定したとします。だから、単純にそのプロパティを確認してください。そのうちの一つは、何のImageを持っていない場合

private void Win() 
{ 
    if (cardsHolder.Controls.OfType<PictureBox>().Any(pb => pb.Image == null)) 
     return; 

    MessageBox.Show("You matched all the icons!", "Congratulations"); 
    Close(); 
} 

OfType<PictureBox>Controls収集とAny戻りtrueからすべてのPictureBoxのESを選択します。

+0

残念ながら、画像をマスクするために他の画像を追加しました。 –

+0

だから私はあなたのビジネス(ゲーム)ロジックをui_から分離することをお勧めします。これは一般的には良い考えです。ゲームの_state_をそのクラス用に設計された状態に保ち、その状態に従って画像を更新します。このようにすれば、winformsの代わりにウェブサイトのような別のUIを使用することに決めた場合には、コードを再利用する方が簡単になります(もちろん理論上のヒントです)。 –

関連する問題