So my program works it just doesn't generate new rolls when I play another game. I'm using a seed of 35 because the assignment requires me to do so. How can I get my game to run on different dice rolls after the first game?
#include <iostream>
#include <stdlib.h>
#include <time.h>
usingnamespace std;
//Declaring Global variables
int roll_count = 0;
//Function declarations
void playOneGameOfCraps();
int rollOneDice();
void FirstRoll();
void OtherRolls(int point);
//Main method
int main()
{
char ch;
while(true)
{
//Calling the function
playOneGameOfCraps();
cout<<"\nWould you like to play again (y for yes)?";
cin>>ch;
if(ch=='y'||ch=='Y')
{
continue;
}else
{
break;
}
}
return 0;
}
//Function implementation which have the code to play the game
void playOneGameOfCraps()
{
srand(35);
FirstRoll();
}
void FirstRoll()
{
int sum = 0;
char ch;
int point;
sum = rollOneDice();
if (sum == 7 || sum == 11) {
cout << "Yeah! you won with a " << sum << " on the first roll." << endl;
ch = 'W';
}
elseif (sum == 2 || sum == 3 || sum == 12) {
cout << "Sorry, you lost with a " << sum << " on the first roll." << endl;
}
elseif (sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10) {
point = sum;
cout <<"\nYour point is " << sum << endl;
cout<<endl;
OtherRolls(point);
}
}
void OtherRolls(int point)
{
int sum = 0;
while (true) {
sum = rollOneDice();
// cout << "Roll " << roll_count << ". " << sum << " " << endl;
if (sum == point) {
cout << "\nYou rolled your point! You won!" << endl;
break;
}
elseif (sum == 7) {
cout << "Craps! You lost!" << endl;
break;
}
elsecontinue;
}
}
int rollOneDice()
{
int sum = 0;
int dice1,dice2;
constint SnakeEyes = 2; //If the player throws two ones.
constint BoxCars = 12; //If the player throws two sixes.
constint BigRed = 7; //If the player throws a three and four
dice1= rand() % 5 + 1;
dice2= rand() % 5 + 1;
sum=(dice1+dice2);
cout<<"Player rolled:"<<dice1<<"+"<<dice2<<"="<<sum<<endl;
if (sum == BoxCars) //If the player rolls two sixes.
{
cout << "Boxcars!" << endl << endl;
}
if (sum == BigRed)
{
cout << "Big Red!" << endl << endl; //If the player's roll equals 7.
}
if (sum == SnakeEyes)//If the player rolls two ones.
{
cout << "Snake Eyes!" << endl << endl;
}
roll_count++;
return sum;
}
What you're using to generate your random numbers - rand() - isn't random. It will generate a sequence of numbers based on the seed value you give it with srand().
If you give it the same seed value every time, you'll get the same sequence every time.
You are giving it the same seed value every time (35) so you get the same sequence every time.