#include #include #include "CPstring.h" #include "rando.h" #include "prompt.h" #include "wheel.h" // the bet struct represents a generic bet // in theory capably of representing any kind of bet, // e.g., odds/evens, red/black, single number etc. struct Bet { string description; int odds; Bet(const string & d, int o) : description(d), odds(0){} }; // this global list of bets is all the different kind of // bets there are. This could easily (and probably should be) // put into a Roulette class, where the class has a vector of // bets initialized in the constructor. This would make a good // class exercise. Bet betlist_g[] = { Bet("bet on black or red",1), Bet("bet on odd or even",1), Bet("bet on three consecutive numbers",11) }; // the number of bets, based on entries in betlist_g[] int numBets_g = sizeof(betlist_g)/sizeof(betlist_g[0]); class Game { public: Game(); // initialize a game bool Continue(); // returns true if game continues to play void Play(); // play the game once private: Wheel myWheel; int myMoney; bool myContinue; }; Game::Game() : myWheel(), myMoney(1000), myContinue(true) { // start with 1000 dollars in the bankroll } bool Game::Continue() { bool ok; char ch; cout << "continue? [y/n] "; cin >> ch; return 0 <= myMoney && tolower(ch) == 'y'; } void Game::Play() { int k; char betChar = '@'; // initialize to a garbage value int betNum = 38; // ditto, not valid bet int betIndex; // which bet is chosen int betAmount; // how much the wager is // prompt user for a bet for(k=0; k < numBets_g; k++) { cout << k << ") " << betlist_g[k].description << endl; } betIndex = PromptRange("choice: ",0,numBets_g-1); // distinguish bet type, get more info depending // on kind of bet if (betIndex == 0) { cout << "black or red [b/r] "; cin >> betChar; } else if (betIndex == 1) { cout << "odd or even [o/e] "; cin >> betChar; } else { cout << "one of three consecutive numbers" << endl; betNum = PromptRange("enter first number: ",0,34); } cout << endl; betAmount = PromptRange("how much to bet:",0,myMoney); cout << endl; // 5 spins (could be more) before 'real' number comes up for(k=0; k < 5; k++) { myWheel.spin(); } cout << endl << endl; int numSpun = myWheel.getNumber(); // process what appears on wheel based on kind of bet // made earlier if (betIndex == 0) { if ((myWheel.getColor() == Wheel::red && betChar == 'r') || (myWheel.getColor() == Wheel::black && betChar == 'b')) { myMoney += betAmount; } else { myMoney -= betAmount; } } else if (betIndex == 1) { if ((numSpun % 2 == 0 && betChar == 'e') || (numSpun % 2 == 1 && betChar == 'o')) { myMoney += betAmount; } else { myMoney -= betAmount; } } else { if (betNum <= numSpun && numSpun <= betNum+2) { myMoney += 11 * betAmount; } else { myMoney -= betAmount; } } cout << "bankroll = " << myMoney << endl; } int main(int argc, char * argv[]) { Game g; do { g.Play(); } while (g.Continue()); return 0; }