2015-04-22 89 views
0

我一直在C#中製作一個卡匹配遊戲。它使用圖片陣列設置了12張卡片(每張照片2張,總共6張照片),當您按下卡片時,通過使用另一個數組(一個數組爲1的int數組) -6重複兩次,相同的照片)我使用此代碼隨機數字匹配遊戲,使照片在C中相匹配#

  Image away; 
      int tagger; 
      for (int i = 1; i < 13; i++) 
      { 
       cards[i].Visible = true; 
       away = pics[i]; 
       tagger = tags[i]; 

       int h = random.Next(1, 6); 
       pics[i] = pics[h]; 
       tags[i] = tags[h]; 
       pics[h] = away; 
       tags[h] = tagger; 
      } 
      for (int i = 1; i < 13; i++) 
      { 
       cards[i].Image = pics[i]; 
      } 

它適用於比賽的4,那麼它說的匹配是錯誤的休息...誰能幫助?

這裏是我的代碼的其餘部分:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Threading; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     int a = 0; 
     PictureBox card = null; 
     Image[] pics = new Image[13]; 
     int[] tags = new int[13]; 
    PictureBox[] cards = new PictureBox[13]; 
    Random random = new Random(); 
    public Form1() 
    { 
     InitializeComponent(); 
     pics[1] = Image.FromFile(@"h:\profile\desktop\game\photos\g1.jpg"); 
     pics[2] = Image.FromFile(@"h:\profile\desktop\game\photos\g1.jpg"); 
     pics[3] = Image.FromFile(@"h:\profile\desktop\game\photos\g2.jpg"); 
     pics[4] = Image.FromFile(@"h:\profile\desktop\game\photos\g2.jpg"); 
     pics[5] = Image.FromFile(@"h:\profile\desktop\game\photos\g3.jpg"); 
     pics[6] = Image.FromFile(@"h:\profile\desktop\game\photos\g3.jpg"); 
     pics[7] = Image.FromFile(@"h:\profile\desktop\game\photos\g4.jpg"); 
     pics[8] = Image.FromFile(@"h:\profile\desktop\game\photos\g4.jpg"); 
     pics[9] = Image.FromFile(@"h:\profile\desktop\game\photos\g5.jpg"); 
     pics[10] = Image.FromFile(@"h:\profile\desktop\game\photos\g5.jpg"); 
     pics[11] = Image.FromFile(@"h:\profile\desktop\game\photos\g6.jpg"); 
     pics[12] = Image.FromFile(@"h:\profile\desktop\game\photos\g6.jpg"); 
     tags[1] = 1; 
     tags[2] = 1; 
     tags[3] = 2; 
     tags[4] = 2; 
     tags[5] = 3; 
     tags[6] = 3; 
     tags[7] = 4; 
     tags[8] = 4; 
     tags[9] = 5; 
     tags[10] = 5; 
     tags[11] = 6; 
     tags[12] = 6; 

     cards[1] = pictureBox1; 
     cards[2] = pictureBox2; 
     cards[3] = pictureBox3; 
     cards[4] = pictureBox4; 
     cards[5] = pictureBox8; 
     cards[6] = pictureBox7; 
     cards[7] = pictureBox6; 
     cards[8] = pictureBox5; 
     cards[9] = pictureBox9; 
     cards[10] = pictureBox10; 
     cards[11] = pictureBox11; 
     cards[12] = pictureBox12; 
    } 

    private void click_card(object sender, EventArgs e) 
    { 
     PictureBox pic = sender as PictureBox; 
     string s = pic.Name; 
     string s1 = s.Substring(10); 
     int num = int.Parse(s1); 
     if (card == null) 
     { 
      card = pic; 
      a = num; 
     } 
     else if (a > 0) 
     { 
      int one = tags[a]; 
      int two = tags[num]; 
      if (one == two) 
      { 
       System.Windows.Forms.MessageBox.Show("Correct!"); 
       a = 0; 
       pic.Visible = false; 
       card.Visible = false; 
       card = null; 
      } 
      else if (one != two) 
      { 
       System.Windows.Forms.MessageBox.Show("Wrong!"); 
       card = null; 
       a = 0; 
      } 
     } 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
      Image away; 
      int tagger; 
      for (int i = 1; i < 13; i++) 
      { 
       cards[i].Visible = true; 
       away = pics[i]; 
       tagger = tags[i]; 

       int h = random.Next(1, 6); 
       pics[i] = pics[h]; 
       tags[i] = tags[h]; 
       pics[h] = away; 
       tags[h] = tagger; 
      } 
      for (int i = 1; i < 13; i++) 
      { 
       cards[i].Image = pics[i]; 
      } 
     } 
    } 
} 
+1

如果使用__points__的圖片和標籤數組索引的數據結構,可以使用隨機播放功能:http://stackoverflow.com/questions/273313/randomize-a-listt-in-c -sharp –

+0

即時通訊對不起,我不知道如何使用此... @matthew –

+0

因爲您正在使用它們來確定要使用哪個標籤,因此分配給您的PictureBox的名稱是什麼? – Shar1er80

回答

1

要在 「洗牌」 發表評論。

如果cards是你的數組,那麼你的for循環應該從0開始。數組基於零。你有12張牌,所以你會有0-11個指數。然後你應該使用數組的Length屬性來控制你的for循環。

當您使用random.next(1,6)時,您只會生成一個從1到5的隨機數。索引0-6 - 11永遠不會被生成,所以永遠不會被洗牌。

最後,我不明白爲什麼你需要額外的for循環,只需將圖像的賦值添加到交換後的for循環的底部。

Image away; 
int tagger; 
for (int i = 1; i < cards.Length; i++) 
{ 
    cards[i].Visible = true; 
    away = pics[i]; 
    tagger = tags[i]; 

    int h = random.Next(1, cards.Length + 1); 
    pics[i] = pics[h]; 
    tags[i] = tags[h]; 
    pics[h] = away; 
    tags[h] = tagger; 

    cards[i].Image = pics[i]; 
} 
+0

我只有6張卡,數組有12張卡,每張卡2個被複制,所以我只想要1到6之間的數字,然後我想要標籤數組複製 –

+1

的圖片編號您的代碼表示您有12個圖片,標籤和卡片對象。 – Shar1er80

+0

您應該將此發佈到您的問題中,而不是作爲評論。在洗牌方面仍然看到我的回答。如果您的變量圖片有12個索引,則圖片聲明可能看起來像圖片[]圖片=新圖片[13]; – Shar1er80