2012-07-31 140 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来确定图像。

创建您的集合作为 名单<游戏牌>