You are on page 1of 7

// --------------------------------------------------------------------------// Team Undecided

:
//
// Jazzmine Bess, Dessirree Gray, Katrice St.Rose, Jynesia Stewart, Ron Waye
//
//
Final Project Blackjack
// --------------------------------------------------------------------------#include "blackjack.h" //connects header file
#include <iostream>//input and output
#include <iomanip> //manipulates display of output
#include <ctime>//time and data type time_t
#include <string>// strings
#include <cstdlib>//random generator rand srand
#include <fstream> //file stream
#include <algorithm> //needed for swap function in quicksort
using namespace std;
//----------------------------------------------//QuickSort Function Prototypes
//----------------------------------------------void quickSort(int[], int, int);
int partition(int[], int, int);
//----------------------------------------------//Recursive Function Declarations
//----------------------------------------------void revDisplay(const string& name);
//----------------------------------------------//Main Program
//----------------------------------------------int main()
{
//----------------------------------------------//Declare Variables
//----------------------------------------------Blackjack player, dealer; //objects of the blackjack class
bool CardDeck[52]; //keeps track of cards dealt
int DealerHand[13]; //Array of size 13 - If you have 13 cards in your hands
int PlayerHand[13];//you automatically bust
int *cards; //dynamically allocates an array
int DealerCardCount = 0; //initizlize how many cards you have
int PlayerCardCount = 0;//initizlize how many cards you have
time_t Time; //needed for the random function
time(&Time); //needed for the random function
srand(Time); //needed for the random function
string name; //gets name from user
ifstream inputFile; //reads the content of a file
ofstream outputFile; //displays content into a file
const int LENGTH = 1000; //used to read content from the file
char input[LENGTH]; //variable needed for file input
int choice; //gets users menu choice
//----------------------------------------------//Introduction to Program
//----------------------------------------------cout << setw(67) << "_______________________________________________________

_________________" <<endl <<endl;
cout << setw(50) << "A Team Undecided Productions" << endl << endl;
cout << setw(46) << "Welcome to a Game of" << endl << endl;
cout << setw(40) << "Blackjack" << endl << endl;
cout << setw(67) << "_______________________________________________________
_________________" <<endl <<endl;
//----------------------------------------------//Menu Options
//----------------------------------------------do
{
cout << "Menu\n\n\n";
cout << "1.Play Blackjack\n\n";
cout << "2.Rules of Blackjack\n\n";
cout << "3.Exit\n\n\n";
cout << "Enter Your Choice (1-3): ";
cin >> choice;
//Validate and process the menu choice
while ((choice < 1) || (choice > 3))
{
cout << "\nInvalid Input! Please enter a value of either 1, 2 or 3):
";
cin >> choice;
cout << endl;
}//end of while
//Switch Statement for Menu Option
switch (choice)
{
//----------------------------------------------//Menu Option #1
//----------------------------------------------case 1:
system ("cls");//clears the screen
cout << "Quick Play" << endl << endl;
cout << "To Begin Please <Enter> Your Name: ";
cin >> name;
cout << endl << "\n...And Here is Your Name Backwards!! ";
revDisplay(name);//recursive function call
cout << setw(67) << "_______________________________________________________
_________________" <<endl <<endl;
cout << endl << endl; //spacing
cout << "Hello " << name << " !!! Let's play a game of Blackjack! " << endl
<< endl;
cout << setw(67) << "_______________________________________________________
_________________" <<endl <<endl;
system ("pause");
system ("cls");
{
player.Shuffle(CardDeck);// shuffles cards
dealer.Shuffle(CardDeck);// shuffles cards

//Deals a random card to the Player and Stores the Value in the
array
PlayerHand[0] = player.GetCard(CardDeck);//deals hands - this ar
ray stores your card in array[0]
PlayerHand[1] = player.GetCard(CardDeck);//deals hands - this ar
ray stores your card in array[1]
//Deals a random card to the Player and Stores the Value in the
array
DealerHand[0] = dealer.GetCard(CardDeck);//deals hands - this array stor
es your card in array[0]
DealerHand[1] = dealer.GetCard(CardDeck);//deals hands - this ar
ray stores your card in array[1]
//Updates the Number of cards the Dealer and Player has
DealerCardCount = 2; // sets the number of cards in your hand to two
PlayerCardCount = 2; //sets the number of cards in your hand to
two
//---------------------------------------------//Variable Declaration
//---------------------------------------------char PlayerChoice; //user input to hit or stay
bool PlayerHits = true; //true if the player wants to hit otherw
ise false
int PlayerScore = player.ScoreHand(PlayerHand, PlayerCardCount);
//Update Player Score
// Print the dealt cards, but only the Dealer's second card
cout << endl << "The Dealer has a " ;
dealer.PrintCard(DealerHand[1]);
cout << " in his hand" << endl << endl;
cout << " ------------------------------------------------------";
cout << endl << endl;
//Prints the Players Hand
cout << name << " you were dealt two cards: ";
//----------------------------------------------//Hit or Stay Do-While Loop
//----------------------------------------------do
{
player.ScoreHand(PlayerHand, PlayerCardCount);//Update Score
player.PrintHand(PlayerHand, PlayerCardCount);//Print Hand
// Ask the Player whether he wants a hit or to stay
cout << endl << "Hit(h) or stay(s): ";
cin >> PlayerChoice;
if (PlayerChoice == 'h')
{
cout << endl <<"--------------------------------------------------------" <<endl;
cout << "You now have a ";
PlayerHand[PlayerCardCount] = player.GetCard(CardDeck);//Get

Card
++PlayerCardCount;
} //end of if
else if (PlayerChoice == 's') {
cout << "--------------------------------------------------------" <<endl;
PlayerHits = false;
} //end of else
else {
cout << "Error: Try Again!" << endl;
} //end of else
cout << endl;
//-----------------------------//Pointer to Allocate Memory
//-----------------------------cards = new int[PlayerCardCount];
// Get the Player's current score to update
PlayerScore
= player.ScoreHand(PlayerHand, PlayerCar
dCount);
//Check to See if Player Bust
} while (PlayerHits && PlayerScore < 22); //end of do while
//----------------------------------------------//Nest If-Else Statements Determing a Winner
//----------------------------------------------if (PlayerScore > 21) // Once the player is done taking hits, ch
eck whether he busted
{
// The Player busted. The House wins.
cout << "The Dealer Wins!!!" << endl;
cout<<endl<<endl;
player.DisplayAllCards(DealerHand, DealerCardCount, Play
erHand, PlayerCardCount);
}
else

// If the player didn't bust, then the house takes hits below 17
{
int DealerScore = dealer.ScoreHand(DealerHand, DealerCardCount);
while (DealerScore < 17) //Loop to keep hitting until dealer ha

s hand >17
{
DealerHand[DealerCardCount] = dealer.GetCard(CardDeck);
++DealerCardCount;
DealerScore = dealer.ScoreHand(DealerHand, De
alerCardCount);
} // end of nested while
bool DealerBusts = (DealerScore > 21); //Dealer Has more
than 21

if (DealerBusts)
{
// The House busted. Player wins
cout << "The Player Wins!!!" << endl;
cout<<endl<<endl;
player.DisplayAllCards(DealerHand, DealerCard
Count, PlayerHand, PlayerCardCount);
}
else
{
// Compare scores and determine the winn
er
if (PlayerScore == DealerScore)
{
// Tie
cout << "Tie!!!" << endl;
cout<<endl<<endl;
player.DisplayAllCards(DealerHand, D
ealerCardCount, PlayerHand, PlayerCardCount);
}
else if (PlayerScore > DealerScore)
{
// The Player wins
cout << "The Player Wins!!!" << endl;
cout<<endl<<endl;
player.DisplayAllCards(DealerHand, Deale
rCardCount, PlayerHand, PlayerCardCount);
}
else
{
// The Dealer wins
cout << "The Dealer Wins!!!" << endl;
cout<<endl<<endl;
player.DisplayAllCards(DealerHand, Deale
rCardCount, PlayerHand, PlayerCardCount);
} //end of else
}//end If
//----------------------------------------------//QuickSort Algorithm
//----------------------------------------------//Sorts the Array Using Quicksort
//Array, Where to Start, Where to end
system("pause");
system("cls");
quickSort(PlayerHand,0,PlayerCardCount-1);
cout <<"This is the Quicksort" <<endl <<endl;
for(int k = 0; k < PlayerCardCount;k++)
player.PrintCard(PlayerHand[k]);
cout <<endl << endl;
//-----------------------------------------------

//Write to a file
//----------------------------------------------//Open File
outputFile.open("OutputFile.txt");
outputFile << name << " Thank You for Playing!";
//Close the file
outputFile.close();
system ("pause");
system ("cls");
}
}
break;
//----------------------------------------------//Menu Option 2
//----------------------------------------------case 2:
inputFile.open("HowToPlay.txt"); //open file
system ("cls");
if (!inputFile) //Check for Errors
{
cout << "File open Error!" << endl;
system("pause");
return 0;
}
inputFile.getline(input, LENGTH); //Read Lines from file
while (!inputFile.fail())
{
cout << endl << input << endl;
inputFile.getline(input, LENGTH);
}
system("pause");
system ("cls");
inputFile.close(); //Close the File
break;
//----------------------------------------------//Menu Option 3
//----------------------------------------------case 3:
return 0;
break;
}//end of switch
} while (choice !=3); //end of do-while
//Free Dynamically allocated memory
delete [] cards;
cards = 0;
return 0;
}//end of int main ()
//----------------------------------------------//Quicksort uses the algorithm to sort player hand

//----------------------------------------------void quickSort(int array[], int start, int end)
{
if (start < end)
{
int p = partition(array, start, end);//Partition the array and get the pivo
t point
quickSort(array, start, p-1);
quickSort(array, p+1, end);//Sort the algorithm before the pivot point
}//end of if
}//end of function
//----------------------------------------------//Partition Function to rearrange elements
//----------------------------------------------int partition(int array[], int start, int end)
{
int pivotValue = array[start];
int pivotPosition = start;
for (int pos = start +1; pos <= end; pos++)
{
if(array[pos] < pivotValue)
{
swap(array[pivotPosition + 1], array[pos]);
swap(array[pivotPosition], array[pivotPosition +1]);
pivotPosition ++;
}//end of if
}//end of for loop
return pivotPosition;
}//end of function
//----------------------------------------------//Function to Reverse String
//----------------------------------------------void revDisplay(const string& name)
{
size_t n = name.size();
if(n == 1)
cout << name << endl;
else
{
cout << name[n-1];
string b = name.substr(0, n-1);
revDisplay(b);
}
}