C#随机分配字符串而不重复

时间:2012-07-29 05:50:27

标签: c# .net random shuffle

大家好我需要一些帮助,我想在6个按钮中随机分配6个字符串作为文本而不重复。那就是我想做一些改组并分发它,但不会重复,每个按钮都会有一个独特的字符串。如果有人可以发布代码那就太棒了:)谢谢

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();
    }
}

}

2 个答案:

答案 0 :(得分:0)

执行此操作的最简单方法是在CardDeck类中实现shuffle算法。像Fisher-Yates这样的东西。

你遇到的问题是你每次都要重新制作数组,然后抓住一个radom元素。这将导致重复。你可以选择返回一个完整的混洗数组(更好的一个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();
        // ...
    }
}

答案 1 :(得分: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 "";
            }

        }
    } 

}