rendered paste body//BlackJack
//Classic Gambling game where the player counts to 21.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NUM_CARDS_IN_DECK = 13; //there are 13 cards in a suit;
const int MAX_CARDS_IN_HAND = 11;//player's limit of how many card he can obtain in one hand.
int main ()
{
char again = 'y';
char approach = 'y';
//code from deck maker start
srand((unsigned int)time(0)); //seeds random numbers for game
int deck[NUM_CARDS_IN_DECK];
int value[NUM_CARDS_IN_DECK] = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
char name[NUM_CARDS_IN_DECK] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
//deck end
int randomNumber = rand(); //generate random number!
int high = 21;
int low = 10;
int range = (high - low) + 1;
int dealerNumber = rand()% range + low;
int sum = 0;
int i = 0;
int handValueTotal = 0;
int finalScore = 0;
int startMoney = 120; //starting spending money
finalScore += startMoney;
int feePerRound = 20; //how much it costs to replay
int tableFee = feePerRound; //amount player pays to get cards/play one round.
int winnings = 0, moneyBet; //how much money the player will bet after seeing the cards.
//deck maker start
//initializing deck
for( int i=0; i<NUM_CARDS_IN_DECK; i++ )
{
deck[i] = i;
}
//shuffle deck
for( int i=0; i<NUM_CARDS_IN_DECK; i++ )
{
int randomCard = rand()%NUM_CARDS_IN_DECK;
int temp = deck[i];
deck[i] = deck[randomCard];
deck[randomCard] = temp;
}
int currentDeckPosition = 0;
int hand[MAX_CARDS_IN_HAND];
int numCardsInHand = 0;
// deck maker end
cout << "\t\t\tWelcome to Blackjack!"<< endl;
cout << "\nWe won't try to take your money... much."<< endl;
cout << "\nYou have currently: " << startMoney;
cout << "\nPay the table minimum?(y/n): ";
cin >> approach;
while (again == 'y')
{
cout << "how Much would you like to bet ?: ";
cin >> moneyBet;
finalScore -= tableFee;
while (approach == 'y')
{
int choice;
cout << "\nYour two cards: ";
hand[numCardsInHand] = deck[currentDeckPosition];
cout << "\n " << name[hand[0]] << endl;
if (numCardsInHand ==0)
{
numCardsInHand++;
currentDeckPosition++;
}
hand[numCardsInHand] = deck[currentDeckPosition];
cout <<" " << name[hand[1]] << endl;
if(numCardsInHand < 2)
{
for( int i=0; i<numCardsInHand; i++)
{
i++;
sum += value[hand[0]];
handValueTotal = (sum +=value[hand[i]]);
cout << "hand total: " << handValueTotal;
}
}
else
{
handValueTotal +=value[hand[numCardsInHand]];
cout << "hand total: " << handValueTotal<< endl<< endl;
}
if(handValueTotal > 21)
{
cout << "\tBUST!"<< endl;
cout << "You went over 21."<< endl;
finalScore -= moneyBet;
break;
}
cout << "\nYou have Currently: " <<finalScore << endl;
cout << "1- Stay"<< endl;
cout << "2- Hit"<< endl;
cout << "3- Bet"<< endl;
cout << "4- Quit"<< endl;
cout << "\nSelect a number to proceed: ";
cin >> choice;
switch (choice)
{
case 1:
cout<< "dealer got: "<< dealerNumber << endl;
cout << "You got:" << handValueTotal<< endl;
if(dealerNumber >handValueTotal)
{
cout << "you lose!"<< endl;
finalScore -= moneyBet;
cout << "You have Currently: " << finalScore << endl;
break;
}
if(dealerNumber < handValueTotal)
{
cout << "You win!"<< endl;
finalScore += moneyBet;
cout << "You have Currently: " << finalScore << endl;
break;
}
break;
case 2:
numCardsInHand++;
currentDeckPosition++;
hand[numCardsInHand] = deck[currentDeckPosition];
cout << "you were dealt: " << name[hand[numCardsInHand]];
break;
case 3:
cout << "how Much?: "<<endl;
cin >> moneyBet;
break;
case 4:
cout << "Your total winnings are: " << finalScore <<endl;
system ("pause");
return 0;
default:
cout << "you didnt enter 1,2, or 3."<< endl;
}
}
cout << "Play again?: ";
cin >> again;
numCardsInHand = 0;
currentDeckPosition = 0;
sum = 0;
for( int i=0; i<NUM_CARDS_IN_DECK; i++ )
{
int randomCard = rand()%NUM_CARDS_IN_DECK;
int temp = deck[i];
deck[i] = deck[randomCard];
deck[randomCard] = temp;
}
int currentDeckPosition = 0;
int hand[MAX_CARDS_IN_HAND];
int numCardsInHand = 0;
}
cout << "Your total winnings are: " << finalScore;
system ("pause");
return 0;
}