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-06-2004, 12:11 PM   #16 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
*blast*
__________________

Last edited by Valmont; 05-06-2004 at 12:58 PM.
Valmont is offline   Reply With Quote
Old 05-06-2004, 12:40 PM   #17 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Didn't notice that your msg was on the second page!!!
Thanks too.... much for soo.... much help!
I will try to implement the rest and would come back to you !
dirs is offline   Reply With Quote
Old 05-06-2004, 12:57 PM   #18 (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 I am removing my own code. Way too hasty. Sorry. I'll be back in a moment. It will be much simpler then it is now. And more logical as well.
__________________
Valmont is offline   Reply With Quote
Old 05-06-2004, 01:00 PM   #19 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Didn't notice that your msg was on the second page!!!
Thanks too.... much for soo.... much help!
I will try to implement the rest and would come back to you !
dirs is offline   Reply With Quote
Old 05-06-2004, 01:19 PM   #20 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Here we go:

In file: Main.cpp
Code:
#include "CardGames.h"
#include "blackjack.h"
#include "Player.h"
#include "card.h"


int main()
{
  
  BlackJack BJ;
  // Line below: A "plug in system". CardGames* can be plugged with
  // other types of card games as well.
  // Just derrive new forms of card games from class "CardGames".
  CardGames* CG = &BJ;  
  CG->Go();

  return 0;
}
In file: card.h
Code:
#ifndef CARD_H
#define CARD_H

#include <iostream>
#include <vector>

using namespace std; 


class Card
{
public:
  Card();
public:
  // Observe the naughty space between > > !
  vector<pair<int,int> > Deck;
  vector<pair<int,int> > GetDeck()
  {
    return Deck;
  }
private:
  enum { Club, Spade, Diamond, Heart} suit;
  
};

Card::Card()
{
  int cardtype;
  for (int i = 0; i < 52; i++)
  {
    // First 13 cards are spades, next 13 are diamonds etc.
    cardtype = i/13;
    switch(cardtype)
    {
      case 0: suit = Club;  break;
      case 1: suit = Spade; break;
      case 2: suit = Diamond; break;
      case 3: suit = Heart; break;
    }
    Deck.push_back(pair<int, int>(suit, 2+i%13));
  }

  //Demonstration of how to iterate through a "vector" holding "pair" objects.
  //It is only one more methods. See "void CardGames::Shuffle()" for the second
  //way of iterating through the vector. 
  for(vector<pair<int, int> >::iterator i = Deck.begin(); i != Deck.end(); ++i)
  {
    cout<< i->first <<" "<< i->second<<endl;
  }

} 

#endif //CARD_H
In file: Player.h
Code:
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
using namespace std;

class Player
{
public:
  void SetNumPlayers();
  int GetNumOfPlayers()
  { return nPlayers;  }

private:
  int nPlayers;
};

void Player::SetNumPlayers()
{
  cout<<"Enter the number of players that will attend the game: ";
  cin>>nPlayers;
}

#endif //PLAYER_H
In file: CardGames.h
Code:
#ifndef CARDGAMES_H
#define CARDGAMES_H

#include "card.h"
#include "Player.h"
#include <vector>
using namespace std;


// A plug-in system for various card games. 
// PURE virtual methods FORCE derrived class (various card games)
// to implement these minimal methods because the are always re-occuring.
class CardGames 
{
public:
  virtual void Deal() = 0;
  virtual void Go() = 0;

protected:
  Card theCards;
  Player thePlayers;
private:
  //Every card game needs a shuffle most likely, but not every 
  //card game needs to shuffle the same way.  
  virtual void Shuffle( vector<pair<int,int> > ) = 0;
};

#endif //CARDGAMES_H

In file: blackjack.h
Code:
#ifndef BLACKJACK_H
#define BLACKJACK_H

#include "CardGames.h"
#include "card.h"
#include <iostream>

using namespace std;

class BlackJack : public CardGames
{
public:
  void Deal();
  void Go();
private:
  void Shuffle(vector<pair<int,int> > theCards);
};

void BlackJack::Go()
{
  thePlayers.SetNumPlayers();
  Shuffle(theCards.GetDeck());
  Deal();
}

void BlackJack::Shuffle(vector<pair<int,int> > cards)
{
  pair<int, int> temp;
  int k;
  for (unsigned int i = 0; i < cards.size(); ++i)
  {
    k=(rand() % 52);
    temp = cards[i];
    cards[i] = cards[k];
    cards[k] = temp;

      // A simple but ugly test. Friend are "helping" me (not)
    //so I need to go soon :(.
    cout<<cards[i].first<<"_"<<cards[i].second<<endl;
  }  
} 

void BlackJack::Deal()
{
  cout<<"TODO: Implement the Deal() method here.";
}
#endif //BLACKJACK_H
__________________

Last edited by Valmont; 05-06-2004 at 02:14 PM.
Valmont is offline   Reply With Quote
Old 05-06-2004, 02:13 PM   #21 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Okidoky.

You can download the source files from my site:
http://home.tiscali.nl/~valmont/cpp/.../blackjack.zip
Just unzip it in a folder and then double clock on "Main.cpp".
Then compile it and then things will go smooth.
It is exactly the same code as posted above. Ready to run, just see what happens.

Here is a drawing of what is going on:


Let me explain what is going on here.
Your class CardGames is a "plug-in machine".
And your class BlackJack is module that can be plugged into your CardGames machine.
If you derrive more classes from the CardGames machine, then you are basically creating more modules for your CardGames machine. For example a class Poker. You name it.
Just implement the pure virtual methods (wich you see in the base class) in your derrived classes.

About pure virtual functions:

A pure virtual function is just like a ordinary virtual function. But instead of code, you add = 0 ; after the declaration.
So this time, you are not only able to override it in your derrived classed, but you MUST implement them in your derrived classes. A pure virtual function is a order:
Quote:
I am a PURE virtual function so you MUST and WILL implement me in classes derrived from me.
If you don't, the compiler will make your life a misery!
[u]About the vector from the <vector> header[u]

A vector is a sort of an array, but is very flexible and easy to handle. It is really simple but the simplicity is a bit obscured by another Standard Template Library item: the "pair". More on that coming up. But understand that learning the ways of vector is very important. It is much easier then using arrays and all. And occasionally even more efficient in terms of run-time-speed management. I fyou need help on this, then go ahead.

[u]About the "pair" from the <utility> header[u]
First of all this. Although the structure "pair" is defined in the include file <utility>, we don't need to add that header because we are using a "vector" (wich was defined in <vector>). In this very case, the <utility> header is automatically included.

Ok, so here is what it does:
A card is distinguished by its value and its suit. Since this is such a unity, I use a "pair" structure to implement the unity:
pair<int, int>(diamonds, 12)
But where do I store a "pair"? Well, I decided to store in a "vector".
That's why you see this strange code:
vector<pair<int, int> > Deck; Wich is the Deck that will hold all the pairs. And then:
Deck.push_back(pair<int, int>(suit, value)) ;
"push_back()" is a method from "vector". It pushed the next value in the vector. We "push_back" 52 times, so Deck has 52 pairs of "suit-value" items.
To view what is in it we do:
Code:
for(vector<pair<int, int> >::iterator i = Deck.begin(); i != Deck.end(); ++i)
	{
		cout<< i->first <<" "<< i->second<<endl;
	}
An "iterator" is a thingy that belongs to the vector (int this case). I load it with a Deck:
Code:
for(vector<pair<int, int> >::iterator i = Deck.begin(); }
And now we can use the iterator (wich we called "i") to traverse the vector.
This seems a bit weird:
Code:
cout<< i->first <<" "<< i->second<<endl;
But this is the way to access the "first value of a pair (suits) and the second value (value of the card).

[u]About main()[u]

I bet you didn't see such simple code in a long time. The main function is often called a "driver". Nevermind what that exactly is, but realize that if you make a library or libraries (and you are, since our classes are libraries in a sense), you want client coders to be able to code dynamically yet easy.
I think I achieved the easy part. More is not needed since that wasn't the assignment I believe.

[u]About You[u]

- Never ever touch the main(). Don't change a single thing!!!
Yes, you may remove (I forgot).
Code:
#include "Player.h"
#include "card.h"
Then forget main(). It doesn't exist anymore.

- I implemented your shuffle and your basic Deck setup.
Try to implement the rest.

- NO. You may NOT touch class "CardGames". Illegal too. Bzzt .
- NO, You may NOT touch class "Card". We defined card, we have Deck. So leave it.
- NO. You may NOT touch the class "Player" until you have a fully functional and correct black jack game. The game only needs to know how much players there are, and I provided that implementation as well. You could let player "1" (or "0" if you will) always be the "computer dealer". Up to you. But that has NOTHING to do with the "Player" class. So :th: to class "Player" for now.

- Yep, you only need black jack to implement the gamerules so it can play black jack.
a) Dealing cards is a rule.
b) Shuffling is a rule.
c) Checking for wagers.
c) Observing wich player number is should receive cards and all.
You name it.

I gotta work until late tomorrow. But I'll be back home eventually I hope. Then I'll be able to help you further.

Good luck!
__________________
Valmont is offline   Reply With Quote
Old 05-06-2004, 02:20 PM   #22 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Crap what typos. I'm done. ZZzzzZZ
__________________
Valmont is offline   Reply With Quote
Old 05-06-2004, 09:54 PM   #23 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Thanks for such a nice explaination!
would come back to u when i get problem!!
dirs is offline   Reply With Quote
Old 05-07-2004, 01:00 AM   #24 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by dirs
Thanks for such a nice explaination!
would come back to u when i get problem!!
Getting compiling errors:
:\blackjack\card.h(46) : error C2040: 'i' : 'struct std::pair<int,int> *' differs in levels of indirection from 'int'
c:\blackjack\card.h(46) : error C2446: '!=' : no conversion from 'struct std::pair<int,int> *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\blackjack\card.h(46) : error C2040: '!=' : 'int' differs in levels of indirection from 'struct std::pair<int,int> *'
c:\blackjack\card.h(48) : error C2227: left of '->first' must point to class/struct/union
c:\blackjack\card.h(48) : error C2227: left of '->second' must point to class/struct/union

Last edited by dirs; 05-07-2004 at 01:53 AM.
dirs is offline   Reply With Quote
Old 05-07-2004, 02:58 AM   #25 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
What program do you code with?

Send me screenies to valmont_programming@hotmail.com.
Make sure you convert them first to *gif images. Saves size.
colors are not important.

I'll be back late today and shouln't do this right now. Send them in the mean time.
__________________

Last edited by Valmont; 05-07-2004 at 03:51 AM.
Valmont is offline   Reply With Quote
Old 05-07-2004, 04:13 AM   #26 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by Valmont
What program do you code with?

Send me screenies to valmont_programming@hotmail.com.
Make sure you convert them first to *gif images. Saves size.
colors are not important.

I'll be back late today and shouln't do this right now. Send them in the mean time.
I use Microsoft Visual Studio!!
dirs is offline   Reply With Quote
Old 05-07-2004, 05:08 AM   #27 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
sent u mail just now!!
dirs is offline   Reply With Quote
Old 05-07-2004, 07:57 AM   #28 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Wich version of VC is that please?
__________________
Valmont is offline   Reply With Quote
Old 05-07-2004, 08:03 AM   #29 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Quote:
Originally posted by Valmont
Wich version of VC is that please?
VC++ 6.0
dirs is offline   Reply With Quote
Old 05-07-2004, 08:42 AM   #30 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Service Pack 5?

MSVC is not fully ISO/ANSI compliant. It's old you know.

Try this workaround:
Code:
typedef vector<pair<int,int> > VPairs;
	VPairs Deck;
And the change the few lines in card constructor to this:
Code:
for(VPairs::iterator i = Deck.begin(); i != Deck.end(); ++i)
	
	{
		cout<< (*i).first <<" "<< (*i).second<<endl;
	}
__________________

Last edited by Valmont; 05-07-2004 at 09:18 AM.
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 04:09 PM
pointer to function with class? Kportertx Standard C, C++ 5 04-11-2003 05:12 AM
class theory sde PHP 2 01-11-2003 12:48 PM


All times are GMT -8. The time now is 10:54 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





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