#include <iostream>
#include <Windows.h>
#include <time.h>
#include <iomanip>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
/*********Wallet*****************/
void blingstack(double& wallet, double& winnings, double& payoff)
{
if(payoff > 0)
cout<< "You won $ " << winnings * payoff <<endl;
wallet = wallet + (winnings * payoff);
cout<<"\nYour current balance is now: $ " <<wallet<< endl;
payoff = 0; winnings = 0;
}
/**********Suits*****************/
void Suits (string& suitarrayf)
{
srand(time(NULL));
int const prosuit = 4;
int suitnum = rand()%4;
string suitarray [prosuit] = {"Spades", "Clubs", "Diamonds", "Hearts"};
suitarrayf = suitarray[suitnum];
}
/***********ACES*****************/
void Aces(int& acevalue)
{
cout<<"Would you like to play your ace as 1 or 11? ";
cin>>acevalue;
}
/***********Cards***************/
void Cards (int& cardvaluef, string& royalsf, int& royalcheckf)
{
srand(time(NULL));
int draw = rand()% 12 + 1;
if (draw <11 && draw > 1)
{
royalcheckf = 0;
cardvaluef = draw;
}
else if (draw == 1)
{
Aces(cardvaluef);
}
else
{
cardvaluef = 10;
royalcheckf = 10;
const int royalpro = 3;
string royalsf2[royalpro] = {"Jack", "Queen", "King"};
if(draw == 11)
{
royalsf = royalsf2[0];
}
else if(draw == 12)
{
royalsf = royalsf2[1];
}
else if(draw == 13)
{
royalsf = royalsf2[2];
}
else
{
cout<<"royal fuction failed, you broke my program \n";
}
}
}
/**************Blackjack game mechanic*******************/
void bjcompare(int playerh, int cpuh, int& resultf, string& pushf)
{
resultf = 0;
if (playerh == 21 && cpuh !=21)
resultf = 3;
else if (playerh > 21) // hand more than 21 BUST LOSE
resultf = 1;
else if (playerh > cpuh) // If not bust, if greater than cpu WIN
resultf = 2;
else if (cpuh > 21) //If not bust, but lower than cpu, but cpu bust WIN
resultf = 2;
else if (cpuh < 22) //if not bust, but lower than cpu, but cpu less than 22, LOSE
resultf = 1;
else
pushf = "You and the Dealer Push!";
}
/**********************************************/
int main()
{
int choice; //Game Choice Loop Variable.
choice = 0;
double entrywallet = 800;
double bets = 0;
double payoutmultiplier = 0;
int correctbling = 0;
do{
if(entrywallet == 0)
{
cout<<"You have no more money, time to leave the Casino! GOODBYE!\n";
break;
}
cout<<"\nPlease select which game you would like to play: ";
cin>>choice; //game choose
switch(choice)
{
case 1:
{
cout<<"\nWelcome to BlackJack\n";
blingstack(entrywallet, bets, payoutmultiplier);
cout<<"\nLets Get Started!\n\n";
do{
cout<<"How much would you like to bet? ";
cin>> bets;
if(entrywallet == 0)
{
cout<<"You have no more money, time to leave the Casino! GOODBYE!\n";
break;
}
else if(bets > entrywallet)
{
cout<<"You don't have that much money!\n\n";
}
else if(bets < 0)
{
cout<<"You can't give money away!\n\n";
}
else if(bets == entrywallet)
{
cout<<"Your ALL IN! GOOD LUCK!\n\n";
correctbling = 1;
}
else
correctbling = 1;
}while(correctbling == 0);
cout<<"\nAlright! Here comes the deal!\n\n";
Sleep(2000);
/*************Front Varibles for Function Calls*************/
int phand, chand, draw;
const int pcardvalue = 2;
int pcard[pcardvalue];
int cardvalue = 0; int royalcheck = 0;
string royals;
string suitsdisp;
/*******************Player Card Draw Sequence*********/
for(int pcardvalue=0; pcardvalue<2; pcardvalue++) //loop to draw and store player draws in an array.
{
Cards(cardvalue, royals, royalcheck); //run card draw function
Suits(suitsdisp); //run random suit function
pcard[pcardvalue] = cardvalue;
if(cardvalue == 1 || cardvalue == 11)
{
cout<< "Ace of "<< suitsdisp << endl; //display value of card according assigned array, then display suit.
}
else if(royalcheck != 10)
{
cout<< pcard[pcardvalue] << " of " << suitsdisp << endl;
}
else if(royalcheck == 10)
{
cout<<royals<< " of " << suitsdisp << endl;
}
Sleep (2000); //Allow time for rand seed to catch up
}
phand = pcard[0] + pcard[1];
cout<<"\nYour total is " << phand << "\n\n";
/*****************************Hit or Stay*********************/
string hs;
int error = 0;
do{
cout<<"Would you like to Hit or Stay? ";
cin>> hs;
transform (hs.begin(), hs.end(), hs.begin(), tolower); //input case insensitivity
if(hs == "hit")
{
Cards(cardvalue, royals, royalcheck); //run card draw function
Suits(suitsdisp); //run random suit function
draw = cardvalue;
if(cardvalue == 1 || cardvalue == 11)
{
cout<< "Ace of "<< suitsdisp << endl; //display value of card according assigned array, then display suit.
}
else if(royalcheck != 10)
{
cout<< draw << " of " << suitsdisp << endl;
}
else if(royalcheck == 10)
{
cout<<royals<< " of " << suitsdisp << endl;
}
}
else if(hs == "stay")
break;
else
{
cout<<"You've entered something invalid, try again\n";
error = 1;
}
phand = phand + draw;
cout<<"\nYour total is " << phand << "\n\n";
if(phand > 21)
{
cout<<"You busted! Good Luck Next Time!\n";
break;
}
else{}
}while(hs != "stay" || error == 1);
/*********************CPU Card Draw Sequence*************/
chand = rand()% 8 + 16;
cout<<"\nComputer's total is " << chand << "\n\n";
/*********************END OF INITIAL CARD DRAW*********START HIT/STAY SEQUENCE**************/
int result = 0;
string push;
bjcompare(phand, chand, result, push);
if (result == 3)
{
cout<<"You got BlackJack! YOU WIN EXTRA!!! \n";
payoutmultiplier = 2.5;
blingstack(entrywallet, bets, payoutmultiplier);
}
else if(result == 2)
{
cout<<"You win! Congratulations!\n";
payoutmultiplier = 2;
blingstack(entrywallet, bets, payoutmultiplier);
}
else if(result == 1)
{
cout<<"You Lose! Good Luck Next Time!\n";
payoutmultiplier = -1;
blingstack(entrywallet, bets, payoutmultiplier);
}
else
cout<< push << endl;
/*********************END OF GAME***********************/
I'm having an issue with invalid inputs, such as when someone inputs a letter when asked for "How much do you want to bet" , it would go ahead an run the problem indefinitely and print out really high numbers or really negative numbers as my balance.
Along those lines similar problem occurs when asked to hit or stay, but it just doesn't run indefinitely, it just prints out those extreme numbers.
Pretty much I'm having issues controlling invalid inputs, it seems as though my if else statements are being bypassed?