Sign in to follow this  

cardgame - best way to draw cards in C++

This topic is 2963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Slightly different way (don't draw the first card, draw a random card and move it to bottom of deck).


//Pseudo code

int deck[56]; //Fill with numbers 0->55
suits[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).

Share this post


Link to post
Share on other sites
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[i] );
mCards.clear();
}
};




Share this post


Link to post
Share on other sites

This topic is 2963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this