0

I am trying to create a basic poker game using c#.

So far I have got the cards randomly dealt using these three lines:

Suit suit = (Suit)randomCardSelector.Next(4);
Value value = (Value)randomCardSelector.Next(13);
Card abc = new Card(suit, value);

Now what I am trying to achieve is that the dealing of cards only of those who haven't been dealt already. Now to do this I have created two 'dictionary' collections, one called 'deck' and one called 'dealt'. I thought about adding all the cards to deck originally and then when dealt on the table they shall be deleted from deck and added to dealt using the cards unique key.

What do you believe will be the best way of going about this? I basically do not want duplicate cards, example Ace Hearts and Ace Hearts combination being in the same deck. How can I prevent this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

4 Answers4

2

Why not just have a list of cards pre-generated, then shuffle the deck and push them onto a stack.

When you are dealing the cards simply pop the next card off the stack.

There is another SO question here on how to shuffle a list in C#.

Community
  • 1
  • 1
benPearce
  • 37,735
  • 14
  • 62
  • 96
0

I think the simplest way would be to:

A: Override the GetHashCode() function, and create a hash using the card value and card suit. B: Implement IEquatable<Card> and compare both the suits and the values.

Then, just have a single HashSet<Card> which contains all dealt cards (no need for a dictionary), and have your card generator check whether or not the card already exists.

Rob
  • 26,989
  • 16
  • 82
  • 98
0

Your Dictionary approach should work, but I think the simplest way will be to generate a List of cards, initially in some natural order, and then shuffle them. Then, simply keep track of how many cards from the list you have dealt out. When it comes to shuffling the cards, you (and everyone else who hasn't read it yet) should read this excellent article on the pitfalls of shuffling.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
0

This is not how it's usually done. Most card games that I've seen have a Deck structure, which is usually an array[0..51] of values in the 0..51 range. For each value, suit is (cardValue / 13) and value is (cardValue % 13). You initally create the deck for a new game by initializing your array to 0..51 and then randomly exchanging the card at each position with a card at another, random position. Once you've done this, you just pick cards from one end and 'deal' to the players as needed.