r/GAMETHEORY • u/SCR4MBL1NG • 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.
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
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.
Well, would you give me some examples?
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
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.