简体   繁体   English

我怎样才能在C#中处理这个套牌(字典)

[英]How can I deal this deck(Dictionary) in C#

I have 2 players, I want each of them to receive 26 random out of this 52 card deck. 我有2名球员,我希望他们每人能从这52张卡牌中随机收到26名球员。 Does anyone have any idea how I can give each player, 26 random cards from the 52 card deck? 有没有人知道我怎么能给每个玩家,从52张牌组中的26张随机牌?

This should empty the original 52 card Deck Dictionary, and create 2 smaller 26 card deck dictionary. 这应该清空原来的52卡Deck字典,并创建2个较小的26卡片组字典。

I have been struggle with this for 3 day, and have done all the research I could do. 我已经为此奋斗了3天,并完成了我能做的所有研究。 Here is my Deck Class: 这是我的甲板课程:

class Deck
{
    public Dictionary<int, string> CreateNewDeck()
    {
        Dictionary<int, string> newDeck = new Dictionary<int, string>()
        {
            {1, "~/cards/c1.png"}, {2, "~/cards/c2.png"}, {3, "~/cards/c3.png"}, {4, "~/cards/c4.png"},
            {5, "~/cards/c5.png"}, {6, "~/cards/c6.png"}, {7, "~/cards/c7.png"}, {8, "~/cards/c8.png"},
            {9, "~/cards/c9.png"}, {10, "~/cards/c10.png"}, {11, "~/cards/cj.png"}, {12, "~/cards/cq.png"}, {13, "~/cards/ck.png"},
            {14, "~/cards/d1.png"}, {15, "~/cards/d2.png"}, {16, "~/cards/d3.png"}, {17, "~/cards/d4.png"},
            {18, "~/cards/d5.png"}, {19, "~/cards/d6.png"}, {20, "~/cards/d7.png"}, {21, "~/cards/d8.png"},
            {22, "~/cards/d9.png"}, {23, "~/cards/d10.png"}, {24, "~/cards/dj.png"}, {25, "~/cards/dq.png"}, {26, "~/cards/dk.png"},
            {27, "~/cards/h1.png"}, {28, "~/cards/h2.png"}, {29, "~/cards/h3.png"}, {30, "~/cards/h4.png"},
            {31, "~/cards/h5.png"}, {32, "~/cards/h6.png"}, {33, "~/cards/h7.png"}, {34, "~/cards/h8.png"},
            {35, "~/cards/h9.png"}, {36, "~/cards/h10.png"}, {37, "~/cards/hj.png"}, {38, "~/cards/hq.png"}, {39, "~/cards/hk.png"},
            {40, "~/cards/s1.png"}, {41, "~/cards/s2.png"}, {42, "~/cards/s3.png"}, {43, "~/cards/s4.png"},
            {44, "~/cards/s5.png"}, {45, "~/cards/s6.png"}, {46, "~/cards/s7.png"}, {47, "~/cards/s8.png"},
            {48, "~/cards/s9.png"}, {49, "~/cards/s10.png"}, {50, "~/cards/sj.png"}, {51, "~/cards/sq.png"}, {52, "~/cards/sk.png"},
        };
        return newDeck;
    }
}

And here is the method I am trying to write to divide the deck As you can probably see, I am a novice: 这是我试图划分甲板的方法你可能会看到,我是一个新手:

class Dealer
{
    public Dictionary<int, string> DealPlayersDeck(Deck deck, Player human, Player computer)
    {
        Deck cards = new Deck();
        Player humanPlayer = new Player();
        Player computerPlayer = new Player();

        var fullDeck = cards.CreateNewDeck();
        var playerDeck = humanPlayer.PlayerCards;
        var computerDeck = computerPlayer.PlayerCards;

        int halfDeck = fullDeck.Count() / 2;

        foreach (object i in fullDeck)
        {
            List<int> keyList = new List<int>(fullDeck.Keys);
            Random rand = new Random();
            int randomKey = keyList[rand.Next(keyList.Count)];

            if (!playerDeck.ContainsKey(randomKey))
            {
                playerDeck.Add(randomKey, fullDeck[randomKey]);
                fullDeck.Remove(randomKey);
            }
        }
        return playerDeck;
    }
}

Here is a reference Link 这是参考链接

You can do like the following: keyList will give you a list of 26 unique keys. 您可以执行以下操作: keyList将为您提供26个唯一键的列表。 loop through the keys and add it to the playerDeck in the same time remove the same from fullDeck so that after the iteration of the loop full deck contains the keys that are not in playerDeck so you can move this to computerDeck . 循环通过键并将其添加到playerDeck同时从fullDeck删除相同的内容,以便在循环迭代完成后,包含不在playerDeck中的键,这样您就可以将其移动到computerDeck

 List<int> keyList = new List<int>(fullDeck.Keys);
 Random rand = new Random();
 var keyList = keyList.OrderBy(item => rand.Next()).Take(26).ToList();//will take only 26 random keys
 foreach (var randKey in keyList) // loop on keylist
  {
       playerDeck.Add(randKey, fullDeck[randKey]);
       fullDeck.Remove(randKey);        
  }
 computerDeck =fullDeck;

You dont have to get list of keys in loop each time. 你不必每次都得到循环中的键列表。 You dont have to loop on Dictionary. 你不必循环字典。 just get list of keys outside loop. 只需获取循环外的键列表。 shuffle it and iterate over it. 将它洗牌并迭代它。

List<int> keyList = new List<int>(fullDeck.Keys);
Random rand = new Random();

// shuffle list and take first 26 items from it
var shuffled = keyList.OrderBy(item => rand.Next()).ToList();
var keyListfirst = shuffled.Take(26).ToList();

foreach (var randKey in keyListfirst) // loop on keylist
{
     playerDeck.Add(randKey, fullDeck[randKey]);
     fullDeck.Remove(randKey);
}

// take the rest of the list
var keyListrest = shuffled.Skip(26).ToList();

foreach (var randKey in keyListrest)
{
     computerDeck.Add(randKey, fullDeck[randKey]);
     fullDeck.Remove(randKey);
}

In this case you can safely remove this condition if (!playerDeck.ContainsKey(randKey)) . 在这种情况下, if (!playerDeck.ContainsKey(randKey))您可以安全地删除此条件。 because now list is shuffled and you are iterating on it. 因为现在列表被洗牌了,你正在迭代它。 so you dont get one item twice. 所以你不要两件事。

It should work for your requirement. 它应该适合您的要求。

public void DealPlayersDeck()
    {
        Deck cards = new Deck();
        Player humanPlayer = new Player();
        Player computerPlayer = new Player();

        var fullDeck = cards.CreateNewDeck();
        //var playerDeck = humanPlayer.PlayerCards;
        //var computerDeck = computerPlayer.PlayerCards;

        Dictionary<int, string> playerDeck=new Dictionary<int,string>();
        Dictionary<int, string> computerDeck = new Dictionary<int, string>();

        int count=1;

        while (fullDeck.Keys.Count>0)
        {
            Random r = new Random();
            int randomKey = fullDeck.Keys.ToList().OrderBy(x => r.Next()).First();

            if (count % 2 == 0)
            {
                playerDeck.Add(randomKey, fullDeck[randomKey]);                
            }
            else
            {
                computerDeck.Add(randomKey, fullDeck[randomKey]);                
            }

            fullDeck.Remove(randomKey);
            count++;

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM