Hi, erm, new here. I'm writing a Roulette randomness program that simulates...well, the roulette game. However, after the "switch (play_method)" chunk, it keeps looping infinitely and skipping the "switch (color)" part.
I've only started on the first case, since I figured that once i got this down everything else would be copy/pasting and tweaking.
If someone could tell me if my use of randomness is correct too, that'd be great...or if there's a better way of shortening this with if statements and loops - at this rate my lines will be in the thousands...
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
main()
{
//declare integers
int total_amt, bet_amt, win_amt, play_method, r, choice, color, num;
//may need constants here
//opening statement
cout << "Welcome welcome, to Ringside Roulette!" << endl;
cout << "How much money do you have? $";
cin >> total_amt;
cout << "How much money would you like to bet? $";
cin >> bet_amt;
//start of overall do while loop
do
{
//choose method of play
cout << "1 - Play a color.\n";
cout << "2 - Play a number.\n";
cout << "Please select an option. ";
cin >> play_method;
switch (play_method)
{
case 1: // color play
cout << "A - Play Red.\n";
cout << "B - Play Black.\n";
cout << "C - Play Green. (Hit the leprechaun's jackpot!)\n";
cout << "Which would you like to bet on? " << endl;
cin >> color;
switch (color)
{
case'A': //the red color
for (r=1; r<0; r=r+2)
{
r = rand()%18+1;
cout << "You spun a " << r << endl;
if (color == r)
{
win_amt = bet_amt*2;
total_amt = total_amt + (win_amt - bet_amt);
cout << "Lucky! You have won $" << win_amt << "!" << endl;
cout << "You now have $" << total_amt << " remaining." << endl;
}
else
{
total_amt = total_amt - bet_amt;
cout << "Tough luck. You have lost $" << bet_amt << endl;
cout << "You now have $" << total_amt << " remaining." << endl;
} //else end
} // for end
break; // break case 1
} // end of color switch statment
break;
} //end of play_method switch statement
cout << "Would you still like to play? Press 1 to continue playing. Press 0 to quit. " << endl;
cin >> choice;
}
while (choice!=0); // end of complete do while
return 0;
}
r = rand()%18 + 1 will give you a number between 1 and 18.
For a roulette table, you may want to roll r = rand()%38; as there are typically 38 numbers on a roulette table. This would give you a range of 0 to 37. You could tread 0 as 0 and 37 as 00.
One thing that you should do, is at the top of your main, call srand() and seed it with something such as the time.
Thank you very much, both of you! This helped a lot!
I'm a little confused though, Stewbond - what is srand? Does it have to be in the code for it to be able to generate random numbers, because my teacher didn't put it in and he was fine. Also, i used the equation that you gave me, but i keep getting "4" whenever i try generating a number - is it a glitch with the compiler or something to do with having to put in srand? (I'm trying to put up my code on here but the limiter says its too long...)
#include <random> // used in Spin function
void CSession::Spin(short number)
{
using std::mt19937_64;
using std::seed_seq;
using sets::VoisinsDeZero; // it is array of numbers
using std::uniform_int_distribution;
if (number > 0)
{
// mpTable is pointer to roulette table object
mBankrol += mpTable->SetOutcome(number);
return;
}
staticbool seed = true;
static std::shuffle_order_engine<mt19937_64, 255> mGenerator;
static seed_seq seeder(VoisinsDeZero, VoisinsDeZero + 17);
static uniform_int_distribution<short> mDistribution(0, mNums - 1);
if (seed)
{
mGenerator.seed(seeder);
seed = false;
}
// mpHistory is pointer to the stack of all numbers comed out
mpHistory->push(mDistribution(mGenerator));
mBankrol += mpTable->SetOutcome(mpHistory->top());
}