2012-07-31 34 views
0

嗨,大家好我想製作一個紙牌遊戲,並面臨在32張圖片框中分發32張圖片時的一些問題,但是我無法編寫關於如何製作圖片列表的代碼由他們的名字。我已經嘗試使用按鈕作爲卡片的代碼,並隨機分配給玩家,這是成功的,但是當我試圖在圖像的情況下做到這一點,我無法得到它的權利我發佈我的兩個代碼在這裏。C#在多個圖片框中隨機化多個圖像

class CardsDeck 
{ 
    public static Random r = new Random(); 

    private static List<string> cards = new List<string>{ "♣ King", "♣ Queen", "♣ Jack", " ♣", "♣ 7", "♣ 8", "♣ 9", "♣ 10", 
                  "♦ King", "♦ Queen", "♦ Jack", " ♦", "♦ 7", "♦ 8", "♦ 9", "♦ 10", 
                  "♥ King", "♥ Queen", "♥ Jack", " ♥", "♥ 7", "♥ 8", "♥ 9", "♥ 10", 
                  "♠ King", "♠ Queen", "♠ Jack", " ♠", "♠ 7", "♠ 8", "♠ 9", "♠ 10" }; 
    public string ReceiveCards() 
    { 
     if (cards.Count > 0) 
     { 
      int index = r.Next(cards.Count); 
      var card = cards[index]; 
      cards.RemoveAt(index); 
      return card; 
     } 
     else 
     { 
      return ""; 
     } 
    } 
} 

,並在主窗體

public partial class mainForm : Form 
{ 
    public mainForm() 
    { 
     InitializeComponent(); 

     CardsDeck cd = new CardsDeck(); 

     btnA1.Text = cd.ReceiveCards(); 
     btnA2.Text = cd.ReceiveCards(); 
     btnA3.Text = cd.ReceiveCards(); 
     btnA4.Text = cd.ReceiveCards(); 

     btnB1.Text = cd.ReceiveCards(); 
     btnB2.Text = cd.ReceiveCards(); 
     btnB3.Text = cd.ReceiveCards(); 
     btnB4.Text = cd.ReceiveCards(); 


     btnC1.Text = cd.ReceiveCards(); 
     btnC2.Text = cd.ReceiveCards(); 
     btnC3.Text = cd.ReceiveCards(); 
     btnC4.Text = cd.ReceiveCards(); 


     btnD1.Text = cd.ReceiveCards(); 
     btnD2.Text = cd.ReceiveCards(); 
     btnD3.Text = cd.ReceiveCards(); 
     btnD4.Text = cd.ReceiveCards(); 



    } 

    private void btnTrump_Click(object sender, EventArgs e) 
    { 
     CardsDeck cd = new CardsDeck(); 


     btnA5.Text = cd.ReceiveCards(); 
     btnA6.Text = cd.ReceiveCards(); 
     btnA7.Text = cd.ReceiveCards(); 
     btnA8.Text = cd.ReceiveCards(); 

     btnB5.Text = cd.ReceiveCards(); 
     btnB6.Text = cd.ReceiveCards(); 
     btnB7.Text = cd.ReceiveCards(); 
     btnB8.Text = cd.ReceiveCards(); 

     btnC5.Text = cd.ReceiveCards(); 
     btnC6.Text = cd.ReceiveCards(); 
     btnC7.Text = cd.ReceiveCards(); 
     btnC8.Text = cd.ReceiveCards(); 

     btnD5.Text = cd.ReceiveCards(); 
     btnD6.Text = cd.ReceiveCards(); 
     btnD7.Text = cd.ReceiveCards(); 
     btnD8.Text = cd.ReceiveCards(); 
    } 
} 

在現在這個情況下,我想用圖片來代替用繩子做的做,但我不能讓我的代碼的權利。任何人都可以請幫我在這方面

class Card 
{ 
    public static Random r = new Random(); 

    PictureBox[] picBox = new PictureBox[32] 

    public static Bitmap[] pictures = new Bitmap[32]; 
    // What should i write here?? 

    bool[] usedPictures = new bool[pictures.Length]; 


} 
public string ReceiveCards() 
{ 
    int ICount = 0; 
    while (iCount < pictures.Length) 
    { 
     int attempt = random.Next(0, pictures.Length); 

     //Ensures you will only use an available picture 
     if (usedPictures[attempt] == false) 
     {    
      picBox[attempt].Image= pictures[iCount]; 
      doorUsed[attempt] = true; 
      iCount++; 
     } 
} 

回答

1

可以使用的ImageList,根據列表汽車

`private static List<string> cards = new List<string>{ "♣ King", "♣ Queen", "♣ Jack", " ♣", "♣ 7", "♣ 8", "♣ 9", "♣ 10", 
                 "♦ King", "♦ Queen", "♦ Jack", " ♦", "♦ 7", "♦ 8", "♦ 9", "♦ 10", 
                 "♥ King", "♥ Queen", "♥ Jack", " ♥", "♥ 7", "♥ 8", "♥ 9", "♥ 10", 
                 "♠ King", "♠ Queen", "♠ Jack", " ♠", "♠ 7", "♠ 8", "♠ 9", "♠ 10" }; 

比你可以用卡來獲得需要的ImageList指數,以添加圖像在ImageList中。

你可以看到如何與http://msdn.microsoft.com/ru-ru/library/system.windows.forms.imagelist.aspx

+0

是我想,但我不能找到所有的人都單幅圖像的任何實例:( – 2012-07-31 14:34:28

0

看來你的方法是從面向對象的角度來看有利於工作。爲什麼不創建一個PlayingCard對象;一個具有'CardImage'屬性?通過這種方式,您不必維護兩個單獨的列表(卡片值之一,卡片圖像之一),您只需維護一張PlayingCards列表,並且您可以訪問.CardImage屬性?

public class PlayingCard 
{ 
    public enum Suit { Spades, Hearts, Diamonds, Clubs } 
    public enum Rank {King =13, Queen = 12, Jack = 11, Seven = 7, Eight = 8, Nine = 9 } 

    public Suit CardSuit; 
    public Rank CardRank; 

    public Bitmap CardImage; 
} 

然後在你的構造函數中解析Suit和Rank來確定圖像。

創建您的集合作爲 名單<遊戲牌>