2017-01-24 32 views
1

基本的には、単純な囚人のジレンマゲームのためのC++プログラムを作っています。C++:Prisoner's Dilemmaアプリケーションへのアドバイス

私が囚人Bを前のラウンドで何をしたかに従わせる必要があるこの変形例では、ここで私が今持っているコードを簡単にするためです。

    ////////////////////////////////// 
        //+--------+---------+---------+// 
        //|Results | Silence | Confess |//<-prisonerB - opponent 
        //+--------+---------+---------+// 
        //|Silence | 3,3 | 0,5 |// 
        //+--------+---------+---------+// 
        //|Confess | 5,0 | 1,1 |// 
        //+--------+---------+---------+// 
        ////////////////////////////////// 
        // ^prisonerA - us 
#include <iostream> 
#include <stdlib.h> 
using namespace std; 

int main() 
{ 
    int numOfRounds; 
    char prisonerA; //us 
    char prisonerB; //opponent 
    int prisonerAYears; 
    int prisonerBYears; 
    int round; //num of round 
    int i; //counter 


    prisonerAYears=0; 
    prisonerBYears=0; 
    round=1; 

    cout<<"Enter the number of rounds you want to play: "; 
    cin>>numOfRounds; 

while(numOfRounds>0) 
{ 
    numOfRounds=numOfRounds-1; 
    cout<<"Game "<<round++<<endl; 

    cout<<"What is your decision? Confess or stay Silent?(C/S) : "; 
    cin>>prisonerA; 


while(round==1) 
{ 
    if ((prisonerA=='C') || (prisonerA=='c')) 
    { 
     //prisonerB='C'; 
     prisonerAYears=prisonerAYears+1; 
     prisonerBYears=prisonerBYears+1; 
     cout<<"Prisoner A (you) chose: Confess. Prisoner B (the opponent) chose: Confess."<<endl; 
     cout<<"Prisoner A (you) is currently in for: "<<prisonerAYears<<" years."<<endl; 
     cout<<"Prisoner B (the opponent) is currently in for: "<<prisonerBYears<<" years."<<endl; 
    } 

    else if((prisonerA=='S') || (prisonerA=='s')) 
    { 
     //prisonerB='C'; 
     prisonerAYears=prisonerAYears+0; 
     prisonerBYears=prisonerBYears+5; 
     cout<<"Prisoner A (you) chose: Silence. Prisoner B (the opponent) chose: Silence."<<endl; 
     cout<<"Prisoner A (you) is currently in for: "<<prisonerAYears<<" years."<<endl; 
     cout<<"Prisoner B (the opponent) is currently in for: "<<prisonerBYears<<" years."<<endl; 
    } 
} 


} 

cout<<"The rounds have come to an end."<<endl<<endl; 
cout<<"Prisoner A (you) is currently in for: "<<prisonerAYears<<" years."<<endl; 
cout<<"Prisoner B (the opponent) is currently in for: "<<prisonerBYears<<" years."<<endl<<endl; 

if(prisonerAYears>prisonerBYears) 
{ 

    cout<<"You have to stay in prison longer. You lose."<<endl; 
} 
if(prisonerAYears<prisonerBYears) 
{ 

    cout<<"The opponent has to stay in prison longer. You win."<<endl; 
} 

if(prisonerAYears==prisonerBYears) 
{ 

    cout<<"You both are staying in prison for the same amount of time. Its a tie."<<endl; 
} 
     return 0; 
} 

また、相手が常に告白したり、黙ったりしている2つのバリエーションも作成しました。相手が黙っている場所はここにあります。

#include <iostream> 
#include <stdlib.h> 
using namespace std; 

int main() 
{ 
    int numOfRounds; 
    char prisonerA; 
    char prisonerB; 
    int prisonerAYears; 
    int prisonerBYears; 
    int round; //broj runde 

    prisonerAYears=0; 
    prisonerBYears=0; 
    round=1; 

    cout<<endl; 
    cout<<"Enter the number of rounds you want to play: "; 
    cin>>numOfRounds; 
    cout<<endl; 

while(numOfRounds>0) 
{ 
    numOfRounds=numOfRounds-1; 
    cout<<endl; 
    cout<<"Game "<<round++<<endl; 
    cout<<endl; 

    cout<<"What is your decision? Confess or stay Silent?(C/S) : "; 
    cin>>prisonerA; 
    cout<<endl; 

    if ((prisonerA=='C') || (prisonerA=='c')) 
    { 
     prisonerB='S'; 
     prisonerAYears=prisonerAYears+5; 
     prisonerBYears=prisonerBYears+0; 
     cout<<"Prisoner A (you) chose: Confess. Prisoner B (the opponent) chose: Confess."<<endl; 
     cout<<"Prisoner A (you) is currently in for: "<<prisonerAYears<<" years."<<endl; 
     cout<<"Prisoner B (the opponent) is currently in for: "<<prisonerBYears<<" years."<<endl; 
    } 

    else if((prisonerA=='S') || (prisonerA=='s')) 
    { 
     prisonerB='S'; 
     prisonerAYears=prisonerAYears+3; 
     prisonerBYears=prisonerBYears+3; 
     cout<<"Prisoner A (you) chose: Silence. Prisoner B (the opponent) chose: Confess."<<endl; 
     cout<<"Prisoner A (you) is currently in for: "<<prisonerAYears<<" years."<<endl; 
     cout<<"Prisoner B (the opponent) is currently in for: "<<prisonerBYears<<" years."<<endl; 
    } 

} 
cout<<endl; 
cout<<"The rounds have come to an end."<<endl<<endl; 
cout<<"Prisoner A (you) is currently in for: "<<prisonerAYears<<" years."<<endl; 
cout<<"Prisoner B (the opponent) is currently in for: "<<prisonerBYears<<" years."<<endl<<endl; 

if(prisonerAYears>prisonerBYears) 
{ 

    cout<<"You have to stay in prison longer. You lose."<<endl; 
} 
if(prisonerAYears<prisonerBYears) 
{ 

    cout<<"The opponent has to stay in prison longer. You win."<<endl; 
} 

if(prisonerAYears==prisonerBYears) 
{ 

    cout<<"You both are staying in prison for the same amount of time. Its a tie."<<endl; 
} 
     return 0; 
} 

これはかなり簡単でした。私は前のラウンドで選んだ囚人A(または私が推測する)を参照する方法がわからないので、今ここで立ち往生しています。

私は最初のラウンドで、相手が告白を選択したと定義しました。

誰かが私が以前のラウンドで言ったことをどのように拾い上げて相手が私がその後したことに従うようにするかについてのアイデアを私が与えることができれば、大いに感謝します。

私が何かを明確に説明していない場合は、さらに詳しく説明してください。

+2

変数を作成してラウンドが終了したら、囚人のしたことを保存します。 – Carcigenicate

答えて

0

理想的には、別のプレーヤーの選択肢を別の機能に分けた場合は、すばらしいことです。以下のような 何か:あなたが選択をすることにしたいところはどこでも

char PrisonerBChoice(char prevPrisonerAChoice){ 
    // your logic for how B's choice depends on A's 
} 

はこれを入れてください。

char prevChoiceA; 
char prevChoiceB; 

をして、コンピュータとプレイヤーが選んだものは何でもしてwhileループの最後にこれらを更新:以前の選択を取得する方法について は、あなたは、whileループの外で2つの変数を作成することができます。

これは意味がありますか?

私はあなたが作成しようとしているのは、標準的なニンジンスティックシミュレーションだと思います。だから、いずれかのプレイヤーがSubgame Perfect Nash Equilibriumを逸脱するとGrim-Triggered戦略につながります。 PrisonerBChoice関数にこれを正確に記述することができます!興味深いことに、異なる種類の人々を描写する異なる機能がここでどのように有意義な結果につながるかを見ることができます。 PrisonerAとBの両方の機能をコーディングして、それを自分で実行させて、結果をチェックアウトすることもできます。 (2つの新しい変数と関数があれば、これを作成するために必要なものはすべて用意されています)

+0

はい、私はあなたが意味するものを得ます。提案していただきありがとうございます!あなたが提案したものを試してみます。正直言って、私はC++でちょっと錆びています。私は2年近く経ってから再び取り上げました。 – aleksxp

関連する問題