2016-09-17 12 views
-1

このプログラムはクラップスのゲームです。このプログラムは完璧に動作しますが、プログラムが起動するたびに同じ乱数が与えられますが、ゲームを続けてコンソールを終了せずに新しいゲームを開始すると、最初のロール番号はランダムになりますagain()機能の後にどうなるか予測できないように見えますが、コードが最初に起動するとき、最初のロールは常に10です。私は自分自身で問題を突き止めようとしていました。クラップスゲームを持っている人は、私がどのようにコード化したのか分かりませんが、誰かが私を助けてくれるかもしれません。自分のプログラムから私の最初のロールアウトがランダムではないのはなぜですか?

#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; 
} 
+1

トピックオフ:C++プログラム内のどこからでも 'main'を呼び出すことは、悪い、悪い、悪い考えです。 Sayeth ** basic.start.main **: "関数mainはプログラム内で使用されないものとします。"もしあなたがそれを定義していなければ、どうなるでしょう。 – user4581301

答えて

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

    int firstdiceroll = 2+rand()%11; 

    // etc. 
}; 

クラスのメンバ変数は、コンストラクタの本体に入る前を初期化されます。したがって、乱数シーケンスを変更する直前にfirstdicerollが設定されます。

+0

これは間違いなく意味がありますが、コンストラクタの内部を初期化しようとしたときに(firstdicerollを永久に設定してプログラムがループして変更されるまで)、firstdicerollは認識されませんでした。しかし、なぜそれが再び周りを回るとき、それは明らかにランダムであるのですか?おそらくより良い質問は、乱数ジェネレータをシードして最初のダイスロールをどのように初期化するかです。 – lilbigwill99

+0

@ lilbigwill99: 'firstdiceroll'の初期化を適切な場所に移動してください。 –

+0

@ lilbigwill99 'int firstdiceroll;'宣言はそのままで、初期化 'firstdiceroll = 2 + rand()%11;'をコンストラクタに移動します。 – pjs

関連する問題