Projects

Roulette

  1#include <iostream>
  2#include <cstdlib>
  3#include <ctime>
  4#include <random>
  5#include <string>
  6
  7using namespace std;
  8
  9// Generate random number (Roulette spin)
 10int generatenum() {
 11    static random_device rd;
 12    static mt19937 gen(rd());
 13    static uniform_int_distribution<int> dist(0,37);
 14    return dist(gen);
 15}
 16
 17// Figures out color each round based on number
 18string getcolor(int number) {
 19    if (number == 0 || number == 37)
 20        return "Green";
 21
 22    static string colors[38] = {
 23        "Green", "Red", "Black", "Red", "Black", "Red", "Black",
 24        "Red", "Black", "Red", "Black", "Black", "Red", "Black",
 25        "Red", "Black", "Red", "Black", "Red", "Red", "Black",
 26        "Red", "Black", "Red", "Black", "Red", "Black", "Red",
 27        "Black", "Black", "Red", "Black", "Red", "Black", "Red",
 28        "Black", "Red"
 29    };
 30    return colors[number];
 31}
 32
 33// Display betting options with payout ratios
 34void instructions() {
 35    cout << "================= ROULETTE BETTING OPTIONS =================" << endl;
 36    cout << "1. Red 1:1      - Bet that the number will be red" << endl;
 37    cout << "2. Black 1:1    - Bet that the number will be black" << endl;
 38    cout << "3. Odd 1:1      - Bet that the number will be odd" << endl;
 39    cout << "4. Even 1:1     - Bet that the number will be even" << endl;
 40    cout << "5. Low 1:1      - Bet that the number will be 1 to 18" << endl;
 41    cout << "6. High 1:1     - Bet that the number will be 19 to 36" << endl;
 42    cout << "7. Dozen 1 2:1  - Bet that the number will be 1 to 12" << endl;
 43    cout << "8. Dozen 2 2:1  - Bet that the number will be 13 to 24" << endl;
 44    cout << "9. Dozen 3 2:1  - Bet that the number will be 25 to 36" << endl;
 45    cout << "10. Column 1 2:1- Bet that the number will be in 1st column" << endl;
 46    cout << "11. Column 2 2:1- Bet that the number will be in 2nd column" << endl;
 47    cout << "12. Column 3 2:1- Bet that the number will be in 3rd column" << endl;
 48    cout << "13. Green 17:1  - Bet that the number will be 0 or 00" << endl;
 49    cout << "============================================================" << endl;
 50    cout << "Enter the number corresponding to your bet type." << endl;
 51}
 52
 53// Bet result class
 54class BetResult {
 55public:
 56    bool win;
 57    int payout;
 58};
 59
 60// Check if user won
 61BetResult result(int choice,int spin,const string &color) {
 62    BetResult r;
 63    if(choice == 1){ r.win = (color=="Red"); r.payout=1; }
 64    else if(choice == 2){ r.win = (color=="Black"); r.payout=1; }
 65    else if(choice == 3){ r.win = (spin!=0 && spin!=37 && spin%2==1); r.payout=1; }
 66    else if(choice == 4){ r.win = (spin!=0 && spin!=37 && spin%2==0); r.payout=1; }
 67    else if(choice == 5){ r.win = (spin>=1 && spin<=18); r.payout=1; }
 68    else if(choice == 6){ r.win = (spin>=19 && spin<=36); r.payout=1; }
 69    else if(choice == 7){ r.win = (spin>=1 && spin<=12); r.payout=2; }
 70    else if(choice == 8){ r.win = (spin>=13 && spin<=24); r.payout=2; }
 71    else if(choice == 9){ r.win = (spin>=25 && spin<=36); r.payout=2; }
 72    else if(choice == 10){ r.win = (spin%3==1); r.payout=2; }
 73    else if(choice == 11){ r.win = (spin%3==2); r.payout=2; }
 74    else if(choice == 12){ r.win = (spin%3==0); r.payout=2; }
 75    else if(choice == 13){ r.win = (spin==0 || spin==37); r.payout=17; }
 76    return r;
 77}
 78
 79int main() {
 80    bool play = true;
 81    int resultnum, choice, bet, total;
 82    char again;
 83    int z=0;
 84
 85    cout << "How much money will you be playing with? (# only, no $): ";
 86    cin >> total;
 87
 88    while(play) {
 89        instructions();
 90        cin >> choice;
 91
 92        while(z<1) {
 93            cout << "\nHow much money would you like to bet? (ex:50)\nBet: ";
 94            cin >> bet;
 95            if(bet>total){ cout<<"Not enough money, enter different amount."<<endl; }
 96            else{ z=1; }
 97        }
 98
 99        resultnum = generatenum();
100        if(resultnum==37) cout<<"00";
101        else cout<<resultnum;
102
103        string color = getcolor(resultnum);
104        cout<<" "<<color<<endl;
105
106        BetResult r = result(choice,resultnum,color);
107        int winamount = bet*r.payout;
108        bool win = r.win;
109
110        if(win){ cout<<"You Win!"<<endl; total+=winamount; cout<<"Your total is: $"<<total<<endl; }
111        else{ cout<<"You lose"<<endl; total-=bet; cout<<"Your total is: $"<<total<<endl; }
112
113        z=0;
114
115        if(total>0){
116            cout<<"play again?(y/n): ";
117            cin>>again;
118            play = (again=='y');
119        } else { cout<<"No more money. Game over"<<endl; play=false; }
120    }
121    return 0;
122}