@@ -262,22 +262,24 @@ def strategy(self, opponent: Player) -> Action:
262
262
return D
263
263
return C
264
264
265
- if len (self .history ) >= 57 :
266
- # Check if opponent plays randomly, if so, defect
267
- p_value = chisquare ([opponent .cooperations , opponent .defections ]).pvalue
268
- self .opponent_is_random = (p_value >= self .alpha ) or self .opponent_is_random
265
+ # Check if opponent plays randomly, if so, defect
266
+ p_value = chisquare ([opponent .cooperations , opponent .defections ]).pvalue
267
+ self .opponent_is_random = (p_value >= self .alpha ) or self .opponent_is_random
269
268
270
- if self .opponent_is_random :
271
- return D
272
- if all (opponent .history [i ] == self .history [i - 1 ] for i in range (1 , len (self .history ))):
273
- # Check if opponent plays Tit for Tat
274
- if opponent .history [- 1 ] == D :
275
- return D
276
- return C
277
-
278
- if len (self .history ) == self .next_random_defection_turn :
279
- self .next_random_defection_turn = random .randint (5 , 15 ) + len (self .history ) # resample the next defection turn
280
- return D
269
+ if self .opponent_is_random :
270
+ return D
271
+ if all (opponent .history [i ] == self .history [i - 1 ] for i in range (1 , len (self .history ))):
272
+ # Check if opponent plays Tit for Tat
273
+ if opponent .history [- 1 ] == D :
274
+ return D
275
+ return C
276
+
277
+ if self .next_random_defection_turn is None :
278
+ self .next_random_defection_turn = random .randint (5 , 15 ) + len (self .history )
279
+
280
+ if len (self .history ) == self .next_random_defection_turn :
281
+ self .next_random_defection_turn = random .randint (5 , 15 ) + len (self .history ) # resample the next defection turn
282
+ return D
281
283
return C
282
284
283
285
0 commit comments