class Game: def __init__(self, player, opponent): self.player = player self.opponent = opponent self.state = State() self.turn = 0 def play(self): while not self.state.gameOver(): if not self.turn & 1: move = (self.player.move(copy(self.state), 1), 1) #print "VALID" if self.state.validMove(*move) else "INVALID" while not self.state.validMove(*move): move = (random.randint(0, 6), 1) self.state.makeMove(*move) else: move = (self.opponent.move(copy(self.state), 2), 2) while not self.state.validMove(*move): move = (random.randint(0, 6), 2) self.state.makeMove(*move) self.turn += 1 print self.state return self.state.winState()
return random.choice(moves) def result(self, state, turn, depth): # return (move, heuristic) if depth == 1: return 0 results = [] for move in range(len(state.board)): if not state.validMove(move, turn): results.append(-1000) else: movedState = deepcopy(state) movedState.makeMove(move, turn) if state.gameOver(): if state.winState() == turn: results.append(1) elif state.winState() == 0: results.append(0) else: results.append(-1) else: results.append(-self.result(movedState, turn % 2 + 1, depth - 0.5)) return max(results) if __name__ == "__main__": b = Breadth() s = State() s.makeMove(3, 1) s.makeMove(3, 1) #s.makeMove(3, 1) s.makeMove(4, 2) s.makeMove(4, 2) s.makeMove(4, 2) print b.move(s, 1)