c++ lack jacks game for beginners in cmpsc 121

This is a beginners game of lackjacks that does not use any functions, arrays, or pointers. This is an excellent project to get to know the basics of C++ since you will be using variables, conditional statements, loops, and basic console I/O (input/output). This is a Windows 32 bit console application, so you should be able to paste this code into a blank project that you added a source page.

For those not familiar with the game of BlackJack the purpose is to get as close to 21 as possible without going over and beating the dealer’s hand. The player gets two cards and the dealer gets one card. The player can take a hit and the dealer has to hit until above the total of 17. Below is the source code, but do not copy and paste this for any homework assignment. Use this code to get you going with your own project. HINT: create pseudo code then start coding from the outside in, like start with the player wanting to play again then the initial deal.

If you select all the code below and paste it into a C++ source file you will also get the code not visible on this page. Please take the time to check out some of my sponsors on the left.

 

/* The object of this game is to score 21 or as close to 21
without going bust since this is a beginner's game there are
no functions and we will only be using iostream and
namespace std for the C++ Windows 32 Console game*/

//predef
#include 
using namespace std;

int main()
{
//variables	
	int pcard1, 
		pcard2,
		ptotal,
		dcard1,
		dtotal;
	char playagain,
		anothercard;

//Tell the player a bit about the game
cout << "This is the game of BlackJack otherwise known as 21. The object\n";
cout << "of the game is to get as close to 21 as possible without going over\n";
cout << "and beating the dealers score. If you get 21 on the initial deal\n";
cout << "you win.\nGood Luck!\n\n";

//set playagain to yes to get the game started and initialize variables
	playagain = 'y';
	anothercard = 'n';
	dtotal = 0;
	ptotal = 0;
	pcard1 = 0;
	pcard2 = 0;
	dcard1 = 0;

//First while statement is to see whether the user
//wants to play again or not.

	while (playagain == 'y')
	{
//Set up the initial cards. The player gets 2 and the dealer gets 
// one with a hold card
		pcard1 = 1 + rand() % 13;

//Take care of Aces and face cards;
		switch (pcard1)
		{
		case 1 :
			pcard1 = 11;			
				break;
		case 11 : 
			pcard1 = 10 ;
			break;
		case 12 :
			pcard1 = 10;
			break;
		case 13 :
			pcard1 = 10;
			break;
		default:
			break;
		} 

		pcard2 = 1 + rand() % 13;
		//Take care of Aces and face cards;
		switch (pcard2)
		{
		case 1 :
			pcard2 = 11;
				break;
		case 11 : 
			pcard2 = 10 ;
			break;
		case 12 :
			pcard2 = 10;
			break;
		case 13 :
			pcard2 = 10;
			break;
		default:
			break;
		} 

//Display the player's first two cards and total
cout << "Your first two cards are " << pcard1  << " and "  << pcard2 << "\n";
ptotal = pcard1 + pcard2;
cout << "Your total is " << ptotal << "\n\n";

//Dealer's card
		dcard1 = 1 + rand() % 13;
		switch (dcard1)
		{
		case 1 :
			dcard1 = 11;
				break;
		case 11 : 
			dcard1 = 10 ;
			break;
		case 12 :
			dcard1 = 10;
			break;
		case 13 :
			dcard1 = 10;
			break;
		default:
			break;
		} 
		cout << "The dealer's show card is " << dcard1 << "\n\n";
		dtotal = dcard1 + dtotal;

//See if the player has 21 for this game we don't care about the dealer
		if (ptotal == 21)
		{
			cout << "\nYOU HAVE BLACKJACK!\n";
		}
//Does the play want a hit or another card?
cout << "Do you want another card? "; cin >> anothercard;

//Here is where we play the game. The first round is \
// going to be the player
while (anothercard == 'y')
{
pcard1 = 1 + rand() %13;
if (pcard1 == 1 || pcard1 == 11)
{
cout << "Your card is an Ace. Do you want it to count as a 1 or 11\t"; cin >> pcard1;
}
switch (pcard1)
{					
		case 12 :
		pcard1 = 10;
		break;
	case 13 :
		pcard1 = 10;
		break;
	default:
		break;
	} 
ptotal = pcard1 + ptotal;
if (ptotal > 21)
{
cout << "Your card is " << pcard1 << "\n";
cout << "Your total is " << ptotal << "\n";
cout << "You are over 21 and have busted\n";
anothercard = 'n';
}
else if (ptotal < 22)
{
cout << "Your card is " << pcard1 << "\n";
cout << "Your total is now " << ptotal << "\n";
cout << "Do you want another card?\t (y or n)";  cin >> anothercard;
			}
		}

//If the player does not bust the dealer takes another card and subsequent 
//cards until not below 17
		while (ptotal < 22 && dtotal < 17 )
		{
			dcard1 = 1 + rand() % 13;
			switch (dcard1)
			{
				case 1 :
					dcard1 = 11;
					break;
				case 11 : 
					dcard1 = 10 ;
					break;
				case 12 :
					dcard1 = 10;
					break;
				case 13 :
					dcard1 = 10;
					break;
				default:
					break;
			} 
			dtotal = dcard1 + dtotal;
			cout << "Dealer draws a " << dcard1 << "\n";
			cout << "Dealer total is " << dtotal << "\n"; 	 dcard1 = 0;  		}  //Let's find a winner  		if (ptotal > dtotal && ptotal < 22 || dtotal > 22)
		{
			cout << "You win!!\n";  		} 		 else if (dtotal > ptotal && dtotal < 22)
		{
			cout << "Dealer Wins!!\n";
		}
		if (ptotal == 21 && dtotal == 21)
		{
			cout << "It's a draw\n";
		}

//Clean up our variables and see if we want to play again
		dtotal = 0;
		ptotal = 0;
		pcard1 = 0;
		pcard2 = 0;
		dcard1 = 0;
		cout << "Do you want to play again?\t ([y]es or [n]o)\t";  		cin >> playagain;
		cout << "\n\n\n";
	}
	return 0;

}

Enjoy!

Comments are closed.