简体   繁体   中英

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. Does anyone have any idea how I can give each player, 26 random cards from the 52 card deck?

This should empty the original 52 card Deck Dictionary, and create 2 smaller 26 card deck dictionary.

I have been struggle with this for 3 day, and have done all the research I could do. 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. 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 .

 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)) . 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++;

        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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