Hey guys! I was listening to an interesting podcast called RadioLab and they did a story on something called 'The Iterated Prisoners Dilemma' (If you haven't heard of it, Wikipedia it). Basicly, this guy made a program that would test two 'prisoner' programs a couple hundred times and see which was better. This was really interesting to me, so I decided to code it in c++ and host a contest! First off, here is the code for the actual program that tests the prisoners:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Prisoner's Dilemma
// Two classes of Prisoner
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
/////////////////////
// Vital Glob Vars //
/////////////////////
int iRedResults[1000]; // will be in format of table above
int iBlueResults[1000];
bool bRedChoice[1000]; // if true, the prisoner testified
bool bBlueChoice[1000x];
#include "blue prisoner.h"
#include "red prisoner.h"
// In chart: red is virtical anxd blue is horazontal,
// in results of chart: Red, Blue. Goal is to get
// highter score
//////////////////////////////////
//+--------+---------+---------+//
//|Results | Nothing | Testify |//
//+--------+---------+---------+//
//|Nothing | 3,3 | 0,5 |//
//+--------+---------+---------+//
//|Testify | 5,0 | 1,1 |//
//+--------+---------+---------+//
//////////////////////////////////
///////////////
// Functions //
///////////////
int Scoring(bool red, bool blue, int i);
int main()
{
for (int i = 0; i < 501; i++)
{
iRedResults[i] = -1;
iBlueResults[i] = -1;
}
RedPrisoner Red;
BluePrisoner Blue;
long a; // all purpose int
bool contenue = true; // for tests and contenuation loops
cout << "Welcome to the Prisone's Dilemma battle.\n";
while (contenue)
{
cout << "Enter the amount of rounds you would like up to 1000:";
cin >> a;
system("cls");
if (a < 1001)
break;
else
cout << "That is not a valid number\n";
}
bool bTempBlue;
bool bTempRed;
// i guess red goes first
for (int i = 0; i < a; i++) // My guy is red
{ // u mad?
bTempRed = Red.decision();
bTempBlue = Blue.decision();
bRedChoice[i] = bTempRed;
bBlueChoice[i] = bTempBlue;
Scoring(bTempRed, bTempBlue, i);
}
cout << "Test done" << endl;
system("pause");
int iBlueTotal = 0;
int iRedTotal = 0;
for (int i = 0; i < a; i++)
{
iBlueTotal += iBlueResults[i];
iRedTotal += iRedResults[i];
}
cout << "Red - " << iRedTotal << endl;
cout << "Blue - " << iBlueTotal << endl;
if (iRedTotal > iBlueTotal)
cout << "Red Wins!" << endl;
else if (iBlueTotal > iRedTotal)
cout << "Blue Wins!" << endl;
else
cout << "It's a tie!" << endl;
ofstream Results;
Results.open("Results.txt", ios::out | ios::trunc);
Results << " Blue Results\tRed Results\n";
for (int i = 0; i < a; i++) // output the results to a txt file
{
Results << (i + 1) << " - " << iBlueResults[i] << " \t\t" <<
iRedResults[i] << endl;
}
Results.close();
return 0;
}
int Scoring(bool red, bool blue, int i)
{
if (!red)
{
if (!blue)
{
iRedResults[i] = 3;
iBlueResults[i] = 3;
}
if (blue)
{
iRedResults[i] = 0;
iBlueResults[i] = 5;
}
}
if (red)
{
if (!blue)
{
iRedResults[i] = 5;
iBlueResults[i] = 0;
}
if (blue)
{
iRedResults[i] = 1;
iBlueResults[i] = 1;
}
}
}
|
The code itself is pretty self explanatory, ask if you have questions. Now, the prisoner is in the form of a fairly simple class, this one is running the tit for tat strategy that is oh so famous:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#ifndef RED_PRISONER_H_INCLUDED
#define RED_PRISONER_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class RedPrisoner
{
public:
string sName;
bool decision();
RedPrisoner();
protected:
int iTurn;
};
RedPrisoner::RedPrisoner()
{
iTurn = 0;
sName = "Luke";
}
bool RedPrisoner::decision()
{
if (iTurn == 0)
{
iTurn++;
return true; // does not testify`
}
else
{
int b = iTurn - 1;
iTurn++;
return bBlueChoice[b];
}
}
#endif // RED_PRISONER_H_INCLUDED
|
This is pretty boring programming by yourself. You don't have anybodies prisoner to compete with. So, I am leaving it up to all of you. That's right, I'm having (as far as I know) The First Prisoners Dilemma Tournament on cplusplus.com! I want you to take an hour or two and program an intelligent class to compete with in this.
The class MAY NOT:
1) Access or Modify any members of the opposing class
2) Modify any variables in the main program
However, your class CAN read from the variables in the program, like a (count) and the four arrays. If you'd like to join, send your class to spamemailnotreal@gmail.com (sorry, I like to use that email for things like this). Ask me anything else on this thread. I will test the programs classes probably in a bracket system unless any of you have a better idea. I will also scan the classes to make sure that they are not doing any dirty tricks to win. And please make your program a little bit more creative than tit for tat (and that doesn't mean submitting the forgiving version of tit for tat (: ). I myself will be working on a class too. I will stop accepting entries and start testing them on the 7th of August. Happy coding!