This is my first program, ever. It comprises a choice of casino games:
- A number guessing game in which you guess a number 1 through ten. If you guess the correct number, you double the money you bet.
- Blackjack. Yes, I know the rules for this blackjack aren't exactly the same as they really are.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; double money = 100; int blackJack(); int numberguess(); void main2() { srand(static_cast<unsigned int>(time(0))); if (money == 0) { cout << "Sorry you hit 0 dollars \a"; return; } cout << "Hello! Welcome to my casino. \n" << "Which game would you like to play? \n" << "1. Guess the number \n" << "2. Blackjack \n"; while (true) { int choice; cin >> choice; switch (choice) { case 1: numberguess(); break; case 2: blackJack(); break; default: cout << "This is not a valid answer \n"; break; } } } double bet; int main() { main2(); return 0; } int numberguess() { while (true) { // Seed the random number generator int answerNumberGuess = (rand() % 10) + 1; cout << "You have " << money << " dollars." << endl; cout << "Now please enter your bet: " << endl; cin >> bet; while (true) { if (bet > money || bet <= 0) { cout << "Your too broke! Try again" << endl; cin >> bet; } else { break; } } cout << "now please enter your guess:" << endl; int guess; cin >> guess; money = money - bet; if (guess == answerNumberGuess) { cout << "Congradulations you win! the correct answerNumberGuess was " << answerNumberGuess << "!" << endl; money = money + bet * 2; cout << "You now have " << money << " dollars!" << endl; } else { char q; cout << "Sorry you lose" << endl << "Would you like to try again? [Y/N]"; cin >> q; if (q == 'y' || q == 'Y') { system("cls"); main2(); } else { return 1; } } if (money <= 0) { cout << "Wow, you lost all your money. \a" << endl; break; } } return 0; } int dHand = 0; int hasAce = 0; int pHand = 0; bool pStand = false; void checkStand(); void addDealerCard() { if (dHand > 21) { return; } int dCardDrawn = (rand() % 12) + 2; dHand += dCardDrawn; checkStand(); } void addPlayerCard() { int cardDrawn; cardDrawn = (rand() % 12) + 1; system("cls"); switch (cardDrawn) { case 8: case 9: case 10: case 11: cout << "You drew a face card worth ten! \n"; pHand += 10; break; case 12: cout << "You drew " << cardDrawn << " " << endl; cout << "You drew an ace! \n"; hasAce = 1; break; default: cout << "You drew " << cardDrawn + 1 << endl; pHand += cardDrawn + 1; break; } addDealerCard(); } void checkStand() { cout << "You're hand is " << pHand << endl; cout << "Would you like to hit[0] or stand[1]? \n"; cin >> pStand; if (pStand == true) { if (hasAce == true) { cout << "would you like your ace to be worth one[0] or eleven[1] \n"; bool aceValue; cin >> aceValue; if (aceValue == 1) { pHand += 11; } else if (aceValue == 0) { pHand += 1; } cout << "You're hand is now " << pHand << endl; } if (dHand > 21) { if (pHand > 21) { cout << "You lose! \n"; cout << "Dealers hand was " << dHand << endl; } if (pHand <= 21) { cout << "You win! \n"; money = money + (bet * 2); cout << "Dealers hand was " << dHand << endl; } } else if (dHand > pHand && dHand <= 21) { cout << "You lose! \n"; cout << "Dealers hand was " << dHand << endl; } else if (pHand > dHand && pHand <= 21) { cout << "You win! \n"; money = money + (bet * 2); cout << "Dealers hand was " << dHand << endl; } else if (pHand > dHand && pHand > 21) { cout << "You lose! \n"; cout << "Dealers hand was " << dHand << endl; } else if (dHand == pHand) { cout << "Its a push! \n"; money += bet; cout << "Dealers hand was " << dHand << endl; } cout << "Would you like to play another game? [Y/N] \n"; pHand = 0; dHand = 0; char question = 'A'; cin >> question; if (question == 'y' || question == 'Y') { system("cls"); main2(); } else { return; } } else { addPlayerCard(); } } int blackJack() { cout << "You have " << money << " dollars." << endl; cout << "Now please enter your bet: " << endl; cin >> bet; while (true) { if (bet > money || bet <= 0) { cout << "Your too broke! Try again" << endl; cin >> bet; } else { break; } } money = money - bet; addPlayerCard(); addDealerCard(); cout << "You're hand is now " << (pHand) << endl; checkStand(); return 0; } I was wondering if there were any ways I could make my program faster. Is there also a way I could simplify the if statements in the checkstand function?
main2won't stack overflow butmainwill? \$\endgroup\$mainwas just being used an example - any method that you call in this way will eventually result in an overflow due to the error in logic that Confettimaker points out. \$\endgroup\$