banner



Designing A Poker Game In C++

Thread: Designing a poker game architecture

  1. December 22nd, 2010,05:32 PM #1

    Designing a poker game architecture

    Soon I'm going to code the actual card generation and the only way I thought of doing this is doing something like this:

    Code:

    CARD card = generateCard();  while(cardAlreadyExists(card)) {   card = generateCard(); }  //continue game

    This just doesn't look like the best way of generating the cards so I thought I should ask here if you know a better way of doing this.

    thanks


  2. December 22nd, 2010,05:35 PM #2

    Re: Designing a poker game architecture

    What do you mean by "generateCard"?

    Viggy


  3. December 22nd, 2010,05:43 PM #3

    Re: Designing a poker game architecture


  4. December 22nd, 2010,06:40 PM #4

    Re: Designing a poker game architecture

    poker is played with more than 52 cards in most cases.

    you might be interested in looking at pokerTH


  5. December 22nd, 2010,06:42 PM #5

    Re: Designing a poker game architecture

    If the mental model guiding you is poker, why not create a deque (pun intended!) of however many card decks you want to use, sort them randomly, and then just deal them from the end one after another.

  6. December 22nd, 2010,06:48 PM #6

    Re: Designing a poker game architecture

    also,

    Code:

    CARD card; do {   card = generateCard(); } while(cardAlreadyExists(card));  //continue game

  7. December 22nd, 2010,09:18 PM #7

    Re: Designing a poker game architecture

    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.


  8. December 23rd, 2010,02:17 AM #8

    Re: Designing a poker game architecture

    Quote Originally Posted by Eri523 View Post

    The "standard" C++ way of doing this is, as Eri said, to create a container of your 52 cards. Random shuffle your container, and read the cards one by one.

    To make it more "OO", make a class called poker_deque, with the public methods "shuffle", and "draw_card". You can also throw in "return_to_bottom", depending on the kind of poker game you want to model.

    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.


  9. December 23rd, 2010,03:04 AM #9

    Smile Re: Designing a poker game architecture

    Quote Originally Posted by monarch_dodra View Post

    ...To make it more "OO", make a class called poker_deque...

    An "OO" approach probably wouldn't start with an 'abstract' object like a queue but with classes/types Game, Deck, Hand, Card, Player, Suit, Rank ... If you found out what each of these objects would do, you would get the methods and then you may think of how you could implement those methods, e. g storing the cards in a deck by means of a standard container.

  10. December 23rd, 2010,03:22 AM #10

    Re: Designing a poker game architecture

    Quote Originally Posted by paprica View Post

    Soon I'm going to code the actual card generation and the only way I thought of doing this is doing something like this:

    Code:

    CARD card = generateCard();  while(cardAlreadyExists(card)) {   card = generateCard(); }  //continue game

    This just doesn't look like the best way of generating the cards so I thought I should ask here if you know a better way of doing this.

    thanks

    Just out of curiosity, how did you implement these two methods (generateCard, cardAlreadyExists)?

    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.


  11. December 23rd, 2010,03:46 AM #11

    Re: Designing a poker game architecture

    I've never tried a card game myself. This is what I've come up with so far.

    I guess you can call this a game of 52 card pickup

    Code:

    #include <iostream> #include <deque> #include <functional> #include <algorithm> #include <iterator> #include <ctime> #include <cstdlib>  enum CardSuit { Spades, Hearts, Diamonds, Clubs }; enum CardNumber { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };  const char* CardSuitNames[] =  { 	"Spades", "Hearts", "Diamonds", "Clubs" };  const char* CardNumberNames[] =  { 	"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };  class Card { public: 	Card(CardNumber number = Ace, CardSuit suit = Spades) 		: m_number(number), m_suit(suit) 	{ 		assert(IsValid()); 	} 	const char* number() const 	{ 		return CardNumberNames[m_number]; 	} 	const char* suit() const 	{ 		return CardSuitNames[m_suit]; 	} 	bool IsValid() const 	{ 		return (m_number <= King && m_suit <= Clubs) ? true : false; 	} private: 	CardNumber m_number; 	CardSuit m_suit; };  class Deck { public:  	Deck()  		: m_cards(0) 	{}  	Card getCard() 	{ 		Card card = m_cards.back(); 		m_cards.pop_back(); 		return card; 	}  	void shuffle() 	{ 		std::random_shuffle(m_cards.begin(), m_cards.end(), [](ptrdiff_t i){ return rand()%i; }); 	}  	bool Empty() const { return size() > 0 ? false : true; }  	typedef Card value_type; 	typedef Card& reference; 	typedef const Card& const_reference; 	typedef std::deque<Card>::iterator iterator; 	typedef std::deque<Card>::const_iterator const_iterator; 	typedef std::deque<Card>::size_type size_type;  	void push_back(const Card& card){ m_cards.push_back(card); }  	size_type size() const { return m_cards.size(); }  	void resize (size_type sz, Card c = Card()) 	{ 		m_cards.resize(sz, c); 	}  	iterator begin() { return m_cards.begin(); } 	const_iterator begin() const { return m_cards.begin(); } 	iterator end() { return m_cards.end(); } 	const_iterator end() const { return m_cards.end(); }  	static Deck GenerateStandardDeck() 	{ 		Deck deck; 		for(int suit = Spades; suit <= Clubs; suit++) 		{ 			for(int number = Ace; number <= King; number++) 			{ 				deck.push_back(Card((CardNumber)number, (CardSuit)suit)); 			} 		} 		return deck; 	}  private: 	std::deque<Card> m_cards; };  using namespace std;  int main(int argc, char *argv[]) { 	srand(time(NULL)); 	Deck myDeck = Deck::GenerateStandardDeck(); 	myDeck.shuffle(); 	while(!myDeck.Empty()) 	{ 		Card card = myDeck.getCard(); 		cout << card.number() << " of " << card.suit() << endl; 	}	 	system("pause"); 	return 0; }

  12. December 23rd, 2010,04:43 AM #12

    Re: Designing a poker game architecture

    Quote Originally Posted by Chris_F View Post

    Code:

                                      bool                                  Empty() const { return size() > 0                                  ? false : true;                                  }
    Why the ternary operator? The type is already a bool, so even under VS, there is no retarded warning to squelch. Also, never check size against 0, but call the container's empty. Also, since you are following stl container interface, the name is usually "empty", not "Empty".

    Code:

    bool empty() const {m_cards.empty();}
    In my opinion (opinion, not absolute truth), your deck leaks way too much abstraction. The entire point of having a "Deck" class to begin with, is so the user does not handle the underlying container. IMO, all the typedefs are useless, and you should not provide the any public iterator.

    If worst comes to worst, and somebody wants direct control on the cards, I think it is better to just provide a "data()" method that returns the underlying container. But for every other user:
    - look at top card
    - draw top card
    - insert on top
    - insert on bottom
    - insert randomly
    - shuffle deck
    - reset deck

    Optionally:
    - remove card
    - sort deck

    For anything more complicated, just give them the container, and the user can implement functions on a need-to basis. That would be my personal opinion on interface design. Feel free to disagree.

    I also disagree with your card design. I don't think you should return card name/vale as strings, but just provide serialization for your cards:

    Code:

    class Card { public: 	Card(CardNumber number = Ace, CardSuit suit = Spades) 		: m_number(number), m_suit(suit) 	{ 		assert(IsValid()); 	}  	CardNumber number() const 	{ 		return m_number; 	}  	CardSuit suit() const 	{ 		return m_suit; 	}  private: 	bool IsValid() const 	{ 		return m_number <= King && m_suit <= Clubs; //Same here, no need for ternary 	}  	CardNumber m_number; 	CardSuit m_suit; };  std::ostream& operator<<(std::ostream& io_ostream, const card& i_card) {     io_ostream << CardNumber[card.number()] << "of" << CardSuit[card.suit()];     return io_ostream; }
    Also, IsValid should be private (IMO)

    When iterating over enums, I've been taught to add a "last_value" in my enums. This makes iteration safer: you can iterate from 0 to last_value, and it always works, even you change the enum. For example, if somebody decides to place Ace after King, your code will break. However:

    Code:

    enum CardSuit {   Spades,   Hearts,   Diamonds,   Clubs,   CardSuit_LastValue };  int main() {     for(int i = 0; i<CardSuit_LastValue; ++i) //Notice < and not <=     {         CardSuit current_suit = static_cast<CardSuit>(i);         /*Do stuff with current_suit*/     } }
    With this design, your code is more robust to change, and overall safer to begin with (If you mess up your first or last value). It is not an end-all design, but I thought I'd share it, since it is always useful.

    Finally, your CardSuitNames is an array of const cstrings, but the array itself is not const. It should:

    Code:

    const char* const CardSuitNames[] = { 	"Spades", "Hearts", "Diamonds", "Clubs" };
    Now I can't do this:

    Code:

    int main(int argc, char* argv[]) {     const char dummy[] = "butt";     CardSuitNames[1] = dummy;     std::cout << CardSuitNames[1] << std::endl; }
    There, these are all my personal 0.02$. Overall, my design would have been similar, I just wanted to point out the snippets I would have done differently.
    Last edited by monarch_dodra; December 23rd, 2010 at 04:46 AM.

    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.


  13. December 23rd, 2010,05:01 AM #13

    Re: Designing a poker game architecture

    Yeah, in my defense it's late and I didn't want to spend to much time on it.

    I just wanted an easy way to copy decks together, which is why I added the iterators.

    The rest were just careless mistakes.


  14. December 23rd, 2010,05:20 AM #14

    Re: Designing a poker game architecture

    Quote Originally Posted by Chris_F View Post

    Yeah, in my defense it's late and I didn't want to spend to much time on it.

    I just wanted an easy way to copy decks together, which is why I added the iterators.

    The rest were just careless mistakes.

    deques provide operator=, and is your only member, so deck already has a default implemented operator= (and copy constructor) (maybe even move-assign, move-construct?).

    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

Click Here to Expand Forum to Full Width



Designing A Poker Game In C++

Source: https://forums.codeguru.com/showthread.php?506783-Designing-a-poker-game-architecture

Posted by: cookanighbold.blogspot.com

0 Response to "Designing A Poker Game In C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel