As I stated in the title i am having the issue where the object 'bj' does not call any of the functions that are within the class. I am stuck on this issue. Any help will be a appreciated. Thanks in advance.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
//Global Variables
int Player_Ace=0, Dealer_Ace=0; //counter for how many aces have been dealt. Initialized to 0
int Deck=0; //index variable needed for card dealing
int cards[53]; //array filled with card values
char CardNames2[53][25]; //Used to display name of card
char CardNames[53][25] = { //array filled with card CardNames.Used for dealing
"Zero",
"ace of Spades(1/11)", "Two of Spades(2)", "Three of Spades(3)", "Four of Spades(4)",
"Five of Spades(5)", "Six of Spades(6)", "Seven of Spades(7)", "Eight of Spades(8)",
"Nine of Spades(9)", "Ten of Spades(10)", "Jack of Spades(10)", "Queen of Spades(10)",
"King of Spades(10)",
"ace of Hearts(1/11)", "Two of Hearts(2)", "Three of Hearts(3)", "Four of Hearts(4)",
"Five of Hearts(5)", "Six of Hearts(6)", "Seven of Hearts(7)", "Eight of Hearts(8)",
"Nine of Hearts(9)", "Ten of Hearts(10)", "Jack of Hearts(10)", "Queen of Hearts(10)",
"King of Hearts(10)",
"ace of Clubs(1/11)", "Two of Clubs(2)", "Three of Clubs(3)", "Four of Clubs(4)",
"Five of Clubs(5)", "Six of Clubs(6)", "Seven of Clubs(7)", "Eight of Clubs(8)",
"Nine of Clubs(9)", "Ten of Clubs(10)", "Jack of Clubs(10)", "Queen of Clubs(10)",
"King of Clubs(10)",
"ace of Diamonds(1/11)", "Two of Diamonds(2)", "Three of Diamonds(3)", "Four of Diamonds(4)",
"Five of Diamonds(5)", "Six of Diamonds(6)", "Seven of Diamonds(7)", "Eight of Diamonds(8)",
"Nine of Diamonds(9)", "Ten of Diamonds(10)", "Jack of Diamonds(10)", "Queen of Diamonds(10)",
"King of Diamonds(10)"
};
class BlackJack{
public:
static void choice();
static void rules();
static void shuffle();
static void shuffle2();
static void init();
static void display(int *, int*);
static void input(char *);
static void process(int *, int *, int*);
static void exit(int *, int *, int *, int *);
};
void BlackJack::choice()
{
int option; //integer variable for option menu
shuffle2();
cout<<"Welcome To Blackjack"<<endl;
cout<<"Press 1 To see the rules of BlackJack"<<endl;
cout<<"Press 2 To play BlackJack"<<endl;
cin>>option;
while((option!=1) && (option!=2))
{
cout<<endl;
cout<<"Sorry that is not an option. Please try again."<<endl;
cout<<endl;
cout<<"Press 1 To see the rules of BlackJack"<<endl;
cout<<"Press 2 To play BlackJack"<<endl;
cin>>option;
}
if (option==1)
{
rules();
}
else if(option==2)
{
shuffle();
init();
}
}
void BlackJack::rules()
{
int option2;
cout << "Welcome to rules section\n";
cout << "aces represent either 1 or 11\n";
cout << "Cards from 2-10 represent their respective values.\n";
cout << "Queens, Kings, and Jacks each representing 10\n";
cout<<"Press 1 to play the game "<<endl;
cin>>option2;
while(option2!=1)
{
cout<<endl;
cout<<"Invalid entry. Please try again."<<endl;
cout<<endl;
cout << "Welcome to rules section\n";
cout << "aces represent either 1 or 11\n";
cout << "Cards from 2-10 represent their respective values.\n";
cout << "Queens, Kings, and Jacks each representing 10\n";
cout<<"Press 1 to play the game "<<endl;
cin>>option2;
}
shuffle();
init();
}
void BlackJack::shuffle()
{
int i, j, temp;
char temp2[25];
srand((unsigned) time(NULL)); //Seeds rand()
i = 52;
while(i > 0) //algorithm that puts the card values and CardNames in random order
{
j = rand() % i +1; //set k to a random # between 1 and the current value of j
temp = cards[i];
strcpy_s(temp2,CardNames[i]);
cards[i] = cards[j];
strcpy_s(CardNames[i],CardNames[j]);
cards[j] = temp;
strcpy_s(CardNames[j],temp2);
i--;
}
}
void BlackJack::shuffle2()
{
int count, i;
//Spades
for(count=1; count<=10; count++) //Fills an array with values of the cards ace-10. Here ace is 1.
{
cards[count]=count;
}
cards[11]=10; //Values for Jack Queen King
cards[12]=10;
cards[13]=10;
i=1;
//Hearts
for(count=14; count<=23; count++) //Fills an array with values of the cards ace-10. Here ace is 1.
{
cards[count]=i;
i++;
}
cards[24]=10; //Values for Jack Queen King
cards[25]=10;
cards[26]=10;
i=1;
//Clubs
for(count=27; count<=36; count++) //Fills an array with values of the cards ace-10. Here ace is 1.
{
cards[count]=i;
i++;
}
cards[37]=10; //Values for Jack Queen King
cards[38]=10;
cards[39]=10;
i=1;
//Diamonds
for(count=40; count<=49; count++) //Fills an array with values of the cards ace-10. Here ace is 1.
{
cards[count]=i;
i++;
}
cards[50]=10; //Values for Jack Queen King
cards[51]=10;
cards[52]=10;
}
void BlackJack::init()
{
static int TestNum=0; //used in checking for natural blackjack
int PlayerCard1, PlayerCard2; //variables for cards dealt to player
int DealerCard1, DealerCard2; //variables for cards dealt to dealer
int PlayerTotal, DealerTotal; //total value of dealer's and player's hands
int *Player_TestNum = &TestNum;
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was re-shuffled. "<<endl;
shuffle();
Deck=0;
}
Deck++;
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was re-shuffled. "<<endl;
shuffle();
Deck=1;
}
PlayerCard1=cards[Deck];
strcpy_s(CardNames2[1],CardNames[Deck]);
Deck++;
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was re-shuffled. "<<endl;
shuffle();
Deck=1;
}
PlayerCard2=cards[Deck];
strcpy_s(CardNames2[2],CardNames[Deck]);
Deck++;
PlayerTotal=PlayerCard1 + PlayerCard2; //Get the total of the players hand
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was re-shuffled."<<endl;
shuffle();
Deck=1;
}
DealerCard1=cards[Deck];
strcpy_s(CardNames2[3],CardNames[Deck]);
Deck++;
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was re-shuffled."<<endl;
shuffle();
Deck=1;
}
DealerCard2=cards[Deck];
strcpy_s(CardNames2[4],CardNames[Deck]);
DealerTotal=DealerCard1 + DealerCard2; //Get the total of the dealers hand
if(PlayerCard1==1) //If first card dealt is an ace, make it an 11
{
Player_Ace=1; //add 1 to Player_Ace. Lets you know later on if any of the
PlayerCard1=PlayerCard1+10; //original two cards where an ace
PlayerTotal=PlayerCard1 + PlayerCard2;
}
if(PlayerCard2==1) //If second card dealt is an ace, decide wheter to make it
{ //an 11 or 1
if(PlayerCard1 != 11) //Make the second card an 11 only if the first card dealt is
{ //not already an 11
Player_Ace=1;
PlayerCard2=PlayerCard2+10;
PlayerTotal=PlayerCard1 + PlayerCard2;
}
else //Otherwise make it a 1
{
PlayerCard2=1;
PlayerTotal=PlayerCard1 + PlayerCard2;
}
}
if(DealerCard1==1) //Now do it again for the dealers cards
{
Dealer_Ace=1;
DealerCard1=DealerCard1+10;
DealerTotal=DealerCard1+DealerCard2;
}
if(DealerCard2==1)
{
if(DealerCard1 != 11)
{
Dealer_Ace=1;
DealerCard2=DealerCard2+10;
DealerTotal=DealerCard1+DealerCard2;
}
else
{
DealerCard2=1;
DealerTotal=DealerCard1+DealerCard2;
}
}
display(&PlayerTotal, &DealerTotal) ;
process(Player_TestNum, &PlayerTotal, &DealerTotal);
}
void BlackJack::display(int *Player_PlayerTotal, int *Dealer_DealerTotal)
{
cout<<"You get a "<<CardNames2[1]<<" and a "<<CardNames2[2]<<" for a total of "<<*Player_PlayerTotal<<endl;
cout<<"Dealer gets a "<<CardNames2[3]<<" and a "<<CardNames2[4]<<" for a total of "<<*Dealer_DealerTotal<<endl;
}
void BlackJack::input(char *choice)
{
cout<<"Would you like to Hit or Pass? (Press H or P)"<<endl;
cin>>*choice;
}
void BlackJack::process(int *TestNum, int *PlayerTotal, int *DealerTotal)
{
static char choice2;
static int lost=0, won=0, tied=0; //won/lost/tie record
int *p_lost=&lost;
int *p_won=&won;
int *p_tied=&tied;
char *p_choice=&choice2;
if(*TestNum==0) //Checks to see if anyone has a natural blackjack. Only do this if TestNum=0
{
if((*PlayerTotal==21) && (*DealerTotal!=21)) //If player has a natural 21 and the dealer
{ //doesnt, he automatically wins
//display(&PlayerTotal, &DealerTotal) ;
cout<<"You have BlackJack! You win!"<<endl;
won=won+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if((*DealerTotal==21) && (*PlayerTotal!=21)) //If dealer has a natural 21 and the player
{ //doesnt, dealer automatically wins
//display(&PlayerTotal, &DealerTotal) ;
cout<<"Dealer has BlackJack! You Lose."<<endl;
lost=lost+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if((*DealerTotal==21) && (*PlayerTotal==21)) //If both the player and dealer have a natural 21,
{ //its a tie
//display(&PlayerTotal, &DealerTotal) ;
cout<<"You both have BlackJack! It's a tie!"<<endl;
tied=tied+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else //Otherwise show player the current hands and goto input() function to see if
{ //he wants another card
*TestNum=1; //Make TestNum a 1 so it doesnt check for natural blackjack again
//display(&PlayerTotal, &DealerTotal);
input(p_choice);
}
}
if(((choice2 == 'H') || (choice2 == 'h')) && (*TestNum==1)) //check to see if player entered H or h
{
Deck++;
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled."<<endl;
shuffle();
Deck=1;
}
if(cards[Deck]==1) //If card dealt is an ace, decide wheter it should be a 1
{ //or 11 for the player.
if(*PlayerTotal<=10) //If his current hand is under 11 make the ace an 11 and
{ //add one to Player_Ace
Player_Ace=1;
*PlayerTotal=*PlayerTotal+11;
}
else //Otherwise make it a 1
{
*PlayerTotal=*PlayerTotal + 1;
}
}
else //card dealt is not an ace, just add it to his total
*PlayerTotal= *PlayerTotal + cards[Deck];
if((*PlayerTotal>=22) && (Player_Ace==1)) //If player is over 21 but has an ace that is an
{ //11, make that ace into a one. Subtract 1 from
*PlayerTotal= *PlayerTotal-10; //Player_Ace to make sure it only does this once.
Player_Ace=0;
}
if(*PlayerTotal>=22) //If player is over 21 he loses and dealer wins
{
cout<<"You are dealt a "<<CardNames[Deck]<<" for a total of "<<*PlayerTotal<<endl;
cout<<"You are over. Dealer wins."<<endl;
lost=lost+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if(*PlayerTotal==21) //If player has 21 he automaticaly wins.
{
cout<<"You are dealt a "<<CardNames[Deck]<<" for a total of "<<*PlayerTotal<<endl;
cout<<"You have BlackJack! You win!"<<endl;
won=won+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if(*PlayerTotal<=20) //If player has 20 or less, let him decide if he
{ //wants to hit again
cout<<"You are dealt a "<<CardNames[Deck]<<" for a total of "<<*PlayerTotal<<endl;
input(p_choice);
process(TestNum, PlayerTotal, DealerTotal);
}
}
else if(((choice2 =='P') || (choice2 == 'p')) && (*TestNum==1)) //checks to see if player entered P or p
{
while(*DealerTotal<=16) //Keep getting cards as long as dealer has a 16 or less
{
Deck++;
if(Deck==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled."<<endl;
shuffle();
Deck=1;
}
if(cards[Deck]==1) //If card dealt is an ace, decide wheter it should be a
{ //1 or 11 for the dealer.
if(*DealerTotal<=10) //If his current hand is under 11 make the ace an 11 and
{ //add one to Dealer_Ace
Dealer_Ace=1;
*DealerTotal=*DealerTotal+11;
}
else //Otherwise make it a 1
{
*DealerTotal= *DealerTotal + 1;
}
}
else //card dealt is not an ace, just add it to his total
*DealerTotal= *DealerTotal + cards[Deck];
if((*DealerTotal>=22) && (Dealer_Ace==1)) //If dealer is over 21 but has an ace that is an
{ //11, make that ace into a one. Subtract 1 from
*DealerTotal= *DealerTotal-10; //Dealer_Ace to make sure it only does this once.
Dealer_Ace=0;
}
cout<<"Dealer is dealt a "<<CardNames[Deck]<<" for a total of "<<*DealerTotal<<endl;
} //end of while statement
if(*DealerTotal>=22) //Checks to see if dealer has gone over. If he has, player wins.
{
cout<<"Dealer has a total of "<<*DealerTotal<<endl;
cout<<"Dealer is over. You win!"<<endl;
won=won+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if(*DealerTotal==21) //Checks to see if dealer has blackjack. If so, player
{ //automaticaly loses
cout<<"Dealer has BlackJack. You lose."<<endl;
lost=lost+1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if((*DealerTotal>*PlayerTotal) && (*DealerTotal<=20)) //Dealer wins if his hand is higher than
{ //players
cout<<"Dealer stands with a "<<*DealerTotal<<endl;
cout<<"Dealer wins"<<endl;
lost=lost + 1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if(*DealerTotal==*PlayerTotal) //If dealer and player have same hand, it's a tie
{
cout<<"Dealer stands with a "<<*DealerTotal<<endl;
cout<<"You tied"<<endl;
tied=tied + 1;
exit(TestNum, p_won, p_lost, p_tied);
}
else if(*DealerTotal<*PlayerTotal) //If player has a higher hand, he wins
{
cout<<"Dealer stands with a "<<*DealerTotal<<endl;
cout<<"You win!"<<endl;
won=won + 1;
exit(TestNum, p_won, p_lost, p_tied);
}
}
//if they enter something other than a H, h, P, or p let them try again
else if((choice2 !='P') && (choice2 != 'p') && (*TestNum==1) && (choice2 !='H') && (choice2 != 'h') && (choice2 !='\0'))
{
cout<<"Sorry that is not an option. Please try again."<<endl;
input(p_choice);
process(TestNum, PlayerTotal, DealerTotal);
}
}
void BlackJack::exit(int *TestNum, int *won, int *lost, int *tied)
{
char choice4;
cout<<endl;
cout<<"Would you like to play again? (Press Y or N)"<<endl;
cin>>choice4;
while((choice4!='Y') && (choice4!='y') && (choice4!='N') && (choice4!='n'))
{
cout<<endl;
cout<<"Sorry that is not an option. Please try again."<<endl;
cin>>choice4;
}
if((choice4 == 'Y') || (choice4 == 'y')) //If yes, start over again
{
cout<<endl;
*TestNum=0; //reset TestNum to 0 so it checks for natural blackjack
Player_Ace=0; //Reinitialize these as 0. Otherwise, if a person was dealt an ace and it
Dealer_Ace=0; //didnt get turned into a one, the program will subtract 10 form their total
init();
}
else if((choice4 == 'N') || (choice4 == 'n')) //If player quits show him his w/l/t record
{
cout<<endl;
cout<<"Thank you for playing."<<endl;
cout<<"Your record was "<<*won<<" wins "<<*lost<<" losses and "<<*tied<<" ties."<<endl;
cout<<endl;
}
}
void main()
{
BlackJack bj;
bj.choice();
bj.rules();
bj.shuffle();
bj.shuffle2();
bj.init();
bj.display(int *Player_PlayerTotal, int *Dealer_DealerTotal);
bj.input(char *choice);
bj.process(int *TestNum, int *PlayerTotal, int *DealerTotal);
bj.exit(int *TestNum, int *won, int *lost, int *tied);
}