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
Old 05-04-2004, 10:00 AM   #1 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Accessing functions from base class

Blackjack class is derived from card class .
I have problem with:
void blackjack:ealCards() function.
I want to use function deal_hands from card class to set the value of pcard1 pcard2, dcard1 and dcard2.
How can I do that???

Code:


#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "playingcards.h"
#include "blackjack.h"

int main()

{

card deck[52];
blackjack bj;
card hand[players][cards_pl];
deck[52].init_deck(deck);
cout << "Unshuffled card deck: ";
deck[52].display_deck(deck);
deck[52].shuffle(deck);
cout << endl << "Shuffled card deck: ";
deck[52].display_deck(deck);
deck[52].deal_hands(deck, hand);
deck[52].display_hands(hand);


//srand(time(NULL)); // seed random number
// bj.Intro();
bj.DealCards();
cout << endl;
return 0;

}
// This function prints a single card
void card::display_card(const card* cd)
{
// Check the value of the card, and the 4 special values
// which are for Ace, Jack, Queen or King
switch (cd->value)
{
case 11: cout << "J"; break;
case 12: cout << "Q"; break;
case 13: cout << "K"; break;
case 14: cout <<"A"; break;
default: cout << cd->value;
}
// Check the card's suit
switch (cd->suit)
{
case clubs: cout << char (5); break;
case diamonds: cout << char (4); break;
case hearts: cout << char (3); break;
case spades: cout << char (6); break;
}

cout << " ";
}
// This function initializes a deck of cards
void card::init_deck(card d[])
{
for (int i = 0; i < 52; i++)
{
// First 13 cards are spades, next 13 are diemonds etc...
switch (i / 13)
{
case 0: d[i].suit = clubs; break;
case 1: d[i].suit = diamonds; break;
case 2: d[i].suit = hearts; break;
case 3: d[i].suit = spades; break;
}
// Assign the card value
d[i].value = 2 + i % 13;
}
}


// This function prints the deck
void card::display_deck(const card d[])
{

for (int i = 0; i < 52; i++)
{
// Print a newline after each 13 cards (for a better look)
if (i % 13 == 0)
cout << "\n";
display_card(&d[i]);// Print cards using display_card
}
cout << endl;
}

// This function shuffles the deck
void card::shuffle(card d[])
{
for (int i = 0; i < 52; i++)
{
// Shuffles the cards using a random number generator, each
// card is replaced with some random card from the same deck
int k = rand()%52;
card temp = d[i];
d[i] = d[k];
d[k] = temp;
}
}

// This function deals the cards to the players
card::deal_hands(const card d[], card h[players][cards_pl])
{
int next_player = 0, next_card = 0;
for (int i = 0; i < 52; i++)
{
// Fill the player's hands equally, card by card
h[next_player][next_card] = d[i];
next_player++;
if (next_player > players)
{
next_player = 0;
next_card++;
}

}
}

// This function prints the hands
void card::display_hands(const card h[players][cards_pl])
{
cout << endl << "The hands are: "<< endl;
// Print the hands of all player, each in separate line
for (int i = 0; i < players; i++)
{
cout << "Player " << i << ": ";
for (int j = 0; j < cards_pl; j++)
{
display_card(&h[i][j]);
}
cout << endl;
}
}


int card::GetCards()
{
int x;

x = 1 + rand() % 11;

return x;
}

//**************
void blackjack:ealCards()
{

Ctotal = 0;
pcard1 = card::deal_hands(0,card [0][0]);
pcard2 = card::deal_hands(0,card [0][1]);
dcard1 = card::deal_hands(0,card [1][0]);
dcard2 = card::deal_hands(0,card [1][1]);

/* cout << "You have " << money << " dollars to play with\n\n";

if (money <= 0)
{
cout << "You are out of money...., GAME OVER!" << endl;
exit(1);
}

cout << "What is your bet? ";
cin >> bet;
if (bet > money)
{
cout << "We do not give credit to the likes of you...\n\n";
DealCards();
}

system("CLS");*/




if (pcard1 == 1 || pcard1 == 11)
{
cout << "\n\n" << "Your first card is an ACE \n";
}
else
{
cout << "\n\nyour first card is " << pcard1 << endl;
}

if (pcard2 == 1 || pcard2 == 11)
{
cout << "Your second card is an ACE\n\n";
}
else
{
cout << "your second card is " << pcard2 << "\n\n";
}

Ctotal = pcard1 + pcard2;





if (dcard1 ==1 || dcard1 == 11)
{
dcard1 = 1;
cout << "The dealer is showing an Ace \n\n";
}
else
{
cout << "The dealer is showing a " << dcard1 << "\n\n";
}





if (pcard1 == 1 || pcard1 == 11)
{
Ace();
}

else if (pcard2 == 1 || pcard2 == 11)
{
Ace();
}




Ctotal = pcard1 + pcard2;
cout << "You have " << Ctotal << " total so far\n\n";
DrawCard();

}





void blackjack:rawCard()
{
cout << "Would you like to (H)it or (S)tand? ";
cin >> draw;

switch (draw)
{
case 'h':
drawcard = card::GetCards();
cout << "You drew a " << drawcard << "\n\n";
Ctotal += drawcard;
cout << "your total is " << Ctotal << "\n";
if (Ctotal > 21)
{
cout << "BUST! \n\n";
/* money -= bet;
cout << "you lost " << bet << " dollars\n\n";*/
DealCards();
// system("CLS");
}
else
{
system("CLS");
DrawCard();

}
break;
case 's':
Dealer();
break;

default:
cout << "Please use h or s...thank you\n\n";
DrawCard();


}

}






void blackjack:ealer()
{
int Dtotal; // dealers card total
int Ddraw; // dealer draw card
Dtotal = 0;
system("CLS");
cout << "You have " << Ctotal << "\n\n";
cout << "The dealer turns over his card....\n";
cout << "The dealers first card was a " << dcard1 << "\n";
cout << "The dealers second card is " << dcard2 << "\n\n";
Dtotal = dcard1 + dcard2;
cout << "The dealer has a total of " << Dtotal << "\n";
while (Dtotal <= 17)
{
Ddraw = GetCards();
cout << "the dealer draws again...\n";
cout << "the dealer drew a " << Ddraw << "\n";
Dtotal += Ddraw;
cout << "The dealer now has " << Dtotal << "\n";
}

if (Dtotal > 21)
{
cout << "Dealer busts...you win" << "\n\n";
/*money += bet;*/
DealCards();
}
else
{
cout << "The Dealer stands on " << Dtotal << "\n\n";
cout << "Dealer has " << Dtotal << "\n";
cout << "You have " << Ctotal << "\n\n";
if (Dtotal < Ctotal)
{
cout << "You win " <<"\n\n";
/*bet += money;*/
DealCards();
}

if (Dtotal == Ctotal)
{
cout << "PUSH..No winner\n";
DealCards();
}


else
{
cout << "Dealer wins \n";
/*money -= bet;*/
system("CLS");
DealCards();
}
}


}


void blackjack::Ace()
{
int answer;

if (pcard1 == 1 || pcard1 == 11)
{
cout << "would you like to use your first ace as a 1 or 11\n";
cin >> answer;

switch (answer)
{
case 1:
pcard1 = 1;
break;
case 11:
pcard1 = 11;
break;
default:
cout << "you need to choose 1 or 11\n\n";
Ace();
}
}

if (pcard2 == 1 || pcard2 == 11)
{
cout << "would you like to use your second ace as a 1 or 11\n";
cin >> answer;

switch (answer)
{
case 1:
pcard2 = 1;
break;
case 11:
pcard2 = 11;
break;
default:
cout << "you need to choose 1 or 11\n\n";
Ace();
}
}

if (pcard1 == 11 && pcard2 == 11)
{
cout << "That would be 22! and you would bust\n";
cout << "play again";
Ace();
}

Ctotal = pcard1 + pcard2;

if (Ctotal == 21)
{
system("CLS");
cout << "Blackjack!" << "\n\n";
/* bet *=1.5;*/
cout << "you won "/*<< bet <<*/ "\n";
/* money += bet; */
DealCards();

}
}


--------------------------------------------------------------------

code for playingcards.h:
const int players = 2;// Number of players
const int cards_pl = 2;// Number of cards per player
randomize();

class card
{
private:
Suit suit; // The card's suit: clubs, diamonds, hearts, spades
int value;// The value of the card: 2 - 10, jack, queen ,king, ace

public:
void display_card(const card*);
void init_deck(card []);
void shuffle(card []);
void display_deck(const card []);
int deal_hands(const card [], card [players][cards_pl]);
void display_hands(const card [players][cards_pl]);
int GetCards();

};
--------------------------------------------------
code for blackjack.h:

class blackjack : public card
{

private:
int pcard1, pcard2; // player cards
int dcard1,dcard2; // dealers cards

char draw; //draw yes or no?
int drawcard; // draw card
int Ctotal; // card totals
int bet; // bet
char name[20];

public:

void DealCards();
void DrawCard();
void Intro();
void Dealer();
void Ace();

};
dirs is offline   Reply With Quote
Old 05-04-2004, 12:16 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
oh my lord, where's that post size parameter! lol ..

welcome to the site none the lesss..

advice from me ( altought i can't help on your problem ) .. use the [code ] tags for your code, .. and people might be more likely to help if you make your posts smaller .. it was almost too much of a chore to scroll down this far to reply!!

good luck
__________________
Mike
sde is offline   Reply With Quote
Old 05-04-2004, 12:29 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
I am running it now.
As usual, more than one thing is needed before it even compiles...
__________________
Valmont is offline   Reply With Quote
Old 05-04-2004, 01:31 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
I've removed your code since the amount of bugs and incompleteness is beyond repair or reproduction.
So here is the basic setup for your question (wich is actually BAD programming so read on:

Besides placing your sourcecode between the "CODE" tags, also disable smilies when posting code. You can see the option just below the post-editor.

Code:
//Below Main.cpp

#include "blackjack.h"

int main()
{
  blackjack bj;
  bj.SetTestValue(12);
  bj.PrintTestValue();

  return 0;
}

//Below playingcards.h


#ifndef PLAYINGCARDS_H
#define PLAYINGCARDS_H

#include <iostream>

using namespace std; 

class card
{
public:
  int m_testvalue;
  void PrintTestValue()
  {
    cout<<m_testvalue<<endl;
  }
};

#endif //PLAYINGCARDS_H

//Below blackjack.h

#ifndef BLACKJACK_H
#define BLACKJACK_H

#include "playingcards.h"
#include <iostream>

using namespace std;

class blackjack : public card
{
public:
  void SetTestValue( int n )
  {
    m_testvalue = n;
  }

};

#endif //BLACKJACK_H
The problem now is that int m_testvalue is made public. Basically, members should be declared private!
So the answer to your question is: you can't.
That's because the program design isn't very good.

SO...
The way to solve this is NOT to make "blackjack" a derrived class from "card". Instead use aggregation. Basically you compose the class "blackjack" by adding a "card" object to the "blackjack" class.

So here is the code (observe card TheCards) . That is the aggregate!:
Code:
{
private:
  int m_testvalue;
public:
  void Set(int n)
  {
    m_testvalue = n;
  }

  int Get()
  {
    return m_testvalue;
  }
  
  void PrintTestValue()
  {
    cout<<m_testvalue<<endl;
  }
};

//-------------------------------------------------------------------

#include "playingcards.h"
#include <iostream>

using namespace std;

class blackjack 
{
public:
  void SetTestValue(int n)
  {
    TheCards.Set(n);
  }

  void PrintTestValue()
  {
    cout << TheCards.Get() << endl;;
  }

private:
  card TheCards;

};
You can use the same Main() function.
__________________

Last edited by Valmont; 05-04-2004 at 02:14 PM.
Valmont is offline   Reply With Quote
Old 05-05-2004, 09:47 AM   #5 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Thanks for the replies and comments! First time user on the forum so dont know much about rules!!
I have not heard about aggregate class before. Actually in the assignement we are supposed to make base class and inherited class.
Cant i somehow return array from the function below
card::deal_hands(const card d[], card h[players][cards_pl])

and use it in void blackjack::DealCards() function to get the value of pcard1, pcard2 etc.

Thanks!
dirs is offline   Reply With Quote
Old 05-05-2004, 10:03 AM   #6 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
BTW, Do u have any or know of any good card game programme in C++??
Thanks!!!
dirs is offline   Reply With Quote
Old 05-05-2004, 10:40 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Well dirs,
You have to think about your design.
I am a stranger to your code and a stranger to your vision and concept.
So when you type...
Code:
class BlackJack : public card {}
...then what are you telling this stranger?
The first two concrete questions I have (and therefore you too!) is:
Quote:
1) Is "BLackJack" a game, or only the rules of the game, but not its assets? What is a "BlackJack".
2) Is (a) "BlackJack" a specialisation of (a) "card"?
Do you see the "weirdness" that my questions imply?
The Black Jack game is NOT a specialisation of cards if I may use some common sense. The Black Jack game IS NOT A CARD. Cards are one of the entities involved in the Black Jack game. Just like "rules" are one of the enities of the Black jack game.

Give me a minute so I can make a *.gif drawing to make it visible.
Here I am again. Here is the drawing:


Let me give you a view on what would be a better design. Once a decent design is up, coding becomes easy.
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 11:24 AM   #8 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Thanks for the help so far and taking interest in the problem!!
The base class card defines the basic functions in any card game e.g. shuffling the cards, dealing the cards to the players.
Any card game uses these basic functions and also it has its own functions to implement the different rules it have.
dirs is offline   Reply With Quote
Old 05-05-2004, 11:39 AM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
Originally posted by dirs
Thanks for the help so far and taking interest in the problem!!
The base class card defines the basic functions in any card game e.g. shuffling the cards, dealing the cards to the players.
Any card game uses these basic functions and also it has its own functions to implement the different rules it have.
AAAAH. You should have told me this before! I just made this new image. I wasn' finished yet .
Anyway here is the image:


But this one doesn't satisfy. But it is much better to start with so you can see how "cards" "Players" and the actual game "black jack" are NOT related!
I need a ciggy right now. I'll be back reeeal soon. But in the mean time, see if you can split "my" BlackJack class so "Deal" and "Shuffle" belongs to any card game.
So Make a baseclass called "ICardGame" and derive class "BlackJack" fromt that class.
The "I" stands for interface.
What you have to think about is that the way you deal for the Black Jack game differs, then the way you deal for lets say a Poker game.
But the way you Shuffle the cards could be the same.
I don't know these games.
So make "void Deal()" a pure virtual method in "ICardGame"!

I am having a tea and a cig now . Good luck!
brb
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 11:42 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
By the way, you introduced a vision. GOOD!
Now introduce your concept: How would you like your product to do things in a bit more detail. Who are and what is important according your excellent vision. How do or what do they do to eachother?

Once you offered this (with simple words!), we can go to the design phase. You will notice how smooth the things will go. Don't worry about your coding skills. I am right here. That comes later.
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 12:23 PM   #11 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Thanks Valmont for help! U are quite good in explaining things!

The base class/es should be able to shuffle the deck, drawing cards, inspecting cards in a hand,adding/removing/playing cards to/from a hand.
Here are the rules for blackjack game:
A natural 21 means an automatic win
If both players hand's are equal, then it's a tie
The game is played with a standard 52 card deck.
J,Q,K all count as 10. An Ace can count as an 11 or a 1.
Actions in blackjack game:
Possible actions on a hand are:
Stand - don't draw more cards. Must be last action on hand.
Hit - draw an additional card.
Double down - draw one additional card only. Allowed if initial value is 9,10,11.
Must be last action on hand.
Split - split initial pair of cards into two hands to be played separately. The
hands are completed by receiving an additional card from the deck for each hand.
dirs is offline   Reply With Quote
Old 05-05-2004, 12:37 PM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Nonono, that is not a concept.
These are details. We are not interested in details. I am not interested in rules. We want to work towards a design. A design is a simple protype from wich we can build up code.
But before we can make a design, we need a concept. After the concpept I do an analysis. After the analysis, we are ready for the design. Then after that we will code. Then when we code, we refine our analysis and design along the way. We can't be perfect. Happens on multi-billion dollar bridge engineering projects as well.

Later more on that!

So answer these questions first:
1) Who are or what is involved.
Example: cards are involved. The Black Jack game is involved. Players are involved. Any Card Game could be involved.
2) What are these things?
Example: Black Jack game is a special Card Game. It's speciality is recognized by its typical rules basically. Players have a name and have Cards and Play Card Games. Could be Black jack.
3) How do they interact?
Example: Players receive card(s). The deck of cards needs to be shuffled. Cards don't want to know anything about wich game they are involved with. Players don't have to know what game exactly they are playing (believe it, it is true!).

Etcetera. Think like this. Pretend you playing games with me and your buds. What happens over there on an abstract level (in general). Write it down. No pictures, no code, no heavy details. Your best but simple english.

Can you think of more relevant questions?

Have you figured out what we are doing? We are communicating closely. That's how we setup a concept. Together. But you have the ideas. Communicating is the very first step, and a "must" to build up the concept.

NOTE: From now on we both need to work hard. I'll be there, but make sure are there too.
__________________

Last edited by Valmont; 05-05-2004 at 01:40 PM.
Valmont is offline   Reply With Quote
Old 05-05-2004, 04:00 PM   #13 (permalink)
pe666o
Registered User
 
pe666o's Avatar
 
Join Date: Apr 2004
Posts: 7
pe666o is on a distinguished road
Send a message via ICQ to pe666o
I think the direct answear to your question is to use a reference to your base class:
Try smth like:
Base::SomeBaseFunction(..);
or I forgot( damn havent used C++ lately)
there might be pointer:
base->baseFunction();
anyway if you know the name of the base class, you can always make a pointer to it and call functions, however look out for virtual functions!!

Looks like you are in C++ class )
and looks like this staff is new for you!
just go and find as much info on C++ as you can, it will save you much time on later assignments!
pe666o is offline   Reply With Quote
Old 05-06-2004, 10:12 AM   #14 (permalink)
dirs
Registered User
 
Join Date: May 2004
Posts: 40
dirs is on a distinguished road
Just now looked at the question you wrote.
Players dont have to know about which game they are playing , seems very strange.Do you mean to say that player class would be a very basic class, used just to add players or? I agree that card dont need to know in which game they are involved or they are in hand of which player.
dirs is offline   Reply With Quote
Old 05-06-2004, 11:52 AM   #15 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
Do you mean to say that player class would be a very basic class, used just to add players.
Yes!
I have something prepared for you. I am now busy bit I'll post it in a moment.
I will also post an imgage so you can oversee the design.
__________________
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 04:04 PM.


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