def vopt(pred, trick, depth): if depth == 0: return utility(pred, trick) score = 0 turn = (players.index(trick.play_order[-1]) + 1) % whist.NUM_PLAYERS # first we calculate the score if possible if not trick.left_to_play: # all players have played if game_state.are_partners(self.name, trick.winning_player().name): score = 1 else: score = -1 turn = players.index(trick.winning_player()) if not players[turn].cards: # game complete return score trick = Trick(players, game_state.trump) depth = depth - 1 cards = util.get_legal_cards(pred.predict(players[turn]), trick.suit_led) cards = cards if len(cards) < self.breadth else random.sample(cards, self.breadth) plays = [] # next we attempt to play cards if not cards: # this branch cannot be satisified, prune it return 0 for card in cards: trick.play_card(players[turn], card) pred.try_play(players[turn], card) plays.append(score + vopt(pred, trick, depth)) pred.revert_play(players[turn], card) trick.revert_play(players[turn], card) # finally we return the highest score if game_state.are_partners(self.name, players[turn].name): # max return max(plays) else: # min return min(plays)
def vopt(hands, trick, depth): if depth == 0: return utility(hands, trick) score = 0 turn = (players.index(trick.play_order[-1]) + 1) % len(players) # first we calculate the score if possible if not trick.left_to_play: # all players have played if game_state.are_partners(player.name, trick.winning_player().name): score = 1 else: score = -1 turn = players.index(trick.winning_player()) if not players[turn].cards: # game complete return score trick = Trick(players, game_state.trump) depth = depth - 1 # QUESTION: Does get_legal_cards also consider trump suite? How does human_player consider trump? cards = util.get_legal_cards(hands[players[turn].name], trick.suit_led) plays = [] # next we attempt to play cards if not cards: # this branch cannot be satisified, prune it return 0 for card in set(cards): trick.play_card(players[turn], card) hands[players[turn].name].remove(card) plays.append(score + vopt(hands, trick, depth)) hands[players[turn].name].append(card) trick.revert_play(players[turn], card) # finally we return the highest score if game_state.are_partners(player.name, players[turn].name): # max return max(plays) else: # min return min(plays)