# Cmput 455 sample code # Written by Martin Mueller # Creates the sample tree of # https://en.wikipedia.org/wiki/Alpha–beta_pruning # returns dictionary with the tree in adjacency list form # In the names, children of the root are labeled a, b, c. # Below that level, 'l' stands for left-most child, # 'r' for right-most child, and # 'm' for middle child when there are three children. from minimax_sample_tree_data import generateSampleTree def opponent(player): if player == 'MIN': return 'MAX' else: return 'MIN' class SampleTree(object): def __init__(self): self._tree, self._score = generateSampleTree() self.resetGame() def resetGame(self): self.toPlay = 'MAX' self.current = 'root' self.moves = [] def switchToPlay(self): self.toPlay = opponent(self.toPlay) def play(self, location): assert not self.endOfGame() assert location in self._tree[self.current] self.moves.append(self.current) self.current = location self.switchToPlay() def undoMove(self): location = self.moves.pop() self.current = location self.switchToPlay() def staticallyEvaluate(self): assert self.endOfGame() return self._score[self.current] def staticallyEvaluateForToPlay(self): score = self.staticallyEvaluate() if 'MIN' == self.toPlay: return -score return score def endOfGame(self): return self._tree[self.current] == [] def legalMoves(self): return self._tree[self.current]