I dont know why, but this program I wrote is having a problem. it has a problem converting an Ace from 11 to 1 when the players have more than 21. if you could look over this, I would appreciate it.

#include <iostream>
#include <ctime>

using namespace std;

double Card = 0;
char cardA;
int YN = 1;
bool Ace = false;
int Play = 1;
double pHand = 0;
double dHand = 0;
double Chips = 1000;
double bet = 0;
int Win = 0;

void cardT()
{
int KQJ = 0;   
  if (Card == 11)
  {
   cout << "Dealer draws a Jack\n";
   Card = 10;
   KQJ = 1;
  }
  if (Card==12)
  {
   cout << "Dealer draws a Queen\n";
   Card = 10;
   KQJ = 1;
  }
  if (Card==13)
  {
   cout << "Dealer draws a King\n";
   Card = 10;
   KQJ = 1;
  }
  if (Card == 1)
  {
   cout << "Dealer draws an Ace\n";
   Card = 11;
   Ace = true;
   KQJ = 1;
  }
  if (KQJ != 1)
  {
   cout << "Dealer draws a "<< Card << endl;
  }  
}

void dDraw()
{
  Card = ( rand() %13) + 1; //Dealer Draws
  cardT();

  dHand = dHand+Card;
  if ((dHand > 21) && (Ace = true))
  {
   dHand = dHand - 10;
   Ace= false;
  } 
  cout << "dealer has " << dHand<< endl << endl;
}

void pDraw()
{
  Card = ( rand() %13) + 1; //Player Draws
  cardT();
  pHand = pHand+Card;
  if ((dHand >21) && (Ace = true))
  {
   pHand = pHand - 10;
   Ace=false;
  } 
  cout << "player has " << pHand<< endl << endl;
}

int main()
{
 cout << "Welcome to Blackjack. Would you like to play?\n";
 cout << " 1.Yes\n 2.No\n";
 cin >> Play;
 while (Play != 2)
 {
 cout << "You have $" << Chips << " in chips.\n";       
 cout << "Place your bet\n";
 cin >> bet;
 while (Chips < bet)
 {
  cout << "Please enter fewer chips than you have.\n";
  cout << "Place your bet\n";
  cin >> bet;
 }
 
  srand( time( NULL ) ); 
  
  
  pDraw();
  dDraw();
  pDraw();
  dDraw();
  
  
  while (YN == 1)
  {
  if (pHand < 21)
  {
  cout << "Would you like another card?\n 1.Hit me \n 2.Stay \n";
  cin >> YN;
  cout << endl;
  }
    if (pHand == 21)
   {
    cout << "Winner! Winner! Chicken dinner!\n\n";
    YN=2;
    Win=1;
   }
   if (pHand >= 22)
   {
    cout << "Player busts. Dealer wins\n\n";
    pHand=0;
    YN=2;
    Win=2;
   }
  if (YN == 1)
  {
   cout << "Very well\n";
   pDraw();   
  }
  
  if ((YN != 1) && (pHand != 0) && (pHand != 21))
  {
while (dHand <= 17)
  {
   dDraw();
  }
   if (dHand == 21)
   {
    cout << "Dealer has BlackJack Dealer wins.\n\n" ;
    Win=2;
   }
  if ((dHand >= 22) && (dHand != 21))
  {
   cout << "Dealer busts. Player wins\n\n";
   dHand=0;
   Win=1;
  }

  if ((dHand != 0) && (pHand !=0) && (dHand != 21) && (pHand != 21))
  {
  if (pHand<dHand)
   {
    cout << "Dealer wins.\n\n";
    Win=2;
   }
   if (pHand>dHand)
   {
    cout << "Player wins.\n\n";
    Win=1;
   }
   if (pHand==dHand)
   {
    cout << "Game ends in tie.\n\n";
   }
 
}
}  
}  
  if (Win == 1)
  {
   Chips = Chips + bet;
  }
  if (Win == 2)
  {
   Chips = Chips - bet;
  }
  cout << "You have $" << Chips << " in chips.\n" ;
   Ace=0;
   Win = 0;
   dHand = 0;
   pHand = 0;
   YN = 1;
   cout << "Would you like to play again?\n 1.Yes\n 2.No\n";
   cin >> Play;
   if ((Chips <= 0) && (Play == 1))
   {
    Play = 2;
    cout << "Sorry, but you are out of chips.\n";
   }
}
  cout << "Thank you for playing!\n";
  system ("pause");
  return 0;                 
}

lenghty, isn't it?:icon_eek:

Recommended Answers

All 5 Replies

I dont know why, but this program I wrote is having a problem. it has a problem converting an Ace from 11 to 1 when the players have more than 21. if you could look over this, I would appreciate it.

lenghty, isn't it?:icon_eek:

Yes, very lengthy. It would help if you pointed out what line numbers to focus on where you attempt to convert the Ace. You can highlight them in red or you can use C++ code tags, which add numbers, and point out the number.

[code=cplusplus] // paste code here

[/code]

sorry, here we go:

void cardT()
{
int KQJ = 0;   
  if (Card == 11)
  {
   cout << "Dealer draws a Jack\n";
   Card = 10;
   KQJ = 1;
  }
  if (Card==12)
  {
   cout << "Dealer draws a Queen\n";
   Card = 10;
   KQJ = 1;
  }
  if (Card==13)
  {
   cout << "Dealer draws a King\n";
   Card = 10;
   KQJ = 1;
  }
 if (Card == 1)
  {
   cout << "Dealer draws an Ace\n";
   Card = 11;
   Ace = true;
   KQJ = 1;
  }
  if (KQJ != 1)
  {
   cout << "Dealer draws a "<< Card << endl;
  }  
}

void dDraw()
{
  Card = ( rand() %13) + 1; //Dealer Draws
  cardT();

  dHand = dHand+Card;

if ((dHand > 21) && (Ace = true))
  {
   dHand = dHand - 10;
   Ace= false;
  } 
  cout << "dealer has " << dHand<< endl << endl;
}

void pDraw()
{
  Card = ( rand() %13) + 1; //Player Draws
  cardT();
  pHand = pHand+Card;
 if ((dHand >21) && (Ace = true))
  {
   pHand = pHand - 10;
   Ace=false;
  } 

  cout << "player has " << pHand<< endl << endl;
}

the lines where the Ace is drawn are 22-28 and the part where they are added to the hands are 42-46 and 55-59

Looks like you are using the assignment operator (=) rather than the comparison operator (==) in lines 42 and 55. Try changing Ace=true to Ace==true or simple Ace in those lines.

You have missed that you could have 2 aces e.g.
AAK45 would be 21 but you will not score it as such.

So ADD 10 if you have an ace. Since you can't have two. as AA+ 20 == 22.
e.g. lines 42-46 become

if ((dHand < 12) && (Ace == true))
{
 dHand += 10;
}

Notice you have also made the beginners error of writing ace=true which is always true and sets ace to true. But any modern compiler gives warnings about that.

of corse i would do something so small it was barely noticeable, it usually only takes a fresh pair of eyes or two. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.