Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- int countDigits(int cash);
- int dealCard(int deck[][16], int& total, int shh, int& aces, int& width, int& size);
- string idCard(int value, int& width);
- string idSuit(int value, int& width);
- void generateDeck(int deck[][16], int& size);
- void playBlackjack(int& money, int& split, int& splitCard, int& bet);
- int main(){
- int cash = 100;
- int split = 0;
- string ans = "y";
- //Greeting
- cout << "Welcome to Black Jack!" << endl;
- //Keep playing
- while (ans == "y"&&cash > 0){
- int splitCard = 0;
- int bet = 0;
- playBlackjack(cash, split, splitCard, bet);
- if (cash > 0){
- cout << endl << "Replay? (Enter y/n)" << endl;
- cin >> ans;
- }
- split = 0;
- }
- if (cash > 0){
- cout << "You leave with $" << cash << "." << endl;
- cout << "Goodbye!" << endl;
- }
- else{
- cout << endl << "You are out of money. " << endl;
- }
- return 0;
- }
- int countDigits(int cash){
- if (cash > 9)
- return 1 + countDigits(cash / 10);
- else
- return 1;
- }
- int dealCard(int deck[][16], int& total, int shh, int& aces, int& width, int& size){
- int num;
- int suit;
- width = 0;
- do{
- num = rand() % 13;
- suit = rand() % 16;
- } while (deck[num][suit] == 0);
- if (shh == 0)
- cout << idCard(num + 1, width) << " of " << idSuit(deck[num][suit], width);
- deck[num][suit] = 0;
- if (num == 10 || num == 11 || num == 12)
- num = 9;
- if (num == 0){
- aces = aces + 1;
- if (total < 11)
- num = 10;
- }
- total = total + num + 1;
- size = size - 1;
- return num;
- }
- string idCard(int value, int& width){
- string card;
- //Switch statement that gives actual name to card values
- switch (value){
- case 1: card = "Ace";
- break;
- case 2: card = "Two";
- break;
- case 3: card = "Three";
- width = width + 2;
- break;
- case 4: card = "Four";
- width = width + 1;
- break;
- case 5: card = "Five";
- width = width + 1;
- break;
- case 6: card = "Six";
- break;
- case 7: card = "Seven";
- width = width + 2;
- break;
- case 8: card = "Eight";
- width = width + 2;
- break;
- case 9: card = "Nine";
- width = width + 1;
- break;
- case 10: card = "Ten";
- break;
- case 11: card = "Jack";
- width = width + 1;
- break;
- case 12: card = "Queen";
- width = width + 2;
- break;
- case 13: card = "King";
- width = width + 1;
- break;
- }
- return card;
- }
- string idSuit(int value, int& width){
- string suit;
- switch (value){
- case 1: suit = "Clubs";
- break;
- case 2: suit = "Diamonds";
- width = width + 3;
- break;
- case 3: suit = "Hearts";
- width = width + 1;
- break;
- case 4: suit = "Spades";
- width = width + 1;
- break;
- }
- return suit;
- }
- void generateDeck(int deck[][16], int& size){
- for (int i = 0; i < 13; i = i + 1){
- for (int j = 0; j < 16; j = j + 1)
- deck[i][j] = j / 4 + 1;
- }
- size = 208;
- }
- void playBlackjack(int& cash, int& split, int& splitCard, int& bet){
- int aces;
- int card1;
- int card2;
- int goof;
- int oppAces;
- int oppTotal;
- static int size = 0;
- int shh = 0;
- int text;
- int total;
- int width = 0;
- string move = "nb";
- static int deck[13][16];
- if (size <= 52)
- generateDeck(deck, size);
- if (splitCard == 0){
- cout << endl << ">How much would you like to bet? (You have $" << cash << ")" << endl;
- cin >> bet;
- while (bet<0 || bet>cash){
- cout << "(!) Invalid. Please enter a valid amount. " << endl;
- cin >> bet;
- }
- }
- cout << bet;
- aces = 0;
- oppAces = 0;
- oppTotal = 0;
- text = 0;
- total = 0;
- if (split == 0){
- cout << "You are dealt a card face up and one face down." << endl;
- cout << "You:" << setw(29) << " " << endl;
- cout << "Face Down: ";
- card1 = dealCard(deck, total, shh, aces, width, size);
- cout << setw(9 - width) << "*" << endl;
- }
- if (split == 1){
- cout << endl;
- total = splitCard + 1;
- }
- cout << "Face Up: ";
- card2 = dealCard(deck, total, shh, oppAces, width, size);
- cout << setw(11 - width) << " " << endl;
- if (total >= 10)
- width = 1;
- else
- width = 0;
- cout << "Total value: " << total << setw(18 - width) << " " << endl;
- cout << "Dealer:" << setw(26) << " " << endl;
- cout << "Face Up: ";
- goof = dealCard(deck, oppTotal, shh, oppAces, width, size);
- cout << setw(11 - width) << " " << endl;
- shh = 1;
- goof = dealCard(deck, oppTotal, shh, oppAces, width, size);
- shh = 0;
- if (total < 21){
- cout << endl;
- if (card1 == card2){
- cout << "(h)it, (s)tand, (d)ouble down, or split a (p)air?" << endl;
- cin >> move;
- if (move == "p"){
- split = 1;
- cout << endl;
- cout << " Splitting Pair " << endl;
- cout << " " << endl;
- cout << endl;
- cout << "Pair 1" << endl;
- playBlackjack(cash, split, card1, bet);
- cout << endl;
- cout << "Pair 2" << endl;
- playBlackjack(cash, split, card2, bet);
- }
- }
- else{
- cout << "(h)it, (s)tand, or (d)ouble down?" << endl;
- cin >> move;
- }
- }
- if (move == "d"){
- cout << endl;
- cout << " Double Down " << endl;
- cout << " " << endl;
- cout << "New Card: ";
- goof = dealCard(deck, total, shh, aces, width, size);
- cout << setw(8 - width) << "|" << endl;
- cout << " " << endl;
- }
- while (move != "s"&&move != "nb"&&move != "d"&&move != "p"&&total <= 21){
- while (move == "h"){
- cout << endl << " " << endl;
- cout << "New Card: ";
- goof = dealCard(deck, total, shh, aces, width, size);
- cout << setw(10 - width) << " ";
- if (total > 21 && aces > 0){
- total = total - 10;
- aces = aces - 1;
- }
- if (total >= 10)
- width = width + 1;
- cout << endl << "Total value: " << total << setw(17) << "*" << endl;
- cout << " " << endl;
- if (total <= 21){
- cout << endl;
- cout << "(h)it or (s)tand?" << endl;
- cin >> move;
- }
- else
- move = "s";
- }
- while (move != "h"&&move != "s"){
- cout << "Please enter either 'h' or 's')" << endl;
- cin >> move;
- }
- }
- if (move != "p"){
- if (total <= 21){
- while (oppTotal < 17){
- cout << endl << " " << endl;
- cout << "The Dealer's new card is: ";
- dealCard(deck, oppTotal, shh, oppAces, width, size);
- cout << setw(6 - width) << " ";
- width = 0;
- if (oppTotal >= 10)
- width = width + 1;
- cout << endl << "The Dealer's total is: " << oppTotal << setw(20 - width) << " ";
- text = 1;
- }
- }
- if (total <= 21 && text != 0)
- cout << endl << " " << endl;
- cout << endl;
- cout << " " << endl;
- if (move == "d"){
- if (oppTotal > 21)
- cash = cash + bet;
- else if (total > 21)
- cash = cash - bet;
- else if (total > oppTotal)
- cash = cash + bet;
- else if (total < oppTotal)
- cash = cash - bet;
- }
- if (total == 21 && move == "nb"){
- cash = cash*1.5;
- cout << " " << endl;
- cout << " Twenty-One, Black Jack! " << endl;
- cout << " " << endl;
- cout << "You have: $" << cash << "." << setw(13 - countDigits(cash)) << " " << endl;
- cout << " " << endl;
- }
- else if (total > 21){
- cash = cash - bet;
- cout << "You busted!" << setw(22) << "." << endl;
- cout << " " << endl;
- cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << "." << endl;
- }
- else if (oppTotal > 21){
- cash = cash + bet;
- cout << "I busted!" << setw(24) << " " << endl;
- cout << " " << endl;
- cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << " " << endl;
- }
- else if (total > oppTotal){
- cash = cash + bet;
- if (total >= 10)
- width = 1;
- else
- width = 0;
- cout << "Your total: " << total << setw(22 - width) << " " << endl;
- if (oppTotal >= 10)
- width = 1;
- else
- width = 0;
- cout << "Dealer's total: " << oppTotal << setw(18 - width) << " " << endl;
- cout << " " << endl;
- cout << "You won!" << setw(25) << " " << endl;
- cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << " " << endl;
- }
- else if (oppTotal > total){
- cash = cash - bet;
- if (total >= 10)
- width = 1;
- else
- width = 0;
- cout << "Your total: " << total << setw(22 - width) << " " << endl;
- if (oppTotal >= 10)
- width = 1;
- else
- width = 0;
- cout << "Dealer's total: " << oppTotal << setw(18 - width) << " " << endl;
- cout << " " << endl;
- cout << "I won!" << setw(27) << " " << endl;
- cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << " " << endl;
- }
- else if (move != "p"){
- if (total > 10)
- width = 1;
- else
- width = 0;
- cout << "Your total: " << total << setw(22 - width) << " " << endl;
- if (oppTotal > 10)
- width = 1;
- else
- width = 0;
- cout << "Dealer's total: " << oppTotal << setw(18 - width) << " " << endl;
- cout << " " << endl;
- cout << "Its a tie!" << setw(22) << " " << endl;
- cout << "Money returned. " << endl;
- }
- cout << " " << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment