Access over 40 Millions of academic & study documents

Blackjack

Content type
User Generated
Subject
C Programming
School
Montana State University - Bozeman
Type
Homework
Showing Page:
1/6
Introduction
The task is to implement a Black Jack program for two players. The game is based on a 52-card deck
where each card face has some point count. Aces may be counted as 11 or as 1, whichever is better
for the player. The goal is to get more points than the enemy but definitely not more than 21.
The program stores its deck as array of C++ structures, shuffles the deck and then plays 5 rounds.
For each card, the current point count is shown. After each round the winning player and the current
score is shown.
Coding approach
I tried to make most constants used in the program (for example, the maximum allowable number of
points, 21) to be C++ constants. Also, I created several functions to make the program more modular.
I show star (*) after a score if an ace was counted as 1.
Conclusion
The program was relatively interesting to write because it deals with a game and uses random
numbers. It was not too hard to write but also not too simple. There are some additions possible to
make the game more interesting (manual choice whether to take the next card, for example).

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/6
Source listing ------------------------------------------------------------------------
// Card Deck.cpp : main project file.
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
// constants
const int NUM_SUITS = 4;
const int NUM_FACES = 13;
const int NUM_CARDS = NUM_SUITS*NUM_FACES;
const int NUM_PLAYERS = 2;
const int NUM_STATUSES = 4;
const int NUM_ROUNDS = 5;
const int NUM_CARDS_START = 2;
const int NUM_POINTS_MUST = 15;
const int MIN_POINTS_WANTS = 11;
const int MAX_POINTS_WANTS = 19;
const int NUM_POINTS_WIN = 21;
// global constant arrays
string faces[NUM_FACES] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string suits[NUM_SUITS] = { "Diamonds", "Clubs", "Hearts", "Spades" };
string Status[NUM_STATUSES] = { "At Player B", "At Player A", "In Deck", "Discard Pile"
};
int CardValue[NUM_FACES] = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
// struct representing a card
struct DefineCard
{
string suit;
string face;
int pointsValue; // FACE => 10 POINTS & ACE's => 1 OR 11 (WHICHEVER IS BEST FOR
PLAYER/DEALER)
int cardStatus; // 0 = dealt to Player1, 1 = dealt to Player2, 2 = InDeck, 3 =
DiscardPile
};
// shows card information on console
void printCard(DefineCard card)
{
cout << card.face << " of " << card.suit << " : Value is " << card.pointsValue;
cout << " : " << Status[card.cardStatus];
}
// shuffles the deck using pairwise swaps
void shuffleDeck(DefineCard Deck[NUM_CARDS])
{
DefineCard tempCard;
int a, b;
for (int x = 0; x < 600; x++)
{
do
a = rand() % NUM_CARDS, b = rand() % NUM_CARDS;
while (a == b);
tempCard = Deck[a];
Deck[a] = Deck[b];
Deck[b] = tempCard;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/6

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 6 pages?
Access Now
Unformatted Attachment Preview
Introduction The task is to implement a Black Jack program for two players. The game is based on a 52-card deck where each card face has some point count. Aces may be counted as 11 or as 1, whichever is better for the player. The goal is to get more points than the enemy but definitely not more than 21. The program stores its deck as array of C++ structures, shuffles the deck and then plays 5 rounds. For each card, the current point count is shown. After each round the winning player and the current score is shown. Coding approach I tried to make most constants used in the program (for example, the maximum allowable number of points, 21) to be C++ constants. Also, I created several functions to make the program more modular. I show star (*) after a score if an ace was counted as 1. Conclusion The program was relatively interesting to write because it deals with a game and uses random numbers. It was not too hard to write but also not too simple. There are some additions possible to make the game more interesting (manual choice whether to take the next card, for example). Source listing -----------------------------------------------------------------------// Card Deck.cpp : main project file. #include #include #include using namespace std; // constants const int NUM_SUITS = 4; const int NUM_FACES = 13; const int NUM_CARDS = NUM_SUITS*NUM_FACES; const int NUM_PLAYERS = 2; const int NUM_STATUSES = 4; const int NUM_ROUNDS = 5; const int NUM_CARDS_ ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4