Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2004, 02:52 PM   #46 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Wanna have a cooler Shuffle method?
Here:
Code:
void BlackJack::Shuffle(vector<pair<int,int> > cards)
{
	int k;
	srand( (unsigned)time( NULL ) );
	for (unsigned int i = 0; i < cards.size(); ++i)
	{
		k=(rand() % 52);		
		swap(cards[i], cards[k]);
      cout<<cards[i].first<<"_"<<cards[i].second<<endl;
	}	
}
I'll get you an A+ if you work hard .
__________________
Valmont is offline   Reply With Quote
Old 05-08-2004, 01:52 AM   #47 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Still gettimg same 0 2 printouts
Didn't help!
dirs is offline   Reply With Quote
Old 05-08-2004, 07:21 AM   #48 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
K I will install VC6 sp5 in a few hours and check it out.

**...**

Ok, I've uploaded the vc6 files and the project file (*.dsw).
Download it from the link below and double click the dsw file. It will load the included sourcefiles with it. I found no errors by the way.

blackjackvc6
__________________

Last edited by Valmont; 05-08-2004 at 09:33 AM.
Valmont is offline   Reply With Quote
Old 05-08-2004, 11:01 AM   #49 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by Valmont
K I will install VC6 sp5 in a few hours and check it out.

**...**

Ok, I've uploaded the vc6 files and the project file (*.dsw).
Download it from the link below and double click the dsw file. It will load the included sourcefiles with it. I found no errors by the way.

blackjackvc6
I didn't get the errors earlier also but when u run the exe file it was just printing 0 2 for 52 times. But now I am getting all the cards. But I think we have to modify the card class a bit because it doesnt display J,Q,K, and A. Also it doesnt print out the card type. we can use ASCII 32 characters to print card type.
dirs is offline   Reply With Quote
Old 05-08-2004, 11:18 AM   #50 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
cout<<cards[i].first<<"_"<<cards[i].second<<endl
In the above statementwhat are we actually trying to print. Is it two cards that are given to one player or?
dirs is offline   Reply With Quote
Old 05-08-2004, 12:09 PM   #51 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I made a mistake: I forgot to pass by reference.
Throw everything away. Get ANOTHER update from the link I posted two posts ago (blackjackvc6).

Tireness I suppose. No rest this weekend. Can't concentrate.
We are printing the shuffled cards to prove they are shuffled. This time the orignal deck is shuffled, NOT a copy of it like it was in the previous code. My bad my bad my bad.

Now this:
Quote:
But I think we have to modify the card class a bit because it doesnt display J,Q,K, and A. Also it doesnt print out the card type. we can use ASCII 32 characters to print card type.
It does print J, Q, and K. Every value higher then 9 is to be considered a value "10", but value 14 is to be considered the special case "A".

So the basic setup is good. What you want is "luxury". I'd wait with that until we have this setup right. If you download from the above you can move on.
But don't implement the kool print stuff yet. Try to implement "Deal()" first.

Quote:
cout<<cards[i].first<<"_"<<cards[i].second<<endl
In the above statementwhat are we actually trying to print. Is it two cards that are given to one player or?
An output like 2__13 means Diamond K. 0_4 means Club 5. And so forth. So basically we do have an output, but not relevant yet. You can make a much cooler output later. Or assign even graphix pictures to it in a much later stage, if you know how to deal with graphix functions in c++. You can read the card.h for the suit codes.
Once again: the code "cout<<cards[i].first<<"_"<<cards[i]" doesn't exist anymore int he updated zip. We are using the deck directly now. Check it out.

Do yo understand the "pair" structure?
in pair::first the suit is stored. In pair::second the value is stored. The code
Code:
cout<<pair::first<<"_"<<pair::second
will merge the two values into one string. That's all. These pair values are stored in a vector for easy handling.
If you don't understand I will make a drawing and post it here.
__________________
Valmont is offline   Reply With Quote
Old 05-08-2004, 01:28 PM   #52 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
This vector concept is quite new to me. Do you know if their is any good site to read more about vectors?
dirs is offline   Reply With Quote
Old 05-08-2004, 02:59 PM   #53 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Will this help:
Code:
#include <iostream>
#include <vector>
#include <string>

//Disabling annoying but innocent warning about long string names.
#pragma warning(disable: 4786 ) 
 
using namespace std;

int main()
{
  vector<string> myIntVector;
  myIntVector.push_back("I ");
  myIntVector.push_back("am a ");
  myIntVector.push_back("string ");
  myIntVector.push_back("in ");
  myIntVector.push_back("a ");
  myIntVector.push_back("vector!");

  //One way to to print the contents. size() function may be slow. for(unsigned int i = 0 ; i<myIntVector.size(); ++i)
    cout<<myIntVector[i];

  cout<<endl;
  
  //The other way to print the contents. It can be faster. //Besides, introducing here the iterator. Might give us interesting //options later. for(vector<string>::iterator j = myIntVector.begin() ; j != myIntVector.end() ; ++j)
    cout<<*j;
   
  cout<<endl<<endl;

  /*-----------------------------------------------*/ //Instead of creating a string vector, we create  a "string/uint"-pair. //We have to tell CardVector.push_back that we enter a "pair". //WATCH OUT: you need to keep a "space" between the "> >" ! //Lets make a shorter name typedef vector<pair<string, unsigned int> > PairVector; //<--Space between > > //Now create a vector wich can hold pairs of "string/numbers".
  PairVector CardVector; 

  //And fill the vector with these pairs..
  CardVector.push_back( pair<string, unsigned int>("Club", 3) );
  CardVector.push_back( pair<string, unsigned int>("Club", 12) );
  CardVector.push_back( pair<string, unsigned int>("Spade", 7) );
  CardVector.push_back( pair<string, unsigned int>("Heart", 2) );

  //One way to to print the contents. size() function may be slow. //Observe how to print the suits (first) and the values (second). //So there are lot of "first/second"-pairs stored in CardVector.   for(unsigned int k = 0 ; k < CardVector.size(); ++k)
    cout<<CardVector[k].first<<" "<<CardVector[k].second<<endl;

  cout<<endl;
  
  //Here is the other way to traverse the list of vectors wich hold  //"pairs of data". for(PairVector::iterator l = CardVector.begin() ; l != CardVector.end() ; ++l)
    cout<<l->first<<" "<<l->second<<endl; 

  cout<<endl;

  return 0;
}
This covers all you need to know right now.

Vectors will make your life easey (and STL) in general. Vectors avoid you the mess with (multidimensional) arrays. No cleaning up is needed, adding and removing elements can be done without fuss.

__________________

Last edited by Valmont; 05-08-2004 at 03:33 PM.
Valmont is offline   Reply With Quote
Old 05-09-2004, 04:15 AM   #54 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Here is some updated code. Has a cleaner output. And the initial deal method is implemented. Check it out:
UPDATED CODE
__________________
Valmont is offline   Reply With Quote
Old 05-09-2004, 09:50 AM   #55 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by Valmont
Here is some updated code. Has a cleaner output. And the initial deal method is implemented. Check it out:
UPDATED CODE
Quite many changes in the code, still trying to understand everything.
dirs is offline   Reply With Quote
Old 05-09-2004, 12:48 PM   #56 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Don't worry, I have code wich you will like more.
I'll send it soon.
The setup is different.

Different code
__________________
Valmont is offline   Reply With Quote
Old 05-10-2004, 03:49 AM   #57 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by Valmont
Don't worry, I have code wich you will like more.
I'll send it soon.
The setup is different.

Different code
In Deck class
theDeck.erase, .begin etc. are all these the methods in Vector?
In Card class
Card(pair<string, unsigned> crd) : aCard(crd) {}
: aCard(crd) Could you explain this?
dirs is offline   Reply With Quote
Old 05-10-2004, 09:59 AM   #58 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by dirs
In Deck class
theDeck.erase, .begin etc. are all these the methods in Vector?
In Card class
Card(pair<string, unsigned> crd) : aCard(crd) {}
: aCard(crd) Could you explain this?
In Black jack game a hand consist of two card.
Can we just hard code in bjplayer.h or blackjack.h?
I am extremely sorry but I still dont understand some part in the code. You have been already so helpful but i would be very grateful to u if u could add small comments at the end. Then it would be very easy for a new beginner like me.
Thanks and hope for a fast reply!
dirs is offline   Reply With Quote
Old 05-10-2004, 04:22 PM   #59 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Sure.
Almost ready.
__________________
Valmont is offline   Reply With Quote
Old 05-10-2004, 08:38 PM   #60 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Here it is. I think you'll like it .
It is complete and working. You can bet some mone on it .
blackjacvc6final.zip
  • Game ends when player has no more money.
  • Card deck is recreated and shuffled if holds less then 33% of the cards. But game continues if player still has money.
  • Class "card.h" defines a pair<string, unsigned> aCard. Since a card is known for its two-unit "suit" and "value".
  • But it is the class Card that actually IS a single card and therefore is the complete definition of a card:
    it has a pair-unit and some methods (member functions). These things makes a card a card.
    Think of a human. It doesn't have only arms and legs. But also some behaviour (give cup of tea), and properties (sleeping, working).
  • The card defines also an overloaded operator "<<".
    Such an operator is naturally used or output to screen (on our example), but by overloading it, it will show a neat output like:
    Heart Ace
    instead of
    3 14
  • The deck: in class Deck.h naturally.
    The core of creating a deck is placing all the individual "cards" (as defined in card.h) in a vector. Here is the core of the deck definition:
    vector<Card> theDeck;
    theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) ));

    Besides creating a deck of cards, it also defines some minimal but highly usefull methods as well. Obviously because a "deck of cards" has a behaviour as well.
Lets analyze what happens in class Deck.h:
First of all, Deck is NOT a derrived class from class Card. Why is that? Well easy: a deck IS NOT A KIND OF a card.
Therefore: a deck IS NOT a card. So NO derrivation from card it is.

In the Deck class, I could have (and should have) created a Deck in its constructor. But instead I made an initialisation method called "CreateDeck". I did this so you can create new decks on-the-fly in the game (in BJ we fetch a new deck if deck has less than 33% of cards). But you may ask yourself if this is really OO programming ! Nevermind that for now though.

The method Deck::Show(ostream &os) belongs in the deck class indeed since the deck is responsible on how it shows itself to the public. Just like you or me are in real life.
So now you know why Shuffle() and RemoveTopCard are in the Deck class too .

Quote:
In Card class
Card(pair<string, unsigned> crd) : aCard(crd) {}
: aCard(crd) Could you explain this?
I was just on my way to do that .

Lets analyze theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) )) for a moment again.
Usually when you put something in a vector you do:
vector myVec<int>;
myVec.push_back(5);

Easy huh? :rock:
But our deck doesn't store integers. It stores cards. And our card was defined in the class Card.
So I could have done easely:
Code:
Card c:
theDeck.push_back(c);
But before I do that, I need to set the card with a card -suit and value pair:
Code:
Card c(pair<string,unsigned>(suit, 2+i%13));
theDeck.push_back(c);
But I don't need card objects, so I use a trick wich does the same thing in one line. In that line, first the constructor of card is called (loads the card with the values passed on) and then I load my Deck vector with that particular card:
Code:
vector<Card> theDeck;
theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) ));
I can do this because my Card constructor accetpts a pair value. This is a typical occasion where a class (like our Card class) doesn't have a default constructor defined by us, since it is of no use. Instead we have that other constructor:
Code:
Card( pair<string, unsigned> crd );
Observe how this is just a SIMPLE constructor wich accepts a single value:
Code:
//EXAMPLE, NOT IN OUR ORIGINAL CODE
//observe how simple this constructor is actually by comparing
//a fictional case:
WeirdCardClass(int crd) { }
Card( pair<string, unsigned> crd ) { }
Quote:
In Deck class
theDeck.erase, .begin etc. are all these the methods in Vector?
Yes.
The vector, wich is part o the STL (Standard Template Library) offers lot of such method. You only have to know them and know a bit how to use them. The vector itself will manage the annoying things like memory allocation, memory freeing etc..
That's why "we" love the STL so much. In fact, if you think of using the good old array again, try to use vector instead.
The vector has template arguments. So you could dump just about anything in a vector. Even vectors in vectors. That's a nice alternative to creating multidimensional arrays. But without fuss.
And don't even think an array executes faster. In complex situations the vector methods may be even faster. You won't lose, that you can count on it in serious applications.
Here is another example of the power of STL (wich we did use in our code):
Code:
void Shuffle() 
	{
		static bool seeded;
		if (!seeded)
		{
			srand(time(0));
			seeded = true;
		}
		random_shuffle(theDeck.begin(), theDeck.end());
	}
I am talking about random_shufle.
This requires the <algorithm> header to be included.

Okidoky. It is VERY early here, and I just created the proggy from scratch for you. You deserve it. Now I need a cappucino, and then to work. I will occasionally lurk for this site at work as I do usually, so if you have questions, ask them anytime.
Be nice and tell me what grade you got for this assignment .

Notice that we don't use all the methods I created in Deck.h and card.h. One rule is:
1) Make your classes minimal.
But the second rule is:
2) Make your classes functional.
So that's why I added a few common occuring methods like accessor methods. These end with the const keyword: to make sure we can't modify our content. That is especially important when we return a reference.

- Val -
__________________

Last edited by Valmont; 05-10-2004 at 11:24 PM.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
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
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
to put data methods inside class or not? sde Java 2 05-25-2004 05:09 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 06:12 AM
class theory sde PHP 2 01-11-2003 01:48 PM


All times are GMT -8. The time now is 11:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting