class Bot: """ Base class for all bots. """ name = "Bot Base Class" debug_flag = False def __init__(self): self.debug(self.name +" init") def answer(self, f, id): pass def inform(self, a, id): pass def debug(self, text): if self.debug_flag: print text class AlwaysZero(Bot): """ Always return choice '0' """ name = "AlwaysZero" def answer(self, f, id): return 0 class AlwaysOne(Bot): """ Always return choice '1' """ name = "AlwaysOne" def answer(self, f, id): return 1 class Spite(Bot): """ Choose the default_choice until the opponent plays spite_trigger, then continue playing spite_choice for rest of the tournament """ name = "Spite" choice = {} default_choice = 1 spite_trigger = 0 spite_choice = 0 def answer(self, f, id): if not self.choice.has_key(id): self.choice[id] = self.default_choice return self.choice[id] def inform(self, a, id): if a == self.spite_trigger: self.choice[id] = self.spite_choice class CopyCat(Bot): """ Always copy my opponent's last move """ name = "CopyCat" choice = {} default_choice = 0 def answer(self, f, id): if not self.choice.has_key(id): self.choice[id] = self.default_choice return self.choice[id] def inform(self, a, id): self.choice[id] = a class AlwaysSelfish(Bot): """ Always choose item that gives most payoff to me """ name = "AlwaysSelfish" a = -1 def answer(self, f, id): if self.a == -1: self.compute(f) return self.a def compute(self, f): self.a = 0 max_payoff = f.field[0][0][0] for i in range(0,f.choices): for j in range(0,f.choices): if f.field[i][j][0] > max_payoff: max_payoff = f.field[i][j][0] self.a = i class AlwaysEvil(Bot): """ Always choose item that results in least gain (or most harm) to the opponent """ name = "AlwaysEvil" a = -1 def answer(self, f, id): if self.a == -1: self.compute(f) return self.a def compute(self, f): self.a = 0 min_payoff = f.field[0][0][1] for i in range(0,f.choices): for j in range(0,f.choices): if f.field[i][j][1] < min_payoff: min_payoff = f.field[i][j][1] self.a = i