2016-11-14 22 views
0

私はスロットマシン用に3枚の画像を比較しようとしていますが、問題は何も起こりませんが、画像は何かに比べて得られないようです。私は、画像一覧の画像を有し、それらは、ランダムに選択されているが、imagebox1はチェリーbettextboxが習慣=勝者C#画像をIfステートメントと比較する方法

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    Bitmap apple = Properties.Resources.Apple; 
    Bitmap cherries = Properties.Resources.Cherries; 
    Bitmap orange = Properties.Resources.Orange; 

    private void spinButton_Click(object sender, EventArgs e) 
    { 
     Random rand = new Random(); 
     int pic = rand.Next(0, images.Images.Count); 
     int pic2 = rand.Next(0, images.Images.Count); 
     int pic3 = rand.Next(0, images.Images.Count); 

     pictureBox1.Image = images.Images[(pic)]; 
     pictureBox2.Image = images.Images[(pic2)]; 
     pictureBox3.Image = images.Images[(pic3)]; 

     if (pictureBox1.Image == cherries) 
     { 
      betTextBox.Text = "Winner"; 
     } 

答えて

0

あるとき、私はイメージリストコントロールにおける画像アレイは元のオブジェクトへの参照ではないことを賭けであろうしたがって、==との比較は機能しません(画像自体は同じですが、画像を保持するオブジェクトは同じ参照ではありません)。

代わりにif式に配列インデックスを使用できます。

int cherriesIndex=1; 

private void spinButton_Click(object sender, EventArgs e) 
{ 
    Random rand = new Random(); 
    int pic = rand.Next(0, images.Images.Count); 
    int pic2 = rand.Next(0, images.Images.Count); 
    int pic3 = rand.Next(0, images.Images.Count); 

    pictureBox1.Image = images.Images[(pic)]; 
    pictureBox2.Image = images.Images[(pic2)]; 
    pictureBox3.Image = images.Images[(pic3)]; 

    if (pic == cherriesIndex) 
    { 
     betTextBox.Text = "Winner"; 
    }` 

これは、チェリーがimages.Imagesの要素1であることを前提としています。主にイラストレーションのためのことです。

+0

これは私のためにやったようですが、今はif文でさまざまな勝利条件を得ることができます。 – Sovias

関連する問題