cardgame - best way to draw cards in C++

Started by
3 comments, last by BroOf 14 years, 5 months ago
Hey guys I am new to C++ and I want to start with a simple cardgame to learn the language. I can code PHP but after looking through the "get started with C++" tutorials I noticed a lot of differences between this languages... of course :D. But now I am able to code little things, display images with SDL, move them and so on... To my problem: A user should be able to draw a card from a predefined pool of cards. In PHP I would save them in an array with keys like this: $array = new array(1 => joker, 2 => king, 3 => ace, and so on...); and if the user draws a card I would grab the first array-element. I would put it into a new function to display the drawn card. Now I have a questions to the experienced users here: Is the way I would do it in PHP also suitable for C++? Because PHP is a serverside language and C++ in my case a clientside language...
Advertisement
You'd probably do something similar in C++. You could use a std::vector (which is probably more analogous to arrays in PHP than an actual array).

PHP isn't a strictly server-side language, by the way, and the server-side/client-side distinction isn't really important for decisions like this.
Slightly different way (don't draw the first card, draw a random card and move it to bottom of deck).

//Pseudo codeint deck[56]; //Fill with numbers 0->55suits[4] = {CLUBS, HEARTS,...};values[14] = {ONE, ..., ACE};int getValue(int i) { return values[i%14]; }int getSuit(int i) { return suits[i/14]; }int drawCard(){   int idx = random(0, deckSize); //use rand or a better way   swap(&deck[idx], &deck[deckSize]);   return deck[deckSize--];}


This way you never have to reset or shuffle the deck, you just reset deckSize to (56-1).
f@dzhttp://festini.device-zero.de
Or you could use std::deque to model the behavior of a deck more accurately:

#include <deque>#include <vector>#include <algorithm>class Deck {private:	std::deque<Card> mCards;public:	Deck() {		// Fill the deck with cards:		for ( unsigned i = 0; i < 52; ++i )			mCards.push_back( Card( i ) );		Shuffle();	}public:	void Shuffle() {		// Shuffles the deck (even when there are cards missing)		std::random_shuffle( mCards.begin(), mCards.end() );	}	Card DrawFromTop() {		Card temp = mCards.front(); // Copy the card at the front of the deck		mCards.pop_front(); // Remove the top card from the deck		return temp; // Return the copy	}	void Discard( const Card& theCard ) {		// Insert a card at the end of the deck		mCards.push_back( theCard );	}};class Hand {private:	std::vector<Card> mCards;public:	void DrawFromDeck( Deck& theDeck ) {		// Take five cards from the top of the deck		for ( int i = 0; i < 5; ++i ) 			mCards.push_back( theDeck.Draw() );	}	void DiscardHand( Deck& theDeck ) {		// Discard all of the cards back to the deck		for ( int i = 0; i < mCards.size(); ++i )			theDeck.Discard( mCards );		mCards.clear();	}};
Wow you guys rock! Thank you very much! This was exactly what I am looking for!

This topic is closed to new replies.

Advertisement