0

I have to write a game application that simulates a roulette table. The roulette table has 36 numbers (1 to 36) that are arranged in three columns of 12 rows. The first row has the numbers 1 through 3, the second row contains 4 through 6, and so on. There is also a number 0 that is outside the table of numbers. The numbers in the table are colored red and black (0 is green). The red numbers are 1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,… 36. The other half of the numbers are black. In a simplified set of rules, players can bet on an individual number(including 0), the red numbers, the black numbers, the even numbers, the odd numbers, the numbers 1 to 18, the numbers 19 to 36, and any of the columns or rows in the table.The users should be allowed to enter one of the bets, and the application uses the rand function from <cstdlib> as the basis for computing the number that would be rolled on the wheel. It then compares this number to the bet, and reports whether it won or lost.

I'm trying to get start on writing this code but I really don't understand the game of roulette and how it work. And I don't know what or how many methods I need for my constructor class. If anybody know this game pretty well please explain the rules to me. thanks

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Clinton Portis
-2
#include<windows.h>
#include<iostream>
#include<cctype>
#include<cstdlib>
#include<ctime>

using namespace std;

class Roulette
{
   public:
             
      Roulette();      
      
      void board_display();
      void prompt();
      void bet_menu();
      void place_bet();
      void set_bet(int choice);
      void set_bet(int menu_choice, int number, int amount); 
      void set_chip(int);
      void set_chip(int, int);          
      void spin_the_wheel();     
      void win_or_lose();
      void reset();
      bool quit();
      void gotoxy(int x, int y);      
        
      bool win_straight_up();   //35:1 + bet back
      bool win_column();        // 2:1 + bet back
      bool win_low();           // 1:1 + bet back
      bool win_high();          // 1:1 + bet back
      bool win_even();          // 1:1
      bool win_odd();           // 1:1
      bool win_red();           // 1:1
      bool win_black();         // 1:1
      bool is_black(int); 
      
      void input_error();
      void bet_error();
      void domain_error();
      void cap_error();
      
   private:
              
      bool is_bet,
           is_quit;      
      
      int  total,
           available,
           winnings,
           winner,
           bet_array_side[9],
           bet_array_number[36],
           zero_coords_x[2],
           zero_coords_y[2],           
           side_coords_x[6],
           side_coords_y[6],
           num_coords_x[36],
           num_coords_y[36],
           col_coords_x[3],
           col_coords_y[3];         
              
};

int main()
{
    Roulette myRoulette;
    
    myRoulette.board_display();
    
    do{          
          myRoulette.prompt();             
    
    }while(!myRoulette.quit());
    
return 0;
}


//////////////////////////////////////////////
///////////////////// Function Definitions //
////////////////////////////////////////////

Roulette::Roulette()
{
   is_bet  = FALSE;
   is_quit = FALSE;
   total  = 2500;
   available = 200;
   winnings = 0;
   
   srand((unsigned)time(NULL));  
   
   for(int i=0; i<9; i++)
   
      bet_array_side[i] = 0;
      
   for(int i=0; i<36; i++)
   
      bet_array_number[i] = 0; 
   
   for(int i=0, x=8; i<2; i++, x+=15)
   {
      zero_coords_x[i] = x;
      zero_coords_y[i] = 6;
   }      
   
   for(int i=0, y=13; i<6; i++, y+=8)
   {
      side_coords_x[i] = 35;
      side_coords_y[i] = y;
   }
   
   for(int i=0, x=5, y=10; i<36; i++, x+=10)
   {
       if(x>26)
       {               
            x = 5;
            y+= 4;
       }
       
      num_coords_x[i] = x;
      num_coords_y[i] = y;
   }
   
   for(int i=0, x=8; i<3; i++, x+=10)
   {
      col_coords_x[i] = x;
      col_coords_y[i] = 56; 
   }   
}   
   
void Roulette::board_display()
{
   cout << " _____________________________ "
        << "\n|      ___     |   ___   ___  |"  
        << "\n|     |   |    |  |   | |   | |"
        << "\n|     |   |    |  |   | |   | |"
        << "\n|     |___|    |  |___| |___| |"
        << "\n|              |              |"        
        << "\n|______________|______________|_________ "
        << "\n|         |         |         |         |"  
        << "\n|    1    |    2    |    3    |  *LOW*  |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|  1 - 18 |"
        << "\n|         |         |         |         |            ________________"  
        << "\n|    4    |    5    |    6    |         |           /   The Wheel.   \\"
        << "\n|         |         |         |         |          /  ______________  \\" 
        << "\n|_________|_________|_________|_________|         |  |              |  |"
        << "\n|         |         |         |         |         |  |              |  |"  
        << "\n|    7    |    8    |    9    | *EVEN*  |         |  |______________|  |"
        << "\n|         |         |         |         |         |   ______________   |" 
        << "\n|_________|_________|_________|         |         |  |              |  |"
        << "\n|         |         |         |         |         |  |              |  |"  
        << "\n|    10   |    11   |    12   |         |         |  |______________|  |"
        << "\n|         |         |         |         |         |                    |"  
        << "\n|_________|_________|_________|_________|          \\   Total $        /"
        << "\n|         |         |         |         |           \\________________/"  
        << "\n|    13   |    14   |    15   |  *RED*  |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|         |"
        << "\n|         |         |         |         |"  
        << "\n|    16   |    17   |    18   |         |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|_________|"
        << "\n|         |         |         |         |"  
        << "\n|    19   |    20   |    21   | *BLACK* |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|         |"
        << "\n|         |         |         |         |"  
        << "\n|    22   |    23   |    24   |         |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|_________|"
        << "\n|         |         |         |         |"  
        << "\n|    25   |    26   |    27   |  *ODD*  |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|         |"
        << "\n|         |         |         |         |"  
        << "\n|    28   |    29   |    30   |         |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|_________|"
        << "\n|         |         |         |         |"  
        << "\n|    31   |    32   |    33   | *HIGH*  |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________| 19 - 36 |"
        << "\n|         |         |         |         |"  
        << "\n|    34   |    35   |    36   |         |"
        << "\n|         |         |         |         |" 
        << "\n|_________|_________|_________|_________|"
        << "\n|   2:1   |   2:1   |   2:1   |"
        << "\n|_________|_________|_________|";
        
   gotoxy(62,22);  cout << total;          
}

void Roulette::prompt()
{   
     int menu_choice=0;
    
      gotoxy(50,35); cout << "      Main Menu       ";
      gotoxy(50,36); cout << "--------------------- ";
      gotoxy(50,38); cout << "1. Place Bet          ";
      gotoxy(50,39); cout << "2. Spin the wheel     ";         
      gotoxy(50,40); cout << "3. Quit               ";
      gotoxy(50,41); cout << "                      ";
      gotoxy(50,42); cout << "                      ";
      gotoxy(50,43); cout << "                      ";
      gotoxy(50,44); cout << "                      ";
      gotoxy(50,45); cout << "                      ";
      gotoxy(50,46); cout << "                      ";
      gotoxy(50,47); cout << "                      ";
      gotoxy(50,48); cout << "                      ";
      gotoxy(50,49); cout << "                      ";
      gotoxy(50,50); cout << "                      ";
      gotoxy(50,51); cout << "                      ";
      gotoxy(50,52); cout << "                            ";
      gotoxy(50,53); cout << "                            ";
      gotoxy(50,54); cout << "                            ";
      gotoxy(50,55); cout << "                            ";
      gotoxy(50,56); cout << "                            ";
      
        
      gotoxy(50,42); cout << "Enter ye' choice: ";
      cin >> menu_choice;      
      
      switch(menu_choice)
      {
         case 1:  place_bet();                           break;
         case 2:  is_bet?spin_the_wheel():bet_error();   break;
         case 3:  is_quit=TRUE;quit();                   break;
        default:  input_error();                         break;
      }            
}

void Roulette::place_bet()
{
    int menu_choice=0,
        number=0,
        bet_amt=0; 
       
    gotoxy(54,26); cout << "               ";
    gotoxy(62,10); cout << "                ";     
    gotoxy(50,39); cout << "1.  Double Zero      ";
    gotoxy(50,35); cout << "      Bet Menu       ";
    gotoxy(50,36); cout << "---------------------";
    gotoxy(50,38); cout << "0.  Zero             ";
    gotoxy(50,40); cout << "2.  Straight-up      ";
    gotoxy(50,41); cout << "3.  Left Column      ";
    gotoxy(50,42); cout << "4.  Middle Column    ";
    gotoxy(50,43); cout << "5.  Right Column     ";
    gotoxy(50,44); cout << "6.  Low  Numbers     ";
    gotoxy(50,45); cout << "7.  High Numbers     ";
    gotoxy(50,46); cout << "8.  Evens            ";
    gotoxy(50,47); cout << "9.  Odds             ";
    gotoxy(50,48); cout << "10. Red              ";
    gotoxy(50,49); cout << "11. Black            ";
    
    
    gotoxy(50,51); cout << "Enter ye' choice: ";
    cin >> menu_choice; 
    
    if(menu_choice < 0 || menu_choice > 11)
    
       domain_error();   
    
    if(menu_choice == 2)      
       
       set_bet(menu_choice, number, bet_amt);    
    
    else
    
       set_bet(menu_choice);
}

void Roulette::set_bet(int choice)
{
    int bet_amt=0;
    
     cin.ignore();
     gotoxy(50,53); cout << "$200 Limit Per Turn";       
     gotoxy(50,54); cout << '$' << available << " available to bet.";
     gotoxy(50,56); cout << "Enter Amount: ";
     gotoxy(64,56); cin >> bet_amt;
     
     if(bet_amt > available)
     
        cap_error();
        
     total -= bet_amt; 
     available -= bet_amt;    
     bet_array_side[choice-3] = bet_amt;
     
     gotoxy(62,22);  cout << "      ";
     gotoxy(62,22);  cout << total;     
     
     is_bet = TRUE;
     set_chip(choice);
}

void Roulette::set_bet(int choice, int number, int bet_amt) 
{    
       gotoxy(50,51); cout << "Enter a number 1-36: ";
       
       cin >> number;
       
       if(number < 1 || number > 36) 
       
          domain_error();    
       
       cin.ignore();
       gotoxy(50,53); cout << "$200 Limit Per Turn";       
       gotoxy(50,54); cout << '$' << available << " available to bet.";
       gotoxy(50,56); cout << "Enter Amount: ";
       gotoxy(64,56); cin >> bet_amt;
     
     if(bet_amt > available)
     
        cap_error();
        
     total -= bet_amt;   
     available -= bet_amt;
     bet_array_number[number-1] = bet_amt;
     
     gotoxy(62,22);  cout << "      ";
     gotoxy(62,22);  cout << total; 
     
     is_bet = TRUE;
     set_chip(choice, number);     
}    

void Roulette::set_chip(int pos)
{  
   char chip = 1;

      switch(pos)
      {
         case 0:  gotoxy(zero_coords_x[0], zero_coords_y[0]);  cout << chip;            
                  break; 
         case 1:  gotoxy(zero_coords_x[1], zero_coords_y[1]);  cout << chip;
                  break;
         case 3:  gotoxy(col_coords_x[0], col_coords_y[0]);    cout << chip;
                  break;
         case 4:  gotoxy(col_coords_x[1], col_coords_y[1]);    cout << chip;
                  break;
         case 5:  gotoxy(col_coords_x[2], col_coords_y[2]);    cout << chip;
                  break;
         case 6:  gotoxy(side_coords_x[0], side_coords_y[0]);  cout << chip;
                  break;
         case 7:  gotoxy(side_coords_x[5], side_coords_y[5]);  cout << chip;
                  break;
         case 8:  gotoxy(side_coords_x[1], side_coords_y[1]);  cout << chip;
                  break; 
         case 9:  gotoxy(side_coords_x[4], side_coords_y[4]);  cout << chip;
                  break;
         case 10: gotoxy(side_coords_x[2], side_coords_y[2]);  cout << chip;
                  break; 
         case 11: gotoxy(side_coords_x[3], side_coords_y[3]);  cout << chip;
                  break;
      }
      
      prompt();
}

void Roulette::set_chip(int pos, int number)
{
   char chip = 1;
   
   gotoxy(num_coords_x[number-1], num_coords_y[number-1]);
   cout << chip;
   
   prompt();
}

void Roulette::spin_the_wheel()
{   
    int turn=0;
    int snooze=75;
    int target=0; 
    gotoxy(56,15); cout << "   ZERO    ";
    gotoxy(60,19); cout << "0 ";
          
    Sleep(snooze);
          
    gotoxy(56,15); cout << "DOUBLE ZERO";
    gotoxy(60,19); cout << "00";
          
    Sleep(snooze);      
          

    for(int i=1; i<37; i++)  
    {
       if(is_black(i))  
       {
          gotoxy(56,15); cout << "   BLACK   ";
          gotoxy(60,19); cout << "  ";    
          gotoxy(60,19); cout << i;                     
       }   
       else 
       {           
          gotoxy(56,15); cout << "   RED     ";
          gotoxy(60,19); cout << "  ";    
          gotoxy(60,19); cout << i;
       }            
                        
       Sleep(snooze);
    } 
           
    //Random Number
    target = rand()%38+1;    
    
    if(is_black(target))
    {
       gotoxy(56,15); cout << "   BLACK   "; 
       gotoxy(60,19); cout << "  ";    
       gotoxy(60,19); cout << target;   
    }   
    else 
    {
       gotoxy(56,15); cout << "   RED     ";
       gotoxy(60,19); cout << "  ";    
       gotoxy(60,19); cout << target;
    }
    if(target == 37)
    {
       gotoxy(56,15); cout << "   ZERO    ";
       gotoxy(60,19); cout << "0 ";
    }
    else if(target == 38)
    {            
       gotoxy(56,15); cout << "DOUBLE ZERO";
       gotoxy(60,19); cout << "00"; 
    }
            
    winner = target;
    win_or_lose();        
} 

void Roulette::win_or_lose()
{    
     int snooze = 2000;
         
     if(is_quit)
     
        return;
         
     win_straight_up();   //35:1 + bet back
     win_column();        // 2:1 + bet back
     win_low();           // 1:1 + bet back
     win_high();          // 1:1 + bet back
     win_even();          // 1:1
     win_odd();           // 1:1
     win_red();           // 1:1
     win_black();
     
         if(winnings)
         {    
              gotoxy(54,26); cout << "                ";   
              gotoxy(54,26); cout << "You WIN: $" << winnings;              
         }
         else
         {
              gotoxy(54,26); cout << "                ";  
              gotoxy(56,26); cout << "No Wins.";              
         }
         
         total += winnings;
         gotoxy(62,22);  cout << "      ";
         gotoxy(62,22);  cout << total;  
         Sleep(snooze);
         reset();          
}

bool Roulette::win_straight_up()
{
     
     if(!bet_array_number[winner-1])
     
        return FALSE;
     
     else
     {
        winnings += bet_array_number[winner-1] * 35 + bet_array_number[winner-1];
        return TRUE;
     }  
}

bool Roulette::win_column()
{         
     if(!bet_array_side[0] && !bet_array_side[1] && !bet_array_side[2])
     
        return FALSE;
        
      for(int i=0, j=1; i<3; i++, j++)
      {  
        if(bet_array_side[i])
        {
           for(int k=j; k<37; k+=3)
           {
              if(winner == k)
              {
                  winnings += bet_array_side[i] * 2 + bet_array_side[i];
                  return TRUE;
              }
           }
        }        
      }
      return FALSE;
}  

bool Roulette::win_low()
{
     if(!bet_array_side[3])
     
        return FALSE;
        
     else if(winner > 0 && winner < 19)
     {
        winnings += bet_array_side[3] + bet_array_side[3];
        return TRUE;
     }
     else
     
        return FALSE;
}

bool Roulette::win_high()
{
     if(!bet_array_side[4])
     
        return FALSE;
        
     else if(winner > 18)
     {
        winnings += bet_array_side[4] + bet_array_side[4];
        return TRUE;
     }
     else
     
        return FALSE;
}

bool Roulette::win_even()
{
     if(!bet_array_side[5])
     
        return FALSE;
        
     else if(winner%2+1)
     {
        winnings += bet_array_side[5];
        return TRUE;
     }
     else 
     
        return FALSE;
}

bool Roulette::win_odd()
{
     if(!bet_array_side[6])
     
        return FALSE;
        
     else if(winner%2)
     {
        winnings += bet_array_side[6];
        return TRUE;
     }
     else 
     
        return FALSE;
}

bool Roulette::win_red()
{
     if(!bet_array_side[7])
     
        return FALSE;
     
     else if(!is_black(winner))
     {
        winnings += bet_array_side[7];
        return TRUE;
     }
     else 
     
        return FALSE;
}

bool Roulette::win_black()
{
     if(!bet_array_side[8])
     
        return FALSE;
     
     else if(is_black(winner))
     {
        winnings += bet_array_side[8];
        return TRUE;
     }
     else 
     
        return FALSE;
}           

bool Roulette::is_black(int number)
{
     if(number<11 && !(number%2) || number>10 && number<18 && number%2 || 
        number>19 && number<29 && !(number%2) || number>28 && number<36 && number%2)
     
        return true;
        
     else
     
        return false;
}    

void Roulette::reset()
{
   winnings  = 0;
   available = 200; 
   is_bet  = FALSE; 
   
   for(int i=0; i<9; i++)
   
      bet_array_side[i] = 0;
      
   for(int i=0; i<36; i++)
   
      bet_array_number[i] = 0; 
      
   for(int i=0; i<2; i++)
   { 
      gotoxy(zero_coords_x[i], zero_coords_y[i]); cout << '_';
   } 
   for(int i=0; i<6; i++)
   {
      gotoxy(side_coords_x[i], side_coords_y[i]);  cout << ' ';
   }
   for(int i=0; i<36; i++)
   {
      gotoxy(num_coords_x[i], num_coords_y[i]);   cout << '_';
   }
   for(int i=0; i<3; i++)
   {
      gotoxy(col_coords_x[i], col_coords_y[i]);   cout << '_';
   }
   
    gotoxy(54,26); cout << "              ";
}

bool Roulette::quit()
{
     return is_quit;
}                
          
void Roulette::gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}  

void Roulette::input_error()
{
      gotoxy(50,44); cout << "\aInvalid Entry! ";      
      Sleep(1500);
      prompt();
}  

void Roulette::cap_error()
{     
     gotoxy(50,56); cout << "\aBet amount exceeds $200 limit! ";
     Sleep(1500);
     gotoxy(50,51); cout << "                     ";
     gotoxy(50,53); cout << "                                 ";
     gotoxy(50,54); cout << "                                 ";
     gotoxy(50,56); cout << "                                 ";    
     
     
     place_bet();
}

void Roulette::domain_error()
{
      gotoxy(50,53); cout << "\aEntry does not fall within specified range. ";
      Sleep(1500);
      gotoxy(50,51); cout << "                                             ";
      gotoxy(50,53); cout << "                                             ";
      place_bet();
} 

void Roulette::bet_error()
{
      gotoxy(50,44); cout << "\aYou must place a bet before spinning the wheel! ";
      Sleep(1500);
      gotoxy(50,44); cout << "                                                  ";
      prompt();
}

Edited by Clinton Portis: n/a

Votes + Comments
You're kidding, right? I thougt you knew better than this!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.