C ++:对囚徒困境应用的建议

时间:2017-01-24 15:04:53

标签: c++

所以,基本上我正在为一个相当简单的囚徒困境游戏制作一个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(或者我猜,我)。

在我定义的代码中,在第一轮中,对手选择了忏悔。

如果有人可以告诉我如何理解我在前一轮中所说的内容,以便对手跟随我从那时起所做的事情,我将不胜感激。

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

1 个答案:

答案 0 :(得分:1)

理想情况下,如果将其他玩家所做的选择分离为单独的功能,那将会很酷。 类似的东西:

var otherInput = $(this).closest('.dropdown').find('span');

将它放在您想要做出选择的任何地方。 关于如何获得先前的选择,您可以在while循环之外创建两个变量:

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

并在while循环结束时更新这些内容,无论计算机和播放器选择什么。

这有意义吗?

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