// simplegame.cpp : Simple Blackjack game. // #include "stdafx.h" #include #include #include using namespace std; string const BUILD = "0001"; void println(string text, ...) { cout << text << endl; return; } void print(string text, ...) { cout << text; return; } string inttostr(int x) { std::stringstream out; out << x; return out.str(); } int strtoint(string x) { return atoi(x.c_str()); } int calcCards(int cards[21], int num) { int aces = 0; int total = 0; int maxTotal = 0; int aceAmount = 0; for(int x=0;x < num;x++) { if(cards[x] == 11) { aces++; } else { total+= cards[x]; } } if(total + aces > 21) { println("You're bust!"); return 0; } if(total + aces == 21) { println("Blackjack!"); return 2; } maxTotal = total; for(int x=0;x < aces;x++) { if(maxTotal + 10 + aces - x > 21){ maxTotal++; } else { maxTotal=+ 11; aceAmount++; } } if(maxTotal == 21) { println("Blackjack!"); return 2; } else { println("You have " + inttostr(maxTotal) + " (or " + inttostr(maxTotal - aceAmount * 10) + ")"); return -1; } } int aiCards(int cards[21], int num, bool show) { int aces = 0; int total = 0; int maxTotal = 0; int aceAmount = 0; for(int x=0;x < num;x++) { if(cards[x] == 11) { aces++; } else { total+= cards[x]; } } if(total + aces > 21) { if(show) println("Dealer Bust!"); return 0; } if(total + aces == 21) { if(show) println("Dealer Wins!"); return -2; } maxTotal = total; for(int x=0;x < aces;x++) { if(maxTotal + 10 + aces - x > 21){ maxTotal++; } else { maxTotal=+ 11; aceAmount++; } } if(maxTotal == 21) { if(show) println("Dealer Wins!"); return -2; } else { return maxTotal; } } int _tmain(int argc, _TCHAR* argv[]) { bool run = true; int cash = 200; int bet = 0; bool hit = true; bool failure = false; int cards[2][21]; int cardLoc[2]; string in; //cout << "GBlackjack (Build " << BUILD << ")"; println("GBlackjack 1.0 (Build " + BUILD + ")"); println(""); while(run) { println(""); if(cash < 1) { println("Getting Saved! +$50"); cash = 50; } bet = 0; // Display Cash println("Your amount: $" + inttostr(cash)); // Enter Bet while (bet == 0) { print("Enter your bet: $"); getline(cin, in); bet = strtoint(in); if(bet > cash) { println("You don't have that kind of money!"); bet = 0; } else if(bet == 0) { println("You can't bet nothing!"); } } // Calc Dealer & Player cards[0][0] = rand() % 9 + 2; cards[0][1] = rand() % 9 + 2; cardLoc[0] = 2; cards[1][0] = rand() % 9 + 2; cards[1][1] = rand() % 9 + 2; cardLoc[1] = 2; calcCards(cards[0],cardLoc[0]); // Hits hit = true; failure = false; while(hit) { println("Hit? (y/n)"); getline(cin, in); if(in.find("y") == 0 || in.find("Y") == 0) { cards[0][cardLoc[0]] = rand() % 9 + 2; cardLoc[0]++; switch(calcCards(cards[0],cardLoc[0])) { case 0: hit = false; failure = true; cash = cash - bet; println("-$"+ inttostr(bet)); break; case 1: hit = false; failure = true; cash = cash + bet; println("+$"+ inttostr(bet)); break; case 2: hit = false; failure = true; cash = cash + bet*2; println("+$"+ inttostr(bet*2)); break; } } else if(in.find("n") == 0 || in.find("N") == 0) { hit = false; } } // Finish if(!failure) { hit = true; while(hit) { if(aiCards(cards[0],cardLoc[0],false) > aiCards(cards[1],cardLoc[1],false)) { cards[0][cardLoc[0]] = rand() % 9 + 2; cardLoc[0]++; switch(aiCards(cards[0],cardLoc[0],true)) { case 0: hit = false; failure = true; cash = cash + bet; println("+$"+ inttostr(bet)); break; case -1: hit = false; failure = true; cash = cash - bet; println("-$"+ inttostr(bet)); break; case -2: hit = false; failure = true; cash = cash - bet; println("-$"+ inttostr(bet)); break; } } else { hit = false; } } } if(!failure) { if(aiCards(cards[0],cardLoc[0],false) > aiCards(cards[1],cardLoc[1],false)) { println("Player Win!"); cash = cash + bet; println("+$"+ inttostr(bet)); } if(aiCards(cards[0],cardLoc[0],false) < aiCards(cards[1],cardLoc[1],false)) { println("Dealer Win!"); cash = cash - bet; println("-$"+ inttostr(bet)); } if(aiCards(cards[0],cardLoc[0],false) == aiCards(cards[1],cardLoc[1],false)) println("Tie!"); } // Display Result println("You now have $" + inttostr(cash)); // Play Again? println("Try again? (y/n)"); getline(cin, in); if(in.find("n") == 0 || in.find("N") == 0) { run = false; } } return 0; }