#include #include #include #include #include #include using std::istringstream; using std::cout; using std::cin; using std::endl; using std::string; using std::transform; using std::exception; bool Running; bool GameOver; int Wallet; int Cards[3] = {0,0,0}; bool IsAnInt(string input) { try { int output = atoi(input.c_str()); return true; } catch(const exception& e) { return false; } } string AskQuestion(string prompt) { while(true) { cout << prompt << "?" << endl; string ans; cin >> ans; transform(ans.begin(), ans.end(), ans.begin(), :: toupper); if(ans == "") cout << "PLEASE ANSWER THE QUESTION." << endl; else return ans; } } int GetIntInput(string prompt) { while(true) { cout << prompt << " "; string ans; cin >> ans; if(IsAnInt(ans)) return atoi(ans.c_str()); cout << "YOU DID NOT ENTER AN INTEGER." << endl; } } int AskForBet() { while(true) { int bet = GetIntInput("WHAT IS YOUR BET?"); if(bet < 0) cout << "PLEASE BET A NON-NEGATIVE AMOUNT." << endl; else if(bet > Wallet) { cout << "SORRY MY FRIEND, BUT YOU BET TOO MUCH." << endl; cout << "YOU HAVE ONLY " << Wallet << " DOLLARS TO BET." << endl; } else return bet; } } bool AskToPlayAgain() { while(true) { string ans = AskQuestion("DO YOU WANT TO PLAY AGAIN"); if(ans == "YES" || ans == "Y") return true; if(ans == "NO" || ans == "N") return false; cout << "PLEASE ANSWER \"YES\" OR \"NO\"" << endl; } } bool AskToSeeTheRules() { while(true) { string ans = AskQuestion("DO YOU WANT TO SEE THE RULES"); if(ans == "YES" || ans == "Y") return true; if(ans == "NO" || ans == "N") return false; cout << "PLEASE ANSWER \"YES\" OR \"NO\"" << endl; } } void DisplayCard(int card) { if(card == 11) cout << "JACK" << endl; else if(card == 12) cout << "QUEEN" << endl; else if(card == 13) cout << "KING" << endl; else if(card == 14) cout << "ACE" << endl; else cout << card << endl; } void DisplayRules() { cout << "ACEY DUCEY IS PLAYED IN THE FOLLOWING MANNER:" << endl; cout << "THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP" << endl; cout << "YOU HAVE THE OPTION TO BET OR NOT BET DEPENDING" << endl; cout << "ON WHETHER OR NOT YOU FEEL THE NEXT CARD WILL" << endl; cout << "HAVE A VALUE BETWEEN THE FIRST TWO." << endl; cout << " " << endl; cout << "IF YOU DO NOT WANT TO BET, INPUT A 0 (ZERO)." << endl; } int GenerateRandomNumber(int minimum, int maximum, bool show) { int random = (rand() % maximum) + minimum; if(show) cout << random << endl; return random; } int SelectCard() { while(true) { int card = GenerateRandomNumber(2, 13, false); if(card != Cards[0] && card != Cards[1] && card != Cards[2]) { DisplayCard(card); return card; } } } int main() { cout << "*** ACEY DUCEY : C\\C++ ***" << endl; Running = true; if(AskToSeeTheRules())DisplayRules(); while(Running) { Wallet = 100; GameOver = false; Cards[0] = Cards[1] = Cards[2] = 0; while(!GameOver) { cout << "YOU NOW HAVE " << Wallet << " DOLLARS." << endl; Cards[0] = SelectCard(); Cards[1] = SelectCard(); int bet = AskForBet(); if(bet > 0) { Cards[2] = SelectCard(); if((Cards[2] > Cards[0] && Cards[2] < Cards[1])||(Cards[2] > Cards[1] && Cards[2] < Cards[0])) { cout << "YOU WIN!!!" << endl; Wallet += bet; } else { cout << "SORRY, YOU LOSE." << endl; Wallet -= bet; } if(Wallet < 1) { cout << "SORRY FRIEND, YOU ARE BROKE." << endl; GameOver = true; } } else GameOver = true; } cout << "*** GAME OVER ***" << endl; Running = AskToPlayAgain(); } cout << "OK, HOPE YOU HAD FUN." << endl; cout << "THANKS FOR PLAYING!" << endl; }