// BlackJack Game // Written by Jason Radosevich http://www.terminalfusion.com #include using std::cout; using std::left; using std::right; using std::cin; using std::endl; #include using std::setw; #include using std::rand; using std::srand; #include using std::time; #include using std::string; class DeckOfCards { public: DeckOfCards(); // constructor void shuffle(); //shuffles the cards in the deck void initialDeal(); //deals the cards. void hit(); //Allows the user to take a hit if they want to void stay(); //Allows the user to Stay if the want to void checkCards();//checks the dealers cards. if they do not have 17 it will force a hit. If they have 17 or greater then we compare cards to find the winner. void updateDealer();//updates the dealer arrays with the correct card values and correct face values void updateUser();//updates the user arrays with the correct card values and correct face values char hitOrStay();//asks the user if they want to hit or stay. char playAgain();//asks the user if they want to play again void endGame();//ends the game if the user has negative money void dealCards();//function to deal the initial cards char gameOver;//holds the answer to if the game is over or not. private: int deck[4][13]; string dealerFace[26];//face deck array to put in the face falue in. string userFace[26];//deck array to put in the actual value of the cards. int dealer[26]; //initialize the Dealer array to hold the dealers hand set to 26 cards to accomodate 1/2 of the deck int user[26]; //initialize the User array to hold the users hand set to 26 cards to accomodate 1/2 of the deck int currPosition; //finds where in the deck of cards we are so we can assign the next one out. int userPosition;// the current position the user is in in the array int dealPosition;// the current position the dealer is in in the array int curRow;//current row the game is in in the deck of cards int userTotal;// the users total value int dealerTotal;//the dealers total value int aceValue;// the value to determine if the user wants to have the ace as a 1 or a 11 double gamblePot;//the amount of money the user has won. int games;// the number of games that the user has played so far. double betAmt;//the amount of money the user wants to bet char ans;//holds the answer to if they want to hit or not char ans2;//holds the answer to if they want to play or not. }; //Default Constructor DeckOfCards::DeckOfCards() { int dealPosition = 0; int currRow = 0; int currPosition = 0; int userPosition = 0; int dealer[26] = {0}; int user[26] = {0}; int x = 1; int aceValue = 0; double gamblePot = 0; int games = 0; double betAmt = 0; string userFace[26] = {""}; string dealerFace[26] = {""}; char ans =' '; char ans2 = ' '; char gameOver = ' '; // Loop thru each row of the deck x = 1; for (int row = 0; row<=3; row++) { //Loop thru columns of deck for current rows for (int column = 0; column <= 12; column++) { deck[row][column] = x; //initialize slof of deck to 0 x++; } } srand(time(0));//set random number generator } //shuffle the deck of cards void DeckOfCards::shuffle() { //declare the vars int row; int column; int *firstValue; int *randValue; int tempValue; int Rrow; int Ccolumn; //resetting the current row and position. curRow = 0; currPosition = 0; games = 0; for (int x = 0; x <= 4; x++) { for (int row = 0; row<=3; row++) { //Loop thru columns of deck for current rows for (int column = 0; column <= 12; column++) { firstValue = &deck[row][column]; //initialize slof of deck to 0 Rrow = rand() % 4; //randomly select the row Ccolumn = rand() % 13; //randomly select the column randValue = &deck[Rrow][Ccolumn]; //cout << "The start value = " << *firstValue << "::" << *randValue << endl; tempValue = *firstValue; *firstValue = *randValue; *randValue = tempValue; //cout << "The End value = " << *firstValue << "::" << *randValue << endl << endl << endl; } } } currPosition = 1; } // this function will start to make the first deals after the bets have been made void DeckOfCards::initialDeal() { //if the have not played any games then ask them how much they want to bet if(games < 1) { gamblePot = 500; cout << " You have a starting pot of $"<> betAmt; dealCards(); } else if (games >= 1 && gamblePot > 0)//if they have alread played then ask how much they want to bet? { cout << "You have $"<>betAmt; dealCards(); } } void DeckOfCards::dealCards() { games = games + 1; cout << "Games = "<< games << endl; if (games > 1) { cout <<"Not resetting the positions " < 21) { cout << "Sorry but you BUST! Dealer is the winner. " <> ans2; if(ans2 =='s'|| ans2 == 'S') { stay(); } else if(ans2 =='h'|| ans2 == 'H') { hit(); } } } //Let the user stay at their current positon.. If the dealer is less than 17 force a hit on the dealer. void DeckOfCards::stay() { while(dealerTotal < 17) { if(currPosition == 13) { curRow++; currPosition = 0; } else if(curRow == 3 && currPosition ==12) { cout << " The Dealer has hit the end of the deck.. Need to re-shuffle" < 21) { cout << " Dealer busted you won!" < userTotal) { cout << " Sorry, but the dealer won that round!" <> aceValue; //if they did not enter in a valid number make them try again. while(aceValue != 1 && aceValue != 2) { cout << "Pleae enter a valid number! 1 = 1, 2 = 11 " <> aceValue; } // if they want it a 1, then updte the value if (aceValue == 1) { user[userPosition] = 1; } //or if they want it as a 11 then update the value. else if (aceValue == 2) { user[userPosition] = 11; } cout << endl <> ans; if(ans == 'h' || ans == 'H') { hit(); } else if(ans =='s'|| ans == 'S') { stay(); } while (ans != 'h' && ans != 'H' && ans != 's' && ans != 'S') { cout <<" INVALID ANSWER Only (h)it or (s)tay" <> ans; } return ans; } //this function will get the users input to if they want to play again or not. char DeckOfCards::playAgain() { cout << "Do you want to play that again ? (y|n) " <> ans2; while (ans2 != 'y' && ans2 != 'Y' && ans2 !='n' && ans2 != 'N') { cout << "Please enter in a valid answer (Y|N)"<> ans2; } while (ans2 == 'y' || ans2 == 'Y') { if(games > 0 && gamblePot <= 0) { cout << "Sorry but you have no money. Please get some more and come back! " <