Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I want to play blackjack (C++)

+12 votes
asked Apr 7, 2024 by Matteo Brini (220 points)
Hi, I will say I'm good at school C++ what libraries should i need to learn to make a working code?

2 Answers

+1 vote
answered Apr 8, 2024 by Peter Minarik (101,340 points)

To be able to create a C++ program, you'll need to arm yourself with the following:

Follow the tutorials to learn the basics. Use the reference manual to see how each function should be used correctly.

And of course, use a search engine to look for problems you run into, chances are someone else has come across the same problem and has a solution online.

Good luck!

+1 vote
answered Apr 2, 2025 by Kunal Dodamani (270 points)
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <ctime>
#include <map>

using namespace std;

struct Card {
    string suit;
    string rank;
};

class Deck {
private:
    vector<Card> cards;
public:
    Deck() {
        string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        string ranks[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        
        for (const auto& suit : suits) {
            for (const auto& rank : ranks) {
                cards.push_back({suit, rank});
            }
        }
        shuffleDeck();
    }

    void shuffleDeck() {
        random_device rd;
        mt19937 g(rd());
        shuffle(cards.begin(), cards.end(), g);
    }

    Card drawCard() {
        if (!cards.empty()) {
            Card drawn = cards.back();
            cards.pop_back();
            return drawn;
        }
        return {"", ""};
    }
};

class PokerGame {
private:
    Deck deck;
    vector<Card> playerHand;
    vector<vector<Card>> aiHands;
    vector<Card> communityCards;
    int playerChips;
    vector<int> aiChips;
    int pot;
    bool playerFolded;
    vector<bool> aiFolded;
    int numAI;

public:
    PokerGame(int numAIPlayers = 2) : playerChips(1000), pot(0), playerFolded(false), numAI(numAIPlayers) {
        aiChips.resize(numAI, 1000);
        aiHands.resize(numAI);
        aiFolded.resize(numAI, false);
    }

    void dealInitialCards() {
        playerHand.push_back(deck.drawCard());
        playerHand.push_back(deck.drawCard());
        for (int i = 0; i < numAI; i++) {
            aiHands[i].push_back(deck.drawCard());
            aiHands[i].push_back(deck.drawCard());
        }
    }

    void dealFlop() {
        for (int i = 0; i < 3; i++) {
            communityCards.push_back(deck.drawCard());
        }
    }

    void dealTurnOrRiver() {
        communityCards.push_back(deck.drawCard());
    }

    void showHand(const vector<Card>& hand, const string& owner) {
        cout << owner << "'s hand: " << endl;
        for (const auto& card : hand) {
            cout << card.rank << " of " << card.suit << endl;
        }
    }

    void showCommunityCards() {
        cout << "\nCommunity Cards: " << endl;
        for (const auto& card : communityCards) {
            cout << card.rank << " of " << card.suit << endl;
        }
    }

    void bettingRound(const string& stage) {
        cout << "\n" << stage << " Betting Round: " << endl;
        if (!playerAction()) return;
        aiActions();
        showCommunityCards();
    }

    bool playerAction() {
        int choice;
        cout << "You have " << playerChips << " chips. Choose an action: (1) Bet (2) Fold (3) Check: ";
        cin >> choice;
        if (choice == 2) {
            cout << "You folded! AIs continue playing." << endl;
            playerFolded = true;
            return false;
        }
        if (choice == 3) {
            cout << "You checked." << endl;
            return true;
        }
        int bet;
        cout << "Enter bet amount: ";
        cin >> bet;
        if (bet > playerChips) {
            cout << "You don't have enough chips!" << endl;
            return false;
        }
        playerChips -= bet;
        pot += bet;
        cout << "You bet " << bet << " chips. Pot is now " << pot << " chips." << endl;
        return true;
    }

    void aiActions() {
        for (int i = 0; i < numAI; i++) {
            if (aiFolded[i]) continue;
            if (rand() % 100 < 30) {
                cout << "AI " << i + 1 << " folded!" << endl;
                aiFolded[i] = true;
                continue;
            }
            int bet = min(50, aiChips[i]);
            aiChips[i] -= bet;
            pot += bet;
            cout << "AI " << i + 1 << " bets " << bet << " chips. Pot is now " << pot << " chips." << endl;
        }
    }

    string determineWinner() {
        if (playerFolded) return "AIs win by default.";
        
        int playerScore = rand() % 100;
        vector<int> aiScores(numAI);
        for (int i = 0; i < numAI; i++) {
            if (!aiFolded[i]) aiScores[i] = rand() % 100;
            else aiScores[i] = -1;
        }

        cout << "\nYour hand strength: " << playerScore << endl;
        for (int i = 0; i < numAI; i++) {
            if (!aiFolded[i]) {
                cout << "AI " << i + 1 << " hand strength: " << aiScores[i] << endl;
            }
        }

        int highestScore = playerScore;
        string winner = "You win the pot!";
        for (int i = 0; i < numAI; i++) {
            if (!aiFolded[i] && aiScores[i] > highestScore) {
                highestScore = aiScores[i];
                winner = "AI " + to_string(i + 1) + " wins the pot!";
            }
        }
        return winner;
    }

    void play() {
        dealInitialCards();
        showHand(playerHand, "Your");
        cout << numAI << " AI players have been dealt cards." << endl;
        
        dealFlop();
        bettingRound("Flop");
        
        dealTurnOrRiver();
        bettingRound("Turn");
        
        dealTurnOrRiver();
        bettingRound("River");
        
        for (int i = 0; i < numAI; i++) {
            if (!aiFolded[i]) showHand(aiHands[i], "AI " + to_string(i + 1));
        }
        
        cout << "\nFinal Pot: " << pot << " chips." << endl;
        cout << determineWinner() << endl;
    }
};

int main() {
    srand(time(0));
    PokerGame game(3); // Adjust number of AI players
    game.play();
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...