samedi 17 septembre 2016

Why is my first roll out of my program not random?

This program is a game of craps I tried coding using nothing but my memory (I am very green at c++, I am only about two weeks into it between work and my girlfriend lol) The program runs perfectly fine, with the exception that every time the program starts up, it gives me the same random number, but if you keep on playing the game and start a new game without exiting the console, the first roll number is apparently random, seeing as how I can't predict what it will be after the "again()" funtion, but when the code first starts up, the first roll is always ten. I have been trying to figure out the problem on my own and I can't seem to find anyone with a craps game coded how I did it, but maybe someone can help me out here.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//main prototype
int main();

//score system
int wins = 0;
int loses = 0;




int again(){

int answer;

cout << "\nWould you like to play another round? (1=y,2=n)\n" << endl;
cin >> answer;
cout << endl;
cout << endl;

if(answer==1){
main();

}else if(answer==2){

cout << "thanks for playing homie" << endl;

return 0;

}else{
cout << "I'm sorry what?" << endl;
again();
}

}//end of again


class DiceClass{
public:
DiceClass(){
srand(time(0));
}


int firstdiceroll = 2+rand()%11;


void PhaseOne(){



cout << "Lets play some craps. \n" << endl;
system("pause");
cout << endl;

cout << "You rolled " << firstdiceroll << "." << endl;

if(firstdiceroll==7 || firstdiceroll==11){
    cout << "You win!!" << endl;
    wins++;
    cout << "Currents wins: " << wins << "\nCurrent loses: " << loses << endl;
    again();
    }
    else if(firstdiceroll==2 || firstdiceroll==3 || firstdiceroll==12){
    cout << "You lose!" << endl;
    loses++;
    cout << "Currents wins: " << wins << "\nCurrent loses: " << loses << endl;
    again();
    }
    else{

        cout << "Rolling again!\n" << endl;
        system("pause");
        cout << endl;
        PhaseTwo();
    }


} //ends PhaseOne

void PhaseTwo(){

int seconddiceroll = 2+rand()%11;

cout << "You rolled " << seconddiceroll << endl;

if(firstdiceroll==seconddiceroll){
    cout << "You win!!" << endl;
    wins++;
    cout << "Currents wins: " << wins << "\nCurrent loses: " << loses << endl;
    again();
    }
else if(seconddiceroll==7){
    cout << "You lose!" << endl;
    loses++;
    cout << "Currents wins: " << wins << "\nCurrent loses: " << loses << endl;
    again();
    }
else{

    cout << "Rolling again." << endl;


        system("pause");
        cout << endl;
        PhaseTwo();

    }



} //ends PhaseTwo


}; //ends DiceClass








int main()
{
DiceClass DObject1;
DObject1.PhaseOne();


return 0;
}




Aucun commentaire:

Enregistrer un commentaire