Coded by Arthur Goldman (Minneapolis Southwest High School, Class of 2017) - Original Programmer
& Themi Megas (SWHS Class of 2021) - 2020 rewrite adding optimization, Python 3.8 support, arguments -e, -s, & rule compliance.
Contributors:
- Henning Tonko Added -r (random) argument (SWHS Class of 2018)
For more information contact: [email protected]
This is a Python 3 implementation of the age-old problem of logic and game theory. It was inspired by a Python 2 approach by PLTW (Project Lead the Way) for their Computer Science Principles course. This implementation of the dilemma was coded from scratch by the Southwest High School students credited above. It contains no source material other than the age-old dilemma. Our approach provides a new formulation of the classic conundrum, with new rules, more data on your opponent allowing a more rigorous playing field, and, of course, more new opportunities to cheat ( ͡° ͜ʖ ͡°). We are providing this new Python 3 wrapper Prisoner's Dilemma to the community of Python students and developers with a GNU Public License for your enjoyment and challenge.
This code requires Python 3.6 or above
and depends on pandas
.
Pandas can be installed with:
python -m pip install pandas
With these Requirements, you are ready to run your tournament. See the OFFICIAL_RULES to understand how to play.
Tournaments are run via wrapper.py
. Competitors create modules like those in the examplemodules directory. Each of these presents a different approach to competing against opponent modules, by colluding or betraying in iterative rounds. Effective competitors will find the most cunning strategies to predict and outsmart the betrayals of their opponents, inducing an opponent to collude as often as possible while sometimes being betrayed.
To run an actual tournament, wrapper.py
requires parameter arguments.
If modules for your competition are contained in a directory, use the command flag -d <directory-path>
:
./wrapper.py -d examplemodules
Alternately, to manually select modules, you can enter the names of the .py files as parameters.
For example, if modules are named...
player1.py
WinningTeam.py
C:\Users\Me\Desktop\weSuck.py
...run ./wrapper.py player1.py WinningTeam.py C:\Users\Me\Desktop\weSuck.py
.
Any number of modules can be given.
Additional Options:
-s
argument: Allows you to manually step into each round with the enter key. Good for small groups-r
argument: Randomizes player order.-e
argument: Error messages from player modules are suppressed when printing results.
The Iterative Prisoners Dilemma can be staged on GitHub. To host your own tournament, Fork this repository and upload it as a new repository to your own GitHub account. Each competing team can fork the repository to program and test their solution. When ready to compete players can send a pull request to your master branch. Merge all your competitors branches and run the competition.
(Using a Git server is by no means a requirement to host a tournament, however it provides an ideal collaborative environment.)
- In the OFFICIAL_RULES, players are instructed to place modules ready for competition in the
tournament
directory. - It is usually ideal to have players also compete against classic solutions from the
examplemodules
directory. - If you plan to include any
examplemodules
copy them to thetournament
directory before allowing your players to clone the repository. - Run the tournament using:
./wrapper.py -d tournament
Note: In PyCharm and IDEs with a 'RUN Program' function, add parameters such as -d tournament
by adding a "Run Configuration" for wrapper.py
. In PyCharm, make the following menu selection: [Run] > [Edit Configuration]
Then add parameters such as -r -d tournament
to the "Parameters" field.
Note: When running the program in Cmd or PowerShell, if Python files are not set to open in Python (i.e. if .py
files open in an IDE), You may have to enter something like:
python wrapper.py -d examplemodules
python wrapper.py player1.py WinningTeam.py C:\Users\Me\Desktop\youLose.py
python wrapper.py
Each team's module should conform to Python 3.6 or above and must provide several things:
team_name
: a string, no more than 16 characters long, containing no newlines or other weird blank space (must not match regex [^a-zA-Z\d\s:.,'%`])
strategy_name
: a string, no more than 70 characters long, containing no newlines or other weird blank space (must not match regex [^a-zA-Z\d\s:.,'%`])
strategy_description
: a string, no more then 350 characters long, containing no newlines or other weird blank space (must not match regex [^a-zA-Z\d\s:.,'%`])
move(my_history, their_history, my_score, their_score, opponent_name)
: Your definition for this function returning
either 'b'
or 'c'
for BETRAY or COLLUDE. You can also import GLOBALS
and return GLOBALS.BETRAY
or GLOBALS.COLLUDE
to make non-hard coded responses (see usage in example3 & 4). my_moves
and their_moves
are strings containing the
responses each player has made so far during one round of the prisoners dilemma, and my_score
and their_score
are
ints that will usually be negative containing each player's score so far in this round.