#include <iostream>#include <iomanip>#include <string>#include <algorithm>
using namespace std;
void createdeck(int[], int);
int main(){ int cash, bet, player, dealer, count, deck, ace, dace; char play; string action; int cards[52]; cash = 1000; player = 0; dealer = 0; bet = 0; count = 0; ace = 0; dace = 0; deck = 1; cout << "Welcome to the casino! Let's begin with a game of black jack!" << endl; cout << "You will begin with $1,000 in cash, don't lose it all!" << endl; cout << "You can place any size bet as long as it does not exceed the amount you have." << endl; cout << "If you lose the house takes all. If you win you double your bet!\n" << endl; cout << "-----------------------------------------------------------------\n\n"; createdeck(cards, deck); random_shuffle(&cards[0], &cards[51]); cout << "Enter 'Y' for yes to begin round: "; cin >> play; while (play == 'Y' || play == 'y') { cout << "Place your bet! "; cin >> bet; while (cash < bet) { cout << "Oops! You can't bet more than you have. Try again." << endl; cin >> bet; } while (bet < 1) { cout << "Oops! You need to bet more than $0. Try again." << endl; cin >> bet; } if (cash >= bet) cash = cash - bet; cout << "You bet: $" << bet << ". Your remaining balance is: $" << cash << "." << endl; player = player + cards[count]; if (cards[count] == 1 && player + 11 <= 21) { player = player + 10; ace++; } cout << "Your first card is: " << cards[count] << ". Your total is: " << player << ".\n"; cout << "Would you like to hit or stand? "; cin >> action; for (int i = 0; i < 1; i++) { if (count > 51) count = 0; while (action == "hit") { count++; if (cards[count] == 1 && player + 11 <= 21) { player = player + 10; ace++; } if (ace > 0 && player + cards[count] > 21) { player = player - 10; } player = player + cards[count]; if (player < 21) { cout << "Your next card is: " << cards[count] << ". Your total is : " << player << "." << endl; cout << "Would you like to hit or stand ? "; cin >> action; if (cards[count] == 1 && player + 11 <= 21) { player = player + 10; ace++; } if (ace > 0 && player + cards[count] > 21) { player = player - 10; ace--; } } if (player == 21) { cout << "Your next card is: " << cards[count] << ". Your total is : " << player << "." << endl; cout << "Congratulations! You won $" << bet * 2 << "!" << endl; cash = cash + bet * 2; break; } if (player > 21) { cout << "Your next card is: " << cards[count] << ". Your total is : " << player << "." << endl; cout << "Bust! You lost $" << bet << "!" << endl; break; } } if (action == "stand") { count++; cout << "\nDealer's turn!" << endl; dealer = dealer + cards[count]; if (cards[count] == 1 && dealer + 11 <= 21) { dealer = dealer + 10; dace++; } if (dace > 0 && dealer + cards[count] > 21) { dealer = dealer - 10; dace--; } cout << "Dealer's first card is: " << dealer << "." << endl; while (dealer < 17) { if (dealer <= player) { count++; if (cards[count] == 1 && dealer + 11 <= 21) { dealer = dealer + 10; dace++; } if (dace > 0 && dealer + cards[count] > 21) { dealer = dealer - 10; ace--; } dealer = dealer + cards[count]; cout << "Dealer's next card is: " << cards[count] << ". Their total is now: " << dealer << "." << endl; } if (dealer > player) break; } if (dealer > player && dealer > 21) { cout << "Player total: " << player << " Dealer total: " << dealer << endl; cout << "Dealer busts! Player wins $" << bet * 2 << "!" << endl; cash = cash + bet * 2; } if (dealer > player && dealer <= 21) { cout << "Player total: " << player << " Dealer total: " << dealer << endl; cout << "Dealer wins! You lost $" << bet << "." << endl; } if (dealer == player) { cout << "Player total: " << player << " Dealer total: " << dealer << endl; cout << "You tied! Your bet of $" << bet << " was returned." << endl; cash = cash + bet; } if (dealer < player) { cout << "Player total: " << player << " Dealer total: " << dealer << endl; cout << "Dealer loses! Player wins $" << bet * 2 << "!" << endl; cash = cash + bet * 2; } } player = 0; dealer = 0; bet = 0; ace = 0; dace = 0; if (count > 52) count = 0; cout << "\nYour total balance is currently: $" << cash << "." << endl; if (cash == 0) { cout << "\nThe casino wins, you have no more money left!\n"; return 0; } cout << "\n-----------------------------------------------------\n"; cout << "\nEnter 'Y' to play again! "; cin >> play; } }}
void createdeck(int cards[], int deck){ srand(time(0)); for (int i = 0; i < 52; i++) { for (int j = 0; j < 4; j++) { cards[i] = deck; i++; } i--; deck++; if (deck == 11) deck--; }if (play != 'Y' || play != 'y') cout << "\nYour season earnings were: $" << cash << ". Thanks for playing!\n";
}