【问题标题】:C# Randomly distributing strings without repetiotion [closed]C#随机分布字符串而不重复[关闭]
【发布时间】:2012-07-27 06:23:30
【问题描述】:

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

}

【问题讨论】:

  • 我们不是代码工厂?你试过什么?你被困在哪里了?我们无法帮助那些不会自助的人。
  • 对不起,我当时无法保存我的代码,但现在我有,所以现在你能帮帮我吗:)

标签: c# .net random shuffle


【解决方案1】:

公共部分类 Form1 : Form { 公共表格1() { 初始化组件(); }

    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 "";
            }

        }
    } 

}

【讨论】:

  • 非常感谢,这也很有帮助:)
【解决方案2】:

最简单的方法是在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();
        // ...
    }
}

【讨论】:

  • 非常感谢您的帮助我正在尝试制作纸牌游戏,所以我需要它。我将制作的游戏将是一个 4 人在线游戏,所以你能参考任何关于 C# 网络的书吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 2015-02-20
  • 1970-01-01
  • 2020-11-22
  • 2019-04-01
  • 2013-02-14
相关资源
最近更新 更多