A neato blackjack game (C++)
//Intergalactic Blackjack
//By Marc Friedenberg
//Started on Jaunary 28, 2002, at 10:07 A.M.
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int drawCard(); //Draw a random card (for player or dealer)
char hitStay(); //Returns if they want to hit or stay
void dealerRoutine(); //Automate the card selection of the dealer
void determineWinner(); //From player and dealer totals, determine winner
int finish(); //End the game (ask to play again)
void statusFind(); //If the player is under 21, ask them to hit or stay
void dealerFirst(); //Determine dealer's first card
void playerFirst(); //Determine player's first two cards
void playAgainDecide(); // Would they like to play again?
int card; //The card that was just drawn
int ptot; //The player's total
int dtot; //The dealer's total
char move; // 'h' if hit, 's' if stay
char playAgain; //Do they want to play again?
int main()
{
ptot=0;
dtot=0;
srand(time(NULL));
cout << "\n\nWelcome to Intergalactic Blackjack!\n\n";
playerFirst();
dealerFirst();
statusFind();
dealerRoutine();
return 0;
}
int drawCard()
{
card=1+rand()%11;
return card;
}
char hitStay()
{
cout << "Would you like to hit (h) or stay (s)?";
cin >> move;
if (move=='h'||move=='H'||move=='s'||move=='S')
{
return move;
}
else
{
cout << "Please enter h or s" << endl;
hitStay();
}
}
void determineWinner() //Compare card values to see who wins
{
if (ptot>21)
{
cout << "Sorry, you busted. The dealer wins." << endl;
}
else if (dtot>21)
{
cout << "The dealer busts with " << dtot << ", so you win!" << endl;
}
else
{
if (ptot>dtot)
{
cout << "Your " << ptot << " beats the dealer's " << dtot << ". Congratulations!" << endl;
}
else if (ptot<dtot)
{
cout << "Your " << ptot << " loses to the dealer's " << dtot << ". Sorry!" << endl;
}
else
{
cout << "It's a tie! You and the dealer each have " << ptot << "." << endl;
}
}
}
int finish()
{
determineWinner();
playAgainDecide();
}
void dealerRoutine()
{
if (ptot>21)
finish();
while (dtot<=21)
{
dtot+=drawCard();
cout << "\tDealer draws " << card << ", total is " << dtot << "..." << endl;
if (dtot>=17)
finish();
}
}
void statusFind()
{
while (ptot<21)
{
move=hitStay();
if (move=='s') //If they want to stay...
{
dealerRoutine();
}
else if (move=='h') //If they want to hit...
{
ptot+=drawCard();
cout << "\tYou drew a " << card << ". Your total is " << ptot << "." << endl;
}
}
}
void dealerFirst()
{
//Determine the dealer's first card
card=drawCard();
cout << "Dealer shows " << card << endl;
dtot+=card;
}
void playerFirst()
{
//Determine the player's two cards
card=drawCard();
cout << "You drew a " << card << " and a ";;
ptot+=card;
card=drawCard();
ptot+=card;
cout << card << ", so the total is " << ptot << endl;
}
void playAgainDecide()
{
cout << "Would you like to play again? (y/n)";
cin >> playAgain;
if (playAgain=='Y'||playAgain=='y')
{
main();
}
else if (playAgain=='N'||playAgain=='n')
{
cout << "Thanks for playing!" << endl;
exit(0);
}
else
{
cout << "Please enter y or n." << endl;
playAgainDecide();
}
}