#include <iostream>
#include <vector>
#include <random>
#include <ctime>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int drawCard() {
static std::mt19937 rng(static_cast<unsigned int>(time(nullptr)));
std::uniform_int_distribution<int> dist(2, 14);
return dist(rng);
}
int cardValue(int card) {
if (card >= 11 && card <= 13) return 10;
if (card == 14) return 11; // Ace
return card;
}
int handTotal(const vector<int>& hand) {
int total = 0;
int aces = 0;
for (int card : hand) {
int value = cardValue(card);
total += value;
if (card == 14) aces++;
}
// Adjust for aces if bust
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
}
int main() {
vector<int> playerHand;
vector<int> dealerHand;
char choice = 'y';
cout << "Welcome to Blackjack\n" << endl;
// Initial deal
playerHand.push_back(drawCard());
playerHand.push_back(drawCard());
dealerHand.push_back(drawCard());
dealerHand.push_back(drawCard());
while (true) {
int playerTotal = handTotal(playerHand);
cout << "Your total: " << playerTotal << endl;
cout << "Dealer shows: " << cardValue(dealerHand[0]) << endl;
if (playerTotal > 21) {
cout << "You busted. Dealer wins." << endl;
return 0;
}
cout << "Hit or stand? (y/n): ";
cin >> choice;
cout << endl;
if (choice == 'y' || choice == 'Y') {
playerHand.push_back(drawCard());
} else {
break;
}
}
cout << "Dealer's turn...\n" << endl;
while (handTotal(dealerHand) < 17) {
dealerHand.push_back(drawCard());
}
int playerTotal = handTotal(playerHand);
int dealerTotal = handTotal(dealerHand);
cout << "Your final total: " << playerTotal << endl;
cout << "Dealer final total: " << dealerTotal << endl;
if (dealerTotal > 21 || playerTotal > dealerTotal) {
cout << "You win!" << endl;
} else if (playerTotal < dealerTotal) {
cout << "Dealer wins." << endl;
} else {
cout << "Push. It's a tie." << endl;
}
return 0;
}