2016-07-13 4 views
0

私はプログラミングにはとても新しいので、この質問が面倒ではないことを願っています。ビデオチュートリアルシリーズの一環として、私はYouTubeを通じて使用しています。このチュートリアルでは、あらかじめ定義された問題を解決するために、その時点までに言語の部分を使用する一連の課題を使用しています。この場合は、そのは、単純な人間対スケルトン「1D戦闘シミュレーター」(LINK:https://youtu.be/TH7plF4UT_E?list=PLSPw4ASQYyynKPY0I-QFHK0iJTjnvNUys)作りメルセンヌツイスターとアルゴリズムのセマンティク問題を受け取ります。

に家庭教師を、ベンは、もともとdefault_random_engineを使用しますが、ビデオの状態の彼のイライラノートにこれは悪い実装であることと、 mt19937は良い選択です。 (リンク:https://youtu.be/JGwSEbnJGR0?list=PLSPw4ASQYyynKPY0I-QFHK0iJTjnvNUys&t=138)。

私は、スタックオーバーフローなどcplusplus.com、および他の部位など

私はこれらのいずれかで役に立つヒントを見つけることができませんでし経由で検索しました。ほとんどのMersenne Twisterの質問は、ヘッダやライブラリファイルの問題ではなく、使用上の問題と思われました。 :Issues with c++ 11 mersenne_twister_engine classHow to run Mersenne Twister inside a function?mt19937 and uniform_real_distributionWhy do I get the same sequence for every run with std::random_device with mingw gcc4.8.1?

私はそれを理解して、より多くのエントロピーと、より正確なランダム生成のためのMT19937の敗走を行くことにしました。私が取り組んでいる小規模なプロジェクトにとっては、残念なことかもしれません。

多くのC++チュートリアル(ビデオや書籍)では、語彙&の構文を教えているが、その言語で問題解決を教えることはめったにありません。だから、私はそれらを理解していなくても、私はそれらをよく理解しようとすることができるように、私は周りをスキップし、物事でおもちゃをする。私の機能の理解は、その解剖学的構造と最良の使用の​​点で弱いです。クラスでも同じことが起こります。それにもかかわらず、関数にmet19937を実装していますので、多面体のダイシミュレーションを作成することができました。 (d4、d6、d8、d10)。

私のソースコードにはエラーはありませんが、algorithm.hファイルには同じタイプの2つのエラーがあります。

タイプ 'のstd :: __ 1 :: mersenne_twister_engine(長い)' が前に使用することはできません '::' 私はalgorithm.hファイルに行くとき、それはメンバー

を持っていないので(など私は)私が何をしていたか知っていたならば、これらのエラーがで発見されたコードの行がある。

private: 
typedef typename _Engine::result_type _Engine_result_type; 
typedef typename conditional 
    < 

tatic _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() 
                + _Working_result_type(1); 

(。それぞれのファイルの行2844 & 2866上)

これは私のソースコードで、私は一つのことは、残りの部分と周りいじる前に作業を取得しようとしていますように、私は、多くの項目をコメントアウトしています

#include <iostream> 
#include <string> 
#include <random> 
//#include <chrono> 
//#include <ctime> 

using namespace std; 

/* 
    // Simple Attack and Defense Attributes. 
int Agility = 40; 
int Attack =52; 

int MyAgility = 45; 
int MyAttack = 64; 
*/ 

//Globals 
mt19937 randomGenerator(time_t); 
//default_random_engine randomGenerator(time_t); 

//Function Protypes 
void d4(); 
void d6(); 
void d8(); 
void d10(); 
void SuccessCheck(); 


int main() 
{ 

    //Variables 
    int MyWarriors; 
    //int WarriorsAvailable = NULL; 
    //int Skeletons = NULL; 
    //int CharacterCount; 

    /* 
    int Attack; 
    int Defense; 
    int AxeDMG = 1; 
    int SwordDMG = 2; 
    int ShieldDEF = 1; 

    //String Variables 
    //Too Many Soldiers Requested 
    char str1[150] = "I'm sorry Sire, we just don't have that many troops available."; 
    char str2[150] = "No sir! We just can't manage that."; 
    char str3[150] = "Oh! Our woes would not be so terrible, should we only have that many in our guard."; 
    char str4[150] = "Have you been listening to my counsel? We just can't raise an army that size in such a short time."; 
    char str5[150] = "Very well Sir, I suppose we'll perish until you can't give me a real answer."; 

    //A Valid Amount of Soldiers Requested 
    char str6[150] = "Great! I'll send them through the gates immediately!"; 
    char str7[150] = "YES! Through the forest, they'll ut down our foes!"; 
    char str8[150] = "By the Lord Almighty, they shall be vanquished!"; 
    char str9[150] = "They won't stand a chance!"; 
    char str10[150] = "No man or beast will rise against you again!"; 

    //Maybe Not Enough Soldiers Requested 
    char str11[150] = "You may need a tad more men, Sire."; 
    char str12[150] = "Perhaps, you'd like to join your men, just in case they need help?"; 
    char str13[150] = "I think we have a few more men you could send, Sire."; 
    char str14[150] = "Oh yea, you'll have them quaking in their boots. Much like kittens and babies would. Perhaps you could spare a few more?"; 
    char str15[150] = "You can't say that I didn't warn you. Maybe send a few more men."; 
    */ 


    //Random number of Skeletons! 
    uniform_int_distribution<int> Skeletons (1, 3000); 

    //Random number of Skeletons! 
    uniform_int_distribution<int> WarriorsAvailable (1, 3000); 

    //Invalid MySoldier Selection 
    uniform_int_distribution<int> TooManySoldiers (1, 5); 

    //Random number of Skeletons! 
    uniform_int_distribution<int> RightNumSoldiers (6, 10); 

    //Random number of Skeletons! 
    uniform_int_distribution<int> ATadMoreSoldiers (11, 15); 

    cout << "Sire! There are " << Skeletons(randomGenerator) << " marching toward the castle!" << endl << "We should send out our troops!" << endl; 
    cout << "Our registry indicates that we have " << WarriorsAvailable << " who are combat ready!"; 
    cout << "How many warriors do wich to send out to battle?" << endl; 
    cin >> MyWarriors; 


    /* 
    do { 
     <#statements#> 
    } while (<#condition#>); (MyWarriors > WarriorsAvailable) 
     cout << << endl << "How many warriors woudl you like to send in to combat?"; 


    cout << "Yes, Sire! I will rally " << MyWarriors << " to do battle with our enemy, at once!" << endl << endl; 


    */ 

    return 0; 

} 


//Dice Functions: Many of these functions cam from a Stack Overflow answer I found weeks prior to joining 
void d4(){//function prints a randomly generated number 
    //mt19937 randomGenerator(time_t); 
    //auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); 
    uniform_int_distribution<int> d4Die (1, 4); 
    int d4Roll = d4Die(randomGenerator); //the generated number is then set equal to an integer for functionality 
    cout << d4Roll; //finally, the integer is printed to the screen 
} 


void d6(){//function prints a randomly generated number 
    //mt19937 randomGenerator(time_t); 
    uniform_int_distribution<int> d6Die (1, 6); 
    int d6Roll = d6Die(randomGenerator); //the generated number is then set equal to an integer for functionality 
    cout << d6Roll; //finally, the integer is printed to the screen 
} 

void d8(){//function prints a randomly generated number 
    //mt19937 randomGenerator(time_t); 
    uniform_int_distribution<int> d8Die (1, 8); 
//the generated number is then set equal to an integer for functionality 
    int d8Roll = d8Die(randomGenerator); 
    cout << d8Roll; //finally, the integer is printed to the screen 
} 

void d10(){//function prints a randomly generated number 
    //mt19937 randomGenerator(time_t); 
    uniform_int_distribution<int> d10Die (1, 10); 
    //the generated number is then set equal to an integer for functionality 
    int d10Roll = d10Die(randomGenerator); 
    cout << d10Roll; //finally, the integer is printed to the screen 
} 

void SuccessCheck(){//function prints a randomly generated number 
    //mt19937 randomGenerator(time_t); 
    //Success Check based on a percentile roll of (2d10) yielding a 1 - 100% 
    uniform_int_distribution<int> SCDie (1, 100); 
    //the generated number is then set equal to an integer for functionality 
    int SCRoll = SCDie(randomGenerator); 
    cout << SCRoll; //finally, the integer is printed to the screen 

} 

//Combat Skill Modifier 
uniform_int_distribution<int> CmbSklMod (1, 3); 

//uniform_real_distribution<float> SucessCheck(0.0f, 1.0f); 
+0

'mt19937 randomGenerator(time_t);'は関数宣言であり、あなたが望むものではないと思います。 – MikeCAT

答えて

2

あなたの問題は

mt19937 randomGenerator(time_t); 

がランダムnumnberジェネレータを作成していないということです。 randomGeneratorという名前の関数を宣言します。関数はtime_tで、mt19937を返します。あなたがする必要がどのような

はPRNGを作成し、rdから初期値でそれをシードします

std::random_device rd 
std::mt19937 randomGenerator{rd()}; 

このようなものを使用しています。あなたがより良い種子を望むなら、あなたは

std::random_device rd; 
std::seed_seq seed{rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()}; 
std::mt19937 randomGenerator(seed); 

を使用することができます上記の技術はthis answer

+0

私はこれらの可能な解決策に取り組み、コメントを付けて皆に戻ってきます。私は本当に助けに感謝します! – jcales

0

bames53から来て、あなたにネイサンをありがとう!これは実際にはかなり役に立ちました。私が最初のオプションを入力すると、私は新しいエラーを受け取りました。以下は、エラーの説明とそれらの解決方法です。もちろん、より多くの熟練プログラマーはそれを明らかに見ていると思います。 :

uniform_int_distribution<int> Skeletons (randomGenerator); 

エラー

なしミリアンペア:Pクラステンプレートuniform_int_distributionの

エラー 使用は、コードで解決

テンプレート引数が必要ですcppreference.com

最終的な作業コードスニペット:

uniform_int_distribution<int> Skeletons (1, 3000); 

追加ヘルプがどこから来た:uniform_int_distributionの初期化コードで解決宣言されていない識別子のスケルトン

の 使用するコンストラクタをtching

及び使用中10

//Random number of Skeletons! 
uniform_int_distribution<int> Skeletons (1, 3000); 

//Random number of Available Warriors! 
uniform_int_distribution<int> WarriorsAvailable (1, 3000); 

//Invalid MySoldier Selection 
uniform_int_distribution<int> TooManySoldiers (1, 5); 

//Random number of Skeletons! 
uniform_int_distribution<int> RightNumSoldiers (6, 10); 

//Random number of Skeletons! 
uniform_int_distribution<int> ATadMoreSoldiers (11, 15); 

=============メスナー-ツイスターの信頼性=============== ====== これは、上記のコードでrd()があまりにも多く使用されている理由です。

より良い、より正確な方法はありますか? Messner-Twisterについての「警告」と、それがより良い播種までどのように予測できるかをご紹介します。

Stack Overflow

======================間接的に関連する================= ================

私の質問からの投稿されたコードから、私は私のダイス機能がどこから来たかをリンクすると思った。

http://www.cplusplus.com/forum/beginner/138382/

ボイドD4(){//関数は、ランダムに生成された数 uniform_int_distribution d4Die(1,4)を出力し、 int d4Roll = d4Die(randomGenerator); //生成された数値は、機能の整数に等しく設定されます。 cout < < d4Roll; //最後に、整数=========画面 }

=======================脚注に印刷されています=================================

ご協力いただきありがとうございます。私は、この答えがPRGNのこの問題や似たような問題を克服する言語に新しい人たちを助けることを願っています。それが戦闘システムであろうとRPG(ロールプレイングゲーム)であろうと、それは有用かもしれません。

+0

**金型シミュレーションの他の実装へのURL ** *(この回答に遭遇する人のために)* フィルのプロジェクト http://philipwashy.blogspot.com/2016/06/ive-been-working-on -building-video-game.html 赤いブロブゲーム: http://www.redblobgames.com/articles/probability/damage-rolls.html https://isocpp.org/files/papers/n3551 pdf – jcales

関連する問題