使用随机数在一个列表中选择一个项目并将其移动到另一个列表 [英] Using random numbers to select a item in one list and move it to another list

查看:144
本文介绍了使用随机数在一个列表中选择一个项目并将其移动到另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的卡片游戏叫四合(继承人呈现规则的链接 HTTP: //boardgames.about.com/od/cardgames/a/snap.htm ),在我的版本中,玩家将在中间点击桩,如果对显示出来。我目前4类,一个用于存储卡(其是称为cardValue_ INT),一个用于玩家手,一个用于卡的原甲板和一个用于卡在中间堆。因此,甲板,桩手类有卡在其中一个列表。现在我试图写它包含卡的列表甲板类洗牌方法。这将选择一个随机卡并将其移动到一个新的列表,直到所有的卡片摘下来,然后再回到原来的列表,从而做一个简单的洗牌。我的方法,到目前为止是这样的...

I am designing a card game called Snap (Heres a link showing rules http://boardgames.about.com/od/cardgames/a/snap.htm), in my version, the player will have to click on the pile in the middle if a pair shows up. I have 4 classes currently, one for the card (which is a int called cardValue_), one for the players hand, one for the original deck of cards and one for the pile of cards in the middle. So the Deck, Pile and Hand classes has a List of Cards in them. I am now trying to write a shuffle method for the Deck class which contains a list of Cards. Which will pick a random card and move it to a new list until all the cards are picked, then move them back to the original list, thus doing a simple shuffle. My method so far looks like this...

public List<Deck> Shuffle(List<Card> cardDeck)
    {
        int index = 0;
        Random randomCard = new Random();
        List<Card> newDeck = new List<Card>();

        while (index < cardDeck.Count)
        {
            int ran = randomCard.Next(0, cardDeck.Count);
            foreach (Card card in cardDeck)
            {

            }
        }
    }

我试图找出哪些应该在foreach循环中去(除非整个的方法是错误的),但现在我想我已经宣布我所有的卡在错误的的地方,所有的52张牌的形式,目前申报,或者我应该在甲板类中声明它们?

I am trying to work out what should go in the foreach loop (unless the whole method is wrong), but now I am thinking I have declared all my Cards in the wrong place, all 52 cards are currently declared in the Form, or should i be declaring them in the Deck class?

推荐答案

您在哪里相当接近,我将如何解决这个问题,我会做的就是复制出源列表中随机直到它是空的,然后只是填充它。你并不需要返回一个列表,因为这会打乱你传递列表中。

You where fairly close to how I would solve it, what I would do is copy out of the source list randomly until it is empty and then just refill it. You don't need to return a list as this will just shuffle the list you pass in.

//Move this out of the function, if you are wondering why search SO for "Not random" and choose any of the 100's of people asking "why is random not random?" :)
private static Random randomCard = new Random(); //Not thread safe, if multi-threading use locks!!!

public static void Shuffle(List<Card> cardDeck)
{
    int index = 0;
    List<Card> tempDeck = new List<Card>();

    while (cardDeck.Count > 0)
    {
        int removal = randomCard.Next(0, cardDeck.Count);
        Card tempCard = cardDeck[removal];
        cardDeck.RemoveAt(removal);
        tempDeck.Add(tempCard);
    }

    //cardDeck is empty at this point, now we refill it with our randomized deck.
    cardDeck.AddRange(tempDeck);
}






如果你想不。修改原来的列表,你想一个新的随机名单只是第一个复制源列表


If you want to not modify the original list and you do want a new randomized list just copy the source list first.

public static List<Card> Shuffle(List<Card> cardDeck)
{
    int index = 0;
    List<Card> tempDeck = new List<Card>();
    List<Card> localCopy = new List<Card>(cardDeck);   //Creates a shallow copy of the list.      

    while (localCopy.Count > 0)
    {
        int removal = randomCard.Next(0, cardDeck.Count);
        Card tempCard = localCopy[removal];
        localCopy.RemoveAt(removal);
        tempDeck.Add(tempCard);
    }

    return tempDeck;
}






我会推荐使用< A HREF =http://stackoverflow.com/a/14766143/80274>理查德的方法。它要简单得多。

这篇关于使用随机数在一个列表中选择一个项目并将其移动到另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆