2012-07-29 65 views
-4

嗨,大家好,我需要一些帮助,我想在6个按钮中随机分配6个字符串作为文本而不需要重复。这是我想做一些洗牌和分发它,但不会有重复每个按钮将保存一个唯一的字符串。如果任何人都可以张贴代码将是巨大的:)谢谢C#随机分发字符串而不重复

class Card_Deck 
{ 
    public Random r; 
    public string ReceiveCards() 
    { 
     List<string> cards = new List<string>(); 
     cards.Add("♣ King"); 
     cards.Add("♦ King"); 
     cards.Add("♥ King"); 
     cards.Add("♠ King"); 
     cards.Add("♣ Jack"); 
     cards.Add("♦ Jack"); 

     int index = r.Next(cards.Count); 
     var card = cards[index]; 
     cards.RemoveAt(index); 
     return card; 

    } 
} 

}

这是在主窗体

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     Card_Deck cd = new Card_Deck() { r = new Random(DateTime.Now.Millisecond) }; 

     button1.Text = cd.ReceiveCards(); 
     button2.Text = cd.ReceiveCards(); 
     button3.Text = cd.ReceiveCards(); 
     button4.Text = cd.ReceiveCards(); 
     button5.Text = cd.ReceiveCards(); 
     button6.Text = cd.ReceiveCards(); 
    } 
} 

}

+3

我们不是一个代码工厂?你有什么尝试?你卡在哪里?我们不能帮助那些不会帮助自己的人。 – zellio 2012-07-29 05:52:27

+0

对不起,我不能保存我的代码,但现在我已经如此现在可以请你帮我:) – 2012-07-29 06:03:25

回答

0

公共部分Form1类:形式 { 公共Form1中() { 的InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) 
    { 
     label1.Text = Card_Deck.ReceiveCards(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     label2.Text = Card_Deck.ReceiveCards(); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     label3.Text = Card_Deck.ReceiveCards(); 
    } 
    private void button4_Click(object sender, EventArgs e) 
    { 
     label4.Text = Card_Deck.ReceiveCards(); 
    } 
    private void button5_Click(object sender, EventArgs e) 
    { 
     label5.Text = Card_Deck.ReceiveCards(); 
    } 
    private void button6_Click(object sender, EventArgs e) 
    { 
     label6.Text = Card_Deck.ReceiveCards(); 
    } 
    class Card_Deck 
    { 
     public static Random r = new Random(); 
     private static List<string> cards = new List<string>() { "♣ King 1", 
      "♦ King 2", 
      "♥ King 3", 
      "♠ King 4", 
      "♣ Jack 5", 
      "♦ Jack 6" }; 

     public static string ReceiveCards() 
     { 
      if (cards.Count > 0) 
      { 
       int index = r.Next(cards.Count); 
       var card = cards[index]; 
       cards.RemoveAt(index); 
       return card; 
      } 
      else 
      { 
       return ""; 
      } 

     } 
    } 

} 
+0

非常感谢,这也非常有帮助:) – 2012-07-29 18:57:03

0

这样做的最简单的方法将在CardDeck类中实现一个shuffle算法。像Fisher-Yates。

你有的问题是,你每次都重新制作数组,然后抓住一个随机元素。这会造成重复。你的选择是要么返回一个整体洗牌数组(更好的一个imo),要么用一个私有变量来使卡组成为有状态的。

public class CardDeck { 

    private List<String> cards; 

    public CardDeck() { 
     cards = { "♣ King", "♦ King", "♥ King", 
        "♠ King", "♣ Jack", "♦ Jack" }.toList<String>(); 
    } 

    public List<String> Shuffle() { 
     // shuffle cards here 

     var rand = new System.Random(); 
     return cards.OrderBy(x => rand.Next()).toList<String>(); 
    } 
} 

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

     var deck = new CardDeck(); 
     var shuffledDeck = deck.Shuffle(); 

     buttom1.Text = shuffledDeck.pop(); 
     buttom2.Text = shuffledDeck.pop(); 
     // ... 
    } 
} 
+0

非常感谢你的帮助我正在尝试做一个纸牌游戏,所以我需要它。我将制作的游戏将是一个4人在线游戏,那么你可以参考任何书籍在C#网络? – 2012-07-29 06:41:49