none
building a poker game RRS feed

  • Question

  • I am new to C# and OOP and trying to learn the basics. My question has to do with building the correct classes. SO I am attempting to build a draw poker game

    1. public class Card : IComparable<Card> this works fine
    2. public class Deck : List<Card> there are methods for creating, shuffling, cutting, and dealing both a single card and multiple cards. these all work fine
    3. public class Hand : List<Card> there are properties for returning  and sorting the hand
    4. Now I just need to start a game and deal  cards to a hand
    5. Then determine the value of the hand

    The question now is where is the deck of cards to deal into this hand? I know it shouldn't be within Hand class. The Dealer should have the deck. Is there now a dealer class or is this just the main program? Then how do I deal cards into the hand object I create? Where are the evaluation rules for hand values located?

    Any assistance would be great. Thanks


    Cavale
    Friday, July 29, 2011 8:14 PM

Answers

  • So you know things about a deck of cards, you have 13 cards in each suit, and they go up from rank 1-13. (13 being King or Ace). Fill your deck with all 52 cards.

    Conceptually, the deck is really no different than a hand, any card not in a hand is in a deck or in a throw out pile, right?

    So you already have the deck, when you want to deal, just pick N number of random cards, remove them from the deck and place them in a player's hand.

    List<> gives you the ability to RemoveAt(index), so you can do something like:


    for(int cardNum = 0; cardNum < 7; cardNum++) { //simulate dealing 7 cards to each player, one at a time
    foreach(Hand playerHand in AllHands) {
    int thisCard = Random.Next(...); //Pick a card at random from the cards left in the deck to give to the player
    playerHand.Add(Deck[thisCard]); //Place the card in the player's hand
    Deck.RemoveAt(thisCard); //Remove the card from the deck
    }
    }

    Some notes: AllHands is a List<Hand> which you have to create, one entry for each player.

    Deck is your list of 52 cards (initially)

    Friday, July 29, 2011 9:22 PM

All replies

  • So you know things about a deck of cards, you have 13 cards in each suit, and they go up from rank 1-13. (13 being King or Ace). Fill your deck with all 52 cards.

    Conceptually, the deck is really no different than a hand, any card not in a hand is in a deck or in a throw out pile, right?

    So you already have the deck, when you want to deal, just pick N number of random cards, remove them from the deck and place them in a player's hand.

    List<> gives you the ability to RemoveAt(index), so you can do something like:


    for(int cardNum = 0; cardNum < 7; cardNum++) { //simulate dealing 7 cards to each player, one at a time
    foreach(Hand playerHand in AllHands) {
    int thisCard = Random.Next(...); //Pick a card at random from the cards left in the deck to give to the player
    playerHand.Add(Deck[thisCard]); //Place the card in the player's hand
    Deck.RemoveAt(thisCard); //Remove the card from the deck
    }
    }

    Some notes: AllHands is a List<Hand> which you have to create, one entry for each player.

    Deck is your list of 52 cards (initially)

    Friday, July 29, 2011 9:22 PM
  • http://msdn.microsoft.com/en-us/beginner/aa700775.aspx

    Take a look at the link.  There is a sample blackjack game, which you can use to create your own custom card game.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Friday, July 29, 2011 10:42 PM
    Moderator