Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,175 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,380 people online right now. Registration is fast and FREE... Join Now!




Blackjack

2 Pages V  1 2 >  
Reply to this topicStart new topic

Blackjack, Getting random #'s

freddyjones39
23 Jun, 2008 - 02:38 PM
Post #1

New D.I.C Head
*

Joined: 23 Jun, 2008
Posts: 4

I am just working on a personal project for the summer to keep my brain working (so this isn't emergent). I am writing a blackjack program, but I have no idea where to start as far as the program "shuffling" the cards, as to deal at random. I have looked through some other code, but couldn't make sense of it. Any ideas would be greatly appretiated.
Freddy
User is offlineProfile CardPM
+Quote Post

Arenlor
RE: Blackjack
23 Jun, 2008 - 02:41 PM
Post #2

New D.I.C Head
*

Joined: 26 Apr, 2006
Posts: 31


My Contributions
QUOTE(freddyjones39 @ 23 Jun, 2008 - 03:38 PM) *

I am just working on a personal project for the summer to keep my brain working (so this isn't emergent). I am writing a blackjack program, but I have no idea where to start as far as the program "shuffling" the cards, as to deal at random. I have looked through some other code, but couldn't make sense of it. Any ideas would be greatly appretiated.
Freddy

There are 52 cards in a deck, so an array of 52 cards randomly choosing them from the deck and marking it as used.
User is offlineProfile CardPM
+Quote Post

skater_00
RE: Blackjack
23 Jun, 2008 - 02:44 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
You will need the rand() function for this, plus eventually the srand() function.

The following articles may help you with this:

rand(): http://www.cplusplus.com/reference/clibrar...tdlib/rand.html
srand(): http://www.cplusplus.com/reference/clibrar...dlib/srand.html

Hope this helps..
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Blackjack
23 Jun, 2008 - 03:35 PM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
I was gonna give a few pointers, but I decided to write the whole code. I don't know why.

cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// pass a pointer to the deck and the name of the suit
void addSuit (vector <string> &deck, string suitName)
{
for (int i = 1; i <= 13; i++)
{
string str; // string to add to the deck
// put the string together
if (i < 10) str = i+48; // ASCII value of the number
if (i == 10) str = "10";
if (i == 11) str = "Jack";
if (i == 12) str = "Queen";
if (i == 13) str = "King";
str += " of " + suitName;
// add the string to the deck
deck.push_back (str);
}
}

int main ()
{
// create the deck
vector <string> deck;
// pass the deck and the suit name to add
addSuit (deck, "Hearts");
addSuit (deck, "Clubs");
addSuit (deck, "Spades");
addSuit (deck, "Diamonds");

// shuffle the deck
random_shuffle (deck.begin(), deck.end());

// create an iterator, to use for printing
vector <string>:: iterator It;
// loop through the deck
for (It = deck.begin(); It != deck.end(); ++It)
cout << *It << endl; // print the card value

cin.get (); // pause for input
return EXIT_SUCCESS;
}

Was it helpful? [hint, hint] wink.gif
User is offlineProfile CardPM
+Quote Post

skater_00
RE: Blackjack
23 Jun, 2008 - 03:47 PM
Post #5

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
Blah, I got the thanks.. tongue.gif
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Blackjack
23 Jun, 2008 - 03:50 PM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
I got it too tongue.gif

I submitted it as a snippet, others might find it kinda useful smile.gif

Just a quick note, you might want to seed the random number with the system clock, like so:
cpp
#include <iostream> // input/output stream
#include <string> // string
#include <vector> // vector
#include <algorithm> // random_shuffle()
#include <ctime> // time()

using namespace std;

// pass a pointer to the deck and the name of the suit
void addSuit (vector <string> &deck, string suitName)
{
for (int i = 1; i <= 13; i++)
{
string str; // string to add to the deck
// put the string together
if (i < 10) str = i+48; // ASCII value of the number
if (i == 10) str = "10";
if (i == 11) str = "Jack";
if (i == 12) str = "Queen";
if (i == 13) str = "King";
str += " of " + suitName;
// add the string to the deck
deck.push_back (str);
}
}

int main ()
{
// create the deck
vector <string> deck;
// pass the deck and the suit name to add
addSuit (deck, "Hearts");
addSuit (deck, "Clubs");
addSuit (deck, "Spades");
addSuit (deck, "Diamonds");

// shuffle the deck
srand (time(NULL)); // seed the random number with the system clock
random_shuffle (deck.begin(), deck.end());

// create an iterator, to use for printing
vector <string>:: iterator It;
// loop through the deck
for (It = deck.begin(); It != deck.end(); ++It)
cout << *It << endl; // print the card value

cin.get (); // pause for input
return EXIT_SUCCESS;
}
smile.gif

This post has been edited by gabehabe: 23 Jun, 2008 - 04:14 PM
User is offlineProfile CardPM
+Quote Post

freddyjones39
RE: Blackjack
24 Jun, 2008 - 07:39 PM
Post #7

New D.I.C Head
*

Joined: 23 Jun, 2008
Posts: 4

The code helps a lot, but what is the " deck.push_back(str), is push_back a command?
sorry, I'm still pretty new at this.
User is offlineProfile CardPM
+Quote Post

Einherjar
RE: Blackjack
24 Jun, 2008 - 09:42 PM
Post #8

D.I.C Head
**

Joined: 10 Feb, 2008
Posts: 73


My Contributions
QUOTE(freddyjones39 @ 24 Jun, 2008 - 11:39 PM) *

The code helps a lot, but what is the " deck.push_back(str), is push_back a command?
sorry, I'm still pretty new at this.


push_back() is a function of the STD vector. Essentially, he's putting the string str onto the end of the vector.

Edit: And by STD I believe I meant STL

This post has been edited by Einherjar: 24 Jun, 2008 - 09:43 PM
User is offlineProfile CardPM
+Quote Post

polymath
RE: Blackjack
25 Jun, 2008 - 08:39 AM
Post #9

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
I don't think vectors are needed here, but good code anyways!
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Blackjack
25 Jun, 2008 - 12:24 PM
Post #10

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
QUOTE(polymath @ 25 Jun, 2008 - 05:39 PM) *

I don't think vectors are needed here, but good code anyways!

Why? It's much more efficient than an array.
User is offlineProfile CardPM
+Quote Post

polymath
RE: Blackjack
26 Jun, 2008 - 05:45 AM
Post #11

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
true true

vectors sometimes end up overcomplicating things, though.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Blackjack
26 Jun, 2008 - 09:41 AM
Post #12

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Name one.

Everything you can do to an array, you can do to a vector.

Plus, vector are more flexible.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 10:40AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month