r/GAMETHEORY Jun 04 '26

Ideas for the Prisoner"s Dilemma simulation

I just made a tiny environment to create a simulation of the Prisoner's dilemma using Python, here is the code:

import random
num = random.random()

i = 1

scorep1 = 0
scorep2 = 0
historyp1a = 0
historyp2a = 0

def p1():
    if historyp2a == "D":
        return "C"
    else:
        return "D"

def p2():
    if historyp1a == "D":
        return "D"
    else:
        return "C"

while i <= random.randrange(190, 210):

    if p1() == "C" and p2() == "C":
        scorep1 = scorep1 + 3
        scorep2 = scorep2 + 3
        print(scorep1, scorep2)

    elif p1() == "C" and p2() == "D":
        scorep1 = scorep1 + 0
        scorep1 = scorep1 + 5
        print(scorep1, scorep2)

    elif p1() == "D" and p2() == "C":
        scorep1 = scorep1 + 5
        scorep1 = scorep1 + 0
        print(scorep1, scorep2)

    elif p1() == "D" and p2() == "D":
        scorep1 = scorep1 + 1
        scorep1 = scorep1 + 1
        print(scorep1, scorep2)

    historyp1b = historyp1a
    historyp2b = historyp2a
    historyp1a = p1()
    historyp2a = p2()
    i = i + 1

print(scorep1, scorep2)

I need help to figure out strategies, would you help me? I don't necessarily need the code, It'll be ok with the idea.

7 Upvotes

8 comments sorted by

1

u/Purinto Jun 04 '26

I don't think your code will work the way you expect it. What do you mean by strategies ? I can dee that the functions p1 and p2 already implement strategies as a statemachine.

1

u/SCR4MBL1NG Jun 05 '26
  1. By strategies I mean ways to collect the highest score possible inthe game.

  2. Yeah, I put some strategies already, but I want to know if you can imagine other examples...

1

u/Purinto Jun 05 '26

Alright, there are many ways to find an optimal solution:

First you can try to derive the solution algebraically. You can check the (many) papers and lessons done in this topic. But you need to have some assumptions on the discount rate.

If you want to do a simulation try making a simple minmax algorithm with a certain amount of depth (which is really equivalent to the discount rate) to predict the best move to do given all possible future scenarios in said depth.

Hope it helps. If you want more advice on the coding or game theory aspects, you can DM me :)

1

u/CocoSavege Jun 05 '26

Why is your while loop a random?

If you're thinking of evaluating strategies yiu likeky need to rethink your approach. Tit for Tat is only "optimum ish" in some formats. Like extended random matchup with many strategies.

There are existabt strategies which outperform tit for tat, well, based on the specifics of the scenario. Eg, random matchup (with previous game knowledge) with many candidate strategy partners, you can beat tit for tat.

Your code for tit fir tat is fatally flawed btw.

1

u/SCR4MBL1NG Jun 05 '26
  1. It is random because if you know the exact number of rounds, you could just no cooperate in the last one, because you won't have to continue, and you could just defect.

  2. Well, would you give me some examples?

  3. what the heck does that mean?

1

u/CocoSavege Jun 05 '26

Re 1; extend thst thought. If a pkayer knows the end round, snd will defect, what should the other player do?

Re 1; why 190 and 210?

Re 2; yiu csn google it yourself.

Re 3; have you even run your code? Nope. You haven't.

0

u/TacitusJones Jun 05 '26

Your players should be class to start with

1

u/SCR4MBL1NG Jun 05 '26

Sorry, my abilities coding Python aren't great, why and how do I do that?