C++:关于囚徒困境申请的建议

C++: Advice for a Prisoner's Dilemma application

本文关键字:困境 囚徒 于囚徒 C++      更新时间:2023-10-16

所以,基本上我正在为一个相当简单的囚犯困境游戏制作一个C++程序。

在我制作的这个变体中,囚犯B需要遵循囚犯A在上一轮中的做法。为了让它更容易,下面是我现在拥有的代码。

//////////////////////////////////
//+--------+---------+---------+//
//|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;
}

我还做了另外两个变化,对手总是坦白或保持沉默。这是对手保持沉默的地方。

#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(或者我想,我)在上一轮中选择了什么。

在我定义的代码中,在第一轮比赛中,对手选择坦白。

如果有人能给我一个主意,告诉我如何领会我在上一轮比赛中所说的话,让对手从那时起遵循我的做法,我将不胜感激。

如果我没有解释清楚,请询问,以便我尝试进一步解释。

理想情况下,如果将其他玩家的选择分离到一个单独的函数中,那将是很酷的。类似于:

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

把这个放在你想做选择的地方。关于如何获得之前的选择,您可以在while循环之外创建两个变量:

char prevChoiceA;
char prevChoiceB;

并在while循环结束时使用计算机和玩家选择的任何内容更新这些内容。

这有道理吗?

在切线方向上,我认为你试图创建的是标准的胡萝卜棒模拟。因此,当任何一个玩家偏离子游戏完美纳什均衡时,就会产生冷酷触发策略。您可以将其编码到PrisonerBChoice函数中!看看这里的不同功能(描绘不同类型的人)是如何产生有意义的结果的,这可能很有趣。你可以对PrisonerA和B的函数进行编码,让它们自己运行,并检查结果!(一旦你有了两个新的变量和函数,你就有了创建这个所需的一切)