//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.
//random number between 1 and 21 (modified)
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!\n\n";
cout << "\nWe won't try to take your money... much.\n";
cout << "You 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]] << "\n";
if (numCardsInHand ==0)
{
numCardsInHand++;
currentDeckPosition++;
}
hand[numCardsInHand] = deck[currentDeckPosition];
cout <<" " << name[hand[1]] << "\n";
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]]; //adds the value of any new cards obtained
cout << "hand total: " << handValueTotal;
}
if(handValueTotal > 21) //when the player's card total over 21, he busts/loses.
{
cout << "\n\n\tBUST!\n";
cout << "\nYou went over 21.";
finalScore -= moneyBet;
break;
}
cout << "\nYou have Currently: " <<finalScore;
cout << "\n1- Stay";
cout << "\n2- Hit";
cout << "\n3- Bet";
cout << "\n4- Quit";
cout << "\nSelect a number to proceed: ";
cin >> choice;
switch (choice)
{
case 1:
cout<< "dealer got: "<< dealerNumber;
cout << "\nYou got:" << handValueTotal;
if(dealerNumber >handValueTotal)
{
cout << "\nyou lose!\n";
finalScore -= moneyBet;
break;
}
if(dealerNumber < handValueTotal)
{
cout << "\nYou win!\n";
finalScore += moneyBet;
break;
}
break;
case 2:
numCardsInHand++;
currentDeckPosition++;
hand[numCardsInHand] = deck[currentDeckPosition];
cout << "you were dealt: " << name[hand[numCardsInHand]];
break;
case 3:
cout << "how Much?: ";
cin >> moneyBet;
break;
case 4:
cout << "\nYour total winnings are: " << finalScore <<"\n";
system ("pause");
return 0;
default:
cout << "you didnt enter 1,2, or 3./n";
}
}
cout << "Play again?: ";
cin >> again;
}
cout << "\nYour total winnings are: " << finalScore;
system ("pause");
return 0;
}