mercredi 16 décembre 2020

Blackjack and Vectors is there a way to prevent the dealers third card from having the default value 1?

I am creating the game of blackjack using vectors. I don't want to add the value of 0 to the vector. If I add the number 0 to the vector the random number generation will break due to the possibility that you can obtain a zero. You never want to have the number 0 in the game of blackjack is there a way to prevent the vector from obtaining the value 1 straight out of the gates within index 1?



//Buy default the vector is equal to 1, because the index is 0 which on the first index the index is equal to 1.

#include <iostream>
#include <ctime>
#include <vector>
#include <random>
//This 1 and 11 are so that just in case bust the value changes accordingly. 
int ace11 = 11;
int ace1 = 1;
////Here is the problem I have a vector of integers, I don't want a 0 added to this vector because if I add a 1 here in this vector that would break the random number generation. You never want to be able to have hit a zero in the game of blackjack.
std::vector<int> card1 = {ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11};
std::vector<int> card2 = { 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11};
std::vector<int> card3 = {ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11 };
std::vector<int> card4 = {ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11 };
std::vector<int>  DealersCard1 = { ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11 };
std::vector<int>  DealersCard2 = { ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11 };
std::vector<int>  DealersCard3 = { ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11 };
std::vector<int>  DealersCard4 = { ace1, ace1, ace1, ace1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ace11, ace11, ace11, ace11 };
//I am using vectors in order to simulate the suits. 
int randomCardInVector1;
int randomCardInVector2;
int randomCardInVector3;
int randomCardInVector4;
int dealersRandomCardInVector1;
int dealersRandomCardInVector2;
int dealersRandomCardInVector3;
int dealersRandomCardInVector4;
int dealersCardTotal;
int cardTotal;
int play;
void dealCards();
void youLose();
void youWon();
void Hit1();
void Hit2();
void Hit3();
void stand();

int main()
{ 

        std::cout << "Welcome to the Blackjack table!" << std::endl;
        std::cout << "Please press ENTER to get your first cards." << std::endl;
        std::cin.get();
        dealCards();
    do {
        std::cout << "Your first card: " << card1[randomCardInVector1] << std::endl;
        std::cout << "Your second card: " << card2[randomCardInVector2] << std::endl;
        cardTotal = card1[randomCardInVector1] + card2[randomCardInVector2];
        if (cardTotal > 21)
        {
            ace11 = ace1;
        }
        std::cout << "Your card total: " << cardTotal << std::endl;
        std::cout << "*********************************************" << std::endl;
        std::cout << "Dealer's First Card: " << DealersCard1[dealersRandomCardInVector1] << std::endl;
        std::cout << "Dealer's Second Card: " << DealersCard2[dealersRandomCardInVector2] << std::endl;
        dealersCardTotal = DealersCard1[dealersRandomCardInVector1] + DealersCard2[dealersRandomCardInVector2];
        std::cout << "Dealer's Card Total: " << dealersCardTotal << std::endl;
        if (cardTotal > 21)
        {
            youLose();
        }
        if (dealersCardTotal > 21)
        {
            youWon();
        }
        
        if (cardTotal == 21)
        {
            youWon();
        }
        else if (dealersCardTotal == 21)
        {
            youLose();
        }
        std::cout << "Would you like to hit or stay?" << std::endl;
        std::cout << "1)Hit" << std::endl;
        std::cout << "2)Stay" << std::endl;
        std::cin >> play;
        switch (play)
        {
        case 1:
            Hit1();
            break;
        case 2:
            stand();
            break;
        case 3:
            break;
        }
        system("cls");
        std::cout << "Your first card: " << card1[randomCardInVector1] << std::endl;
        std::cout << "Your second card: " << card2[randomCardInVector2] << std::endl;
        std::cout << "Your third card: " << card3[randomCardInVector3] << std::endl;
        cardTotal = cardTotal + card3[randomCardInVector3];
        if (cardTotal > 21)
        {
            ace11 = ace1;
        }
        std::cout << "Your card total: " << cardTotal << std::endl;
        std::cout << "*********************************************" << std::endl;
        std::cout << "Dealer's First Card: " << DealersCard1[dealersRandomCardInVector1] << std::endl;
        std::cout << "Dealer's Second Card: " << DealersCard2[dealersRandomCardInVector2] << std::endl;
        std::cout << "Dealer's Third Card: " << DealersCard3[dealersRandomCardInVector3] << std::endl;
        dealersCardTotal =  dealersCardTotal + DealersCard3[dealersRandomCardInVector3];
        if (dealersCardTotal > 21)
        {
            ace11 = ace1;
        }
        std::cout << "Dealer's Total: " << dealersCardTotal << std::endl;
        std::cin.get();
        if (cardTotal > 21)
        {
            youLose();
        }
        if (cardTotal == 21)
        {
            youWon();
        }
        std::cout << "Would you like to hit or stay?" << std::endl;
        std::cout << "1)Hit" << std::endl;
        std::cout << "2)Stay" << std::endl;
        std::cin >> play;
        switch (play)
        {
        case 1:
            Hit2();
            break;
        case 2:
            
            break;
        case 3:
            break;
        }
        system("cls");
        std::cout << "Your first card: " << card1[randomCardInVector1] << std::endl;
        std::cout << "Your second card: " << card2[randomCardInVector2] << std::endl;
        std::cout << "Your third card: " << card3[randomCardInVector3] << std::endl;
        std::cout << "Your fourth card: " << card4[randomCardInVector4] << std::endl;
        cardTotal = cardTotal + card4[randomCardInVector4];
        if (cardTotal > 21)
        {
            ace11 = ace1;
        }
        std::cout << "Your card total: " << cardTotal << std::endl;
        std::cout << "*********************************************" << std::endl;
        std::cout << "Dealer's First Card: " << DealersCard1[dealersRandomCardInVector1] << std::endl;
        std::cout << "Dealer's Second Card: " << DealersCard2[dealersRandomCardInVector2] << std::endl;
        std::cout << "Dealer's Third Card: " << DealersCard3[dealersRandomCardInVector3] << std::endl;
        std::cout << "Dealer's Fourth Card: " << DealersCard4[dealersRandomCardInVector4] << std::endl;
        
        dealersCardTotal = dealersCardTotal + DealersCard4[dealersRandomCardInVector4];
        if (dealersCardTotal > 21)
        {
            ace11 = ace1;
        }
        std::cout << "Dealer's Total: " << dealersCardTotal << std::endl;
        std::cin.get();
        if (cardTotal > 21)
        {
            youLose();
        }
        if (cardTotal == 21)
        {
            youWon();
        }
    } while (play != 3);
    

    

}
void stand()
{
    srand(time(NULL));
    dealersRandomCardInVector3 = rand() % 52 + 0;
}
void Hit1()
{
    srand(time(NULL));
    randomCardInVector3 = rand() % 52 + 0;
}
void Hit2()
{
    srand(time(NULL));
    randomCardInVector4 = rand() % 52 + 0;
}
void Hit3()
{

}
void dealCards()
{
    srand(time(NULL));
    randomCardInVector1 = rand() % 52 + 0;
    randomCardInVector2 = rand() % 52 + 0;
    dealersRandomCardInVector1 = rand() % 52 + 0;
    dealersRandomCardInVector2 = rand() % 52 + 0;
}

void youLose()
{
    system("cls");
    std::cout << "I am sorry you lost.." << std::endl;
    return;
}


void youWon()
{
    system("cls");
    std::cout << "You Won!" << std::endl;
    return;
}




Aucun commentaire:

Enregistrer un commentaire