Hello Everyone,

I have a Blackjack Assignment due in a few days in my Programming class. I have completed the program and it seems to be working. Could I ask someone to take a look at it and tell me how it looks. Where I could make improvements and so forth.

One major problem I had was using arrays to keep track of the cards. I used the Turbo C++ 4.5 compiler.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#include<iomanip.h>

//Declaring Variables

  char choice, option, decision, value;
  int card1, card2, card3, card4, card5, play_total, deal_total;
  int a;

//Declaring Functions

  void Intro();
  void GameMenu();
  void Selection();
  void Rules();
  int CreateDeck();
  void HumanPlayer();
  void Display();
  void Hit();
  void Ace();
  void Stay();
  void Dealer();
  void Display1();
  void Dealer_Hit();
  void Results();
//*************************************************************************

void Intro()
{//begin Intro Function

  while ((choice != '9'))
  {//begin WHILE

	 cout << "\n\t\t\t***Welcome To The Blackjack Casino***"<<endl;
	 cout << "\nDo You Wish To Play A Game of Blackjack?\n";
	 cout << "1. If Yes - Press <Y>\n";
	 cout << "2. If No - Press <N>\n";
	 cout << "3. If You Wish To Stop The Game - Press <9>\n";
	 cin >> choice;

    if ((choice == 'Y') || (choice == 'y'))
	 {//begin IF

	  GameMenu();

    }//end IF

	  else if ((choice == 'N') || (choice == 'n'))
	  {//begin ELSE IF

				 cout << "\nYou Have Chosen Not To Play !!!\n";

	  }//end  ELSE IF

			 else if (choice == '9')
			{//begin ELSE IF

					 cout << "\nYou Have Chosen To Stop The Game\n";
                cout << "\nThank You For Playing!!!\n";
					 cout << "\nGoodbye!!!\n";

			}//end ELSE IF

					else
					{//

					  cout << "\nYou Have Made An Invalid Entry\n";

					}//

}//end WHILE

}//end Intro Function

//*************************************************************************
void GameMenu()

{//begin GameMenu Function

  cout << "\nYou Have Chosen To Play A Game Of Blackjack!!!\n";
  cout << "1. To View The Rules of Blackjack - Press <R>\n";
  cout << "2. To Deal - Press <D>\n";

  Selection();

}//end GameMenu Function

//*************************************************************************

void Selection()
{//begin Selection Function

  cout << "\nPlease Select An Option\n";
  cin >> option;

  if ((option == 'D') || (option == 'd'))
  {//begin IF

	  cout << "\n";

	  CreateDeck();
	  HumanPlayer();

  }//end IF

  else if ((option == 'R') || (option == 'r'))
  {//begin ELSE IF

		   Rules();

  }//end ELSE IF

		  else
		  {

			  cout << "\nThat Is Not A Valid Entry\n";

		  }

}//end Selection Function

//*************************************************************************
void Rules()
{//begin Rules Function

  cout << "\nThese Are The Instructions For Blackjack:\n";
  cout << "1. Blackjack is a card game which involves The Player vs The Dealer.\n";
  cout << "2. The Player goes first and is dealt two cards face up.\n";
  cout << "3. The Player has the choice to Take a card or Stay.\n";
  cout << "4. The Player may keep taking cards until he/she chooses to Stay.\n";
  cout << "5. If the Player has exceeded 21 then The Player automatically loses.\n";
  cout << "6. If the Player has 21 or Less, it is now the Dealer's turn to take cards.\n";
  cout << "7. The Dealer must continue taking Cards until they Total at least 17 or more.\n";
  cout << "8. If the Dealer exceeds 21 then the Dealer automatically loses.\n";
  cout << "9. If the Dealer nor the Player exceeds 21, the one with the Highest Total wins.\n";
  cout << "10. If the Totals are Identical then this will result in a Draw.\n";
  cout << "11. In this Program, you are the Player while the Computer is the Dealer.\n";

}//end Rules Function
//*************************************************************************

int CreateDeck()
{//begin CreateDeck Function

  a = 1 + rand() % 14;

  if (a > 13)

  {//begin IF

      return 10;

  }//end IF

  else

  {//begin ELSE

    return a;

  }//end ELSE

}//end CreateDeck Function

//*************************************************************************

void HumanPlayer()
{//begin HumanPlayer Function

  cout << "\nYour Play!!!\n";
  cout << "\n";
  cout << "\nYour Cards Are As Follows:\n";

  Display();

  while ((decision != 'E') || (decision != 'e'))
  {//begin WHILE

  cout << "\n";
  cout << "\nWhat Do You Wish To Do?\n";
  cout << "\nStay - Press <S> || Hit - Press <H> || New Hand - Press <E>\n";
  cin >> decision;

  if ((decision == 'H') || (decision == 'h'))
  {//begin IF

      Hit();

  }//end IF

  else if ((decision == 'S') || (decision == 's'))
  {//begin ELSE IF

	       Stay();
           break;

  }//end ELSE IF

		  else if ((decision == 'E') || (decision == 'e'))
		  {//

						GameMenu();
						break;

		  }//

				 else
				 {//begin ELSE

					cout << "\nThat Is Not A Valid Entry\n";

				 }//end ELSE

  }//end WHILE

}//end HumanPlayer Function

//*************************************************************************

void Display()
{//begin Display Function

  card1 = CreateDeck();
  card2 = CreateDeck();

  if ((card1 >= 2) && (card1 <=10))
  {//begin IF

	  cout << "\nYour First Card is " <<card1;

  }//end IF

		else if (card1 == 1)
      {//begin ELSE

				  cout << "\nYour First Card is ACE";
			   Ace();

		}//end ELSE

	       else if (card1 == 11)
		   {//begin ELSE

					card1 = 10;
					  cout << "\nYour First Card is JACK";

	       }//end ELSE

				else if (card1 == 12)
		        {//begin ELSE

					     card1 = 10;
							 cout << "\nYour First Card is QUEEN";

	            }//end ELSE

						 else if (card1 == 13)
		             {//begin ELSE

						      card1 = 10;
									cout << "\nYour First Card is KING";

						  }//end ELSE

  cout << "\n";

  if ((card2 >= 2) && (card2 <= 10))
  {//begin IF

		cout << "\nYour Second Card is " <<card2;

  }//end ELSE

  else if (card2 == 1)

  {//begin ELSE IF

			 cout << "\nYour Second Card is ACE";
		   Ace();

  }//end ELSE IF

		else if (card2 == 11)
	    {//begin ELSE IF

				  card2 = 10;
					cout << "\nYour Second Card is JACK";

	    }//end ELSE IF

		     else if (card2 == 12)
			 {//begin ELSE IF

				      card2 = 10;
						cout << "\nYour Second Card is QUEEN";

		     }//end ELSE IF

			      else if (card2 == 13)
				  {//begin ELSE IF

					       card2 =10;
							 cout << "\nYour Second Card is KING";

			       }//end ELSE IF
 cout << "\n";

	     play_total = card1 + card2;

 cout << "\nYour Current Score is "<<play_total;

}//end Display Function

//*************************************************************************

void Hit()
{//begin Hit Function

  card3 = CreateDeck();

  cout << "\n";

  if ((card3 >= 2) && (card3 <= 10))
  {//begin IF

		 cout << "\nYour Next Card is " <<card3;

  }//end ELSE

  else if (card3 == 1)
  {//begin ELSE IF

			 cout << "\nThis Card is ACE";
		   Ace();

  }//end ELSE IF

		else if (card3 == 11)
	    {//begin ELSE IF

			     card3 = 10;
					cout << "\nThis Card is JACK";

		 }//end ELSE IF

		     else if (card3 == 12)
			 {//begin ELSE IF

				      card3 = 10;
						cout << "\nThis Card is QUEEN";

		     }//end ELSE IF

			      else if (card3 == 13)
				  {//begin ELSE IF

					       card3 = 10;
							 cout << "\nThis Card is KING";

			       }//end ELSE IF

  cout << "\n";

			play_total = play_total + card3;

  cout << "\nYour Current Total is "<<play_total;

}//end Hit Function

//*************************************************************************

void Ace()
{//begin Ace Function

  cout << "\nHow Much Do You Want This ACE To Be Worth?\n";
  cout << "\n1 point Press <A> || 11 points Press <B>\n";
  cin >> value;

  if ((value == 'A') || (value == 'a'))
  {//begin IF

	  card1 = 1;
	  card2 = 1;
	  card3 = 1;

	  cout << "\nThis Card Is Worth 1 Point\n";

  }//end IF

  else if ((value == 'B') || (value == 'b'))
  {//begin ELSE IF

			card1 = 11;
			card2 = 11;
		   card3 = 11;

			cout << "\nThis Card Is Worth 11 Points\n";

  }//end ELSE IF

}//end Ace Function
//*************************************************************************
void Stay()
{//

  if (play_total <= 21)
  {//begin IF

	  Dealer();

  }//end IF

  else 
  {//begin ELSE

	cout << "\nYour Total Score is "<<play_total;
	cout << "\n";
	cout << "\nYou Have Exceeded 21!!!\n";
	cout << "You Lose!!!\n";
	cout << "Dealer Wins!!!\n";

  }//end ELSE

}//
//*************************************************************************
void Dealer()
{//begin Dealer Function

  cout << "\nDealer's Turn!!!\n";
  cout << "\n";
  cout << "The Dealer's Cards Are As Follows:\n";
  cout << "\n";

          Display1();

}//
//*************************************************************************
void Display1()
{//

  card4 = CreateDeck();

  if ((card4 >= 2) && (card4 <=10))
  {//begin IF

	  cout << "\nThe Dealer's First Card is " <<card4;

  }//end IF

		else if (card4 == 1)
      {//begin ELSE

				  cout << "\nThe Dealer's First Card is ACE";

      }//end ELSE

	       else if (card4 == 11)
		   {//begin ELSE

					card4 = 10;
					  cout << "\nThe Dealer's First Card is JACK";

	       }//end ELSE

				else if (card4 == 12)
		        {//begin ELSE

						  card4 = 10;
							 cout << "\nThe Dealer's First Card is QUEEN";

	            }//end ELSE

		             else if (card4 == 13)
						 {//begin ELSE

						      card4 = 10;
									cout << "\nThe Dealer's First Card is KING";

	                 }//end ELSE

  cout << "\n";

          deal_total = card4;

  cout << "\nThe Dealer's Current Score is "<<deal_total;
  cout << "\n";

  while (deal_total < 17)
  {

		  cout << "\n";
	     Dealer_Hit();

  }
  
         Results();

}//
//*************************************************************************
void Dealer_Hit()
{//

  card5 = CreateDeck();

if ((card5 >= 2) && (card5 <=10))
  {//begin IF

	  cout << "\nThe Dealer's Next Card is " <<card5;

  }//end IF

      else if (card5 == 1)
		{//begin ELSE

				  cout << "\nThe Dealer's Next Card is ACE";

      }//end ELSE

			 else if (card5 == 11)
		   {//begin ELSE

					card5 = 10;
					  cout << "\nThe Dealer's Next Card is JACK";

			 }//end ELSE

				else if (card5 == 12)
		        {//begin ELSE

					     card5 = 10;
							 cout << "\nThe Dealer's Next Card is QUEEN";

	            }//end ELSE

		             else if (card5 == 13)
		             {//begin ELSE

						      card5 = 10;
									cout << "\nThe Dealer's Next Card is KING";

	                 }//end ELSE

  cout << "\n";
          
          deal_total = deal_total + card5;

  cout << "\nThe Dealer's Current Total is "<<deal_total;
  cout << "\n";

}//
//*************************************************************************
void Results()
{//

  if (deal_total > 21)
  {//begin IF

	  cout << "\n";
	  cout << "\nThe Dealer's Total Score is "<<deal_total;
	  cout << "\n";
	  cout << "\nThe Dealer's Has Exceeded 21!!!\n";
	  cout << "Dealer Loses!!!\n";
	  cout << "Player Wins!!!\n";

  }//end IF

  else if (deal_total < play_total)
  {//begin ELSE IF

			cout << "\n";
			cout << "\nThe Dealer's Total Score is "<<deal_total;
			cout << "\n";
			cout << "\nThe Player's Total Score is "<<play_total;
			cout << "\n";
			cout << "\nThe Player Has More Points";
			cout << "\nPlayer Wins!!!\n";

  }//end ELSE IF

         else if (deal_total > play_total)

		 {//begin ELSE IF

					 cout << "\n";
					 cout << "\nThe Dealer's Total Score is "<<deal_total;
					 cout << "\n";
					 cout << "\nThe Player's Total Score is "<<play_total;
					 cout << "\n";
					 cout << "\nThe Dealer Has More Points!!!\n";
					 cout << "Dealer Wins!!!\n";

         }//end ELSE IF

		       else 
			   {//begin ELSE

               cout << "\n";
					cout << "\nThe Dealer's Total Score is "<<deal_total;
					cout << "\n";
					cout << "\nThe Player's Total Score is "<<play_total;
				   cout << "\n";
				   cout << "\nBoth Dealer and Player Have The Same Score\n";
				   cout << "Draw!!!\n";

		       }//end ELSE

}//
//*************************************************************************

void main()
{//begin program

  Intro();

}//end program

Recommended Answers

All 3 Replies

Hey for the menu's instead of using the If (Conditons ) And testing it to a char . You should try using the Switch operator as it is a cleaner code then the If.. AND else if..

1. main returns int, not void.

2. //begin program, //end ELSE etc
These kinds of comments are generally not useful at all, except perhaps you can't see the whole function on screen at once (by then, you have other problems).
But some tutors seem to like them, so if that's what you're supposed to do then that's fine.

3. Your indentation needs work. Check your editor to see if
- you have a "use spaces for tabs" option (enable it)
- you have a "show whitespace" option, to see where the spaces and tabs are.
It may look all nice and tidy in your editor, but it's a mess here. Most forums make a complete mess of handing code with mixed spaces and tabs.

4. None of the functions take parameters, and only one returns a result.
This promotes an excess of global variables.

5. int card1, card2, card3, card4, card5
Consider int cards[5]; instead.

6. Choose your language
#include<iostream.h> // a C++ header (old C++ I might add)
#include<stdlib.h> // a C header
#include<conio.h> // a DOS header

I could only see a reason to include iostream, so you could probably ditch the rest.

On a side note, you should really be using a compiler which uses #include <iostream> Old compilers are all very well, but you'll find yourself with some catching up to do when you leave college.

Looks very complicated .. It is better to use switch statement.
Y you have used iomanip header file .
Also tell me Y You have used Conio header file.Using header files without there usage gives a bad impression.

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.