-4

大家好,我需要一些帮助,我想在 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();
    }
}

}

4

2 回答 2

0

最简单的方法是在类内部实现一个随机播放算法 CardDeck。像费雪-耶茨这样的东西。

您遇到的问题是您每次都在重新制作数组,然后抓取一个随机元素。这将导致重复。您的选择是要么返回整个洗牌数组(更好的一个 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();
        // ...
    }
}
于 2012-07-29T06:23:24.877 回答
0

公共部分类 Form1 : Form { public 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 "";
            }

        }
    } 

}
于 2012-07-29T06:29:31.557 回答