-
Notifications
You must be signed in to change notification settings - Fork 0
/
prisoners.py
44 lines (31 loc) · 1.04 KB
/
prisoners.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from dilemma_lib import Prisoner, BETRAY, LOYAL
class LoyalPrisoner(Prisoner):
def do_turn(self, history: list):
return LOYAL
class BetrayPrisoner(Prisoner):
def do_turn(self, history: list):
return BETRAY
class TitForTatPrisoner(Prisoner):
def do_turn(self, history: list):
if len(history) == 0:
return LOYAL
return history[-1][1]
class BetrayEveryOtherTurnPrisoner(Prisoner):
def do_turn(self, history: list):
if len(history) % 2 == 0:
return LOYAL
return BETRAY
class RandomPrisoner(Prisoner):
def __init__(self):
import random
self.__random = random
def do_turn(self, history: list):
return self.__random.choice([BETRAY, LOYAL])
class DoWhatOtherDidMostPrisoner(Prisoner):
def do_turn(self, history: list):
what_other_did = {LOYAL: 0, BETRAY: 0}
for i in history:
what_other_did[i[1]] += 1
if what_other_did[LOYAL] >= what_other_did[BETRAY]:
return LOYAL
return BETRAY