基本的には、単純な囚人のジレンマゲームのための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(または私が推測する)を参照する方法がわからないので、今ここで立ち往生しています。
私は最初のラウンドで、相手が告白を選択したと定義しました。
誰かが私が以前のラウンドで言ったことをどのように拾い上げて相手が私がその後したことに従うようにするかについてのアイデアを私が与えることができれば、大いに感謝します。
私が何かを明確に説明していない場合は、さらに詳しく説明してください。
変数を作成してラウンドが終了したら、囚人のしたことを保存します。 – Carcigenicate