Created
December 31, 2016 02:12
-
-
Save marshareb/700f6232e1e4e1b1c77a50db28261782 to your computer and use it in GitHub Desktop.
Using OOP to make a basic blackjack program. Some imporvements might be to actually account for a real deck of cards. I also don't know the rules of blackjack super well, so this is probably flawed in some way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Blackjack | |
#include <iostream> | |
#include <cstdlib> | |
using namespace std; | |
#define RESET "\033[0m" | |
#define BLACK "\033[30m" /* Black */ | |
#define RED "\033[31m" /* Red */ | |
#define GREEN "\033[32m" /* Green */ | |
#define YELLOW "\033[33m" /* Yellow */ | |
#define BLUE "\033[34m" /* Blue */ | |
#define MAGENTA "\033[35m" /* Magenta */ | |
#define CYAN "\033[36m" /* Cyan */ | |
#define WHITE "\033[37m" /* White */ | |
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ | |
#define BOLDRED "\033[1m\033[31m" /* Bold Red */ | |
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ | |
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ | |
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ | |
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ | |
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ | |
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ | |
bool win; | |
class Player{ | |
public: | |
string name; | |
int money; | |
}; | |
int dealcard(){ | |
int num = rand() % 13 + 1; | |
return num; | |
} | |
void game(){ | |
int dealamount; | |
int handamount; | |
bool hit = true; | |
char check; | |
cout << "Dealing two cards..." << endl; | |
handamount = dealcard(); | |
handamount += dealcard(); | |
cout << "Dealing dealers card..." << endl; | |
dealamount = dealcard(); | |
cout << "Your face up card value is: " << BOLDRED << handamount << RESET << endl; | |
cout << "The dealers card is: " << BOLDBLUE << dealamount << RESET << endl; | |
while (handamount < 21){ | |
cout << "Would you like to hit?" << endl; | |
cin >> check; | |
if (check == 'n' | check == 'N'){ | |
break; | |
} | |
else if (check == 'y' | check == 'Y'){ | |
handamount += dealcard(); | |
cout << "You are now up to " << BOLDRED << handamount << RESET << endl; | |
} | |
} | |
if (handamount > 21){ | |
cout << "You are over 21." << endl; | |
win = false; | |
return; | |
} | |
while (dealamount < 17){ | |
dealamount += dealcard(); | |
cout << "The dealer is now up to " << BOLDBLUE << dealamount << RESET << endl; | |
if (dealamount > 21){ | |
cout << "You win!" << endl; | |
win = true; | |
return; | |
} | |
} | |
if (dealamount >= handamount){ | |
cout << "The dealer beat you." << endl; | |
win = false; | |
} | |
else{ | |
cout << "You beat the dealer!" << endl; | |
win = true; | |
} | |
} | |
void blackjack(){ | |
cout << BOLDBLUE << "888 888 888 d8b 888 " << endl; | |
cout << "888 888 888 Y8P 888 " << endl; | |
cout << "888 888 888 888 " << endl; | |
cout << "88888b. 888 8888b. .d8888b888 888 8888 8888b. .d8888b888 888 " << endl; | |
cout << "888 88b888 88bd88P 888 .88P 888 88bd88P 888 .88P " << endl; | |
cout << "888 888888.d888888888 888888K 888.d888888888 888888K " << endl; | |
cout << "888 d88P888888 888Y88b. 888 88b 888888 888Y88b. 888 88b " << endl; | |
cout << "88888P 888 Y888888 Y8888P888 888 888 Y888888 Y8888P888 888 " << endl; | |
cout << " 888 " << endl; | |
cout << " d88P " << endl; | |
cout << " 888P " << RESET <<endl; | |
} | |
bool checkGameState(Player p1){ | |
if (p1.money <= 0){ | |
return false; | |
} | |
else{ | |
return true; | |
} | |
} | |
int main(){ | |
Player p1; | |
p1.money = 200; | |
int bet; | |
bool cont = true; | |
char check; | |
cout << BOLDBLUE << "Welcome to" << RESET << endl; | |
cout << endl; | |
blackjack(); | |
cout << "What is your name?" << endl; | |
cin >> p1.name; | |
cout << "Hi " + p1.name + ". You are starting off with " << p1.money << " dollars." << endl; | |
while (cont == true){ | |
cout << "How much would you like to bet?" << endl; | |
cin >> bet; | |
while (p1.money - bet < 0){ | |
cout << "You've bet too much. Please try again." << endl; | |
cin >> bet; | |
} | |
p1.money = p1.money - bet; | |
game(); | |
if (win == true){ | |
p1.money = p1.money + 2 * bet; | |
} | |
if (checkGameState(p1) == false){ | |
cout << "You are out of money. The game is over." << endl; | |
return 0; | |
} | |
else{ | |
cout << "You now have " << p1.money << " dollars." << endl; | |
cout << "Would you like to play again?" << endl; | |
cin >> check; | |
if (check == 'y' | check == 'Y'){ | |
cont = true; | |
} | |
else{ | |
cont = false; | |
} | |
} | |
} | |
cout << "Thanks for playing!" << endl; | |
cout << "You're leaving the casino with " << p1.money << " dollars." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment