Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
import random
2
3
class Smart():
4
''' Smart will choose to defect or cooperate depending on
5
the opponents past moves.
6
'''
7
def step(self, history, round):
8
# Random choice to start
9
if (round == 0) or (round == 1):
10
action = random.randint(0, 1)
11
# If the last three moves are the same, defect
12
elif sum(history[not self.order][round-4:round-1])/3 == (1 or 0):
13
action = 0
14
# Else, do what they did two moves ago
15
else:
16
action = history[not self.order][round - 2]
17
return action
18