View Single Post
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