Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AutoPlayer(Strategy):
- """My strategy."""
- def __init__(self, other_players: list, house, decks_count, view: GameView):
- """Init."""
- super().__init__(other_players, house, decks_count)
- self.view = view
- def play_move(self, hand) -> Move:
- """Get next move."""
- matrix_data = {"H": Move.HIT, "S": Move.STAND, "D": Move.DOUBLE_DOWN, "P": Move.SPLIT, "R": Move.SURRENDER}
- if hand.can_split:
- result = self.split_hand(hand, matrix_data)
- elif hand.is_soft_hand and len(hand.cards) == 2:
- result = self.soft_hand(hand, matrix_data)
- else:
- result = self.hard_hand(hand, matrix_data)
- return result
- def hard_hand(self, hand, matrix_data):
- my_hand = {9: 0, 10: 1, 11: 2, 12: 3, 13: 4, 14: 5, 15: 6, 16: 7}
- hard_hand_matrix = [["H", "D", "D", "D", "D", "H", "H", "H", "H", "H"],
- ["D", "D", "D", "D", "D", "D", "D", "D", "H", "H"],
- ["D", "D", "D", "D", "D", "D", "D", "D", "D", "H"],
- ["H", "H", "S", "S", "S", "H", "H", "H", "H", "H"],
- ["S", "S", "S", "S", "S", "H", "H", "H", "H", "H"],
- ["S", "S", "S", "S", "S", "H", "H", "H", "H", "H"],
- ["S", "S", "S", "S", "S", "H", "H", "H", "R", "H"],
- ["S", "S", "S", "S", "S", "H", "H", "R", "R", "R"]]
- if hand.score <= 8:
- return Move.HIT
- elif hand.score >= 17:
- return Move.STAND
- else:
- column = my_hand[hand.score]
- row = self.find_house_card() - 2
- result = hard_hand_matrix[column][row]
- if result == "R" and len(hand.cards) > 2:
- return Move.HIT
- return matrix_data[result]
- def soft_hand(self, hand, matrix_data):
- if hand.score >= 19:
- return Move.STAND
- my_hand = {13: 0, 14: 1, 15: 2, 16: 3, 17: 4, 18: 5}
- soft_hand_matrix = [["H", "H", "H", "D", "D", "H", "H", "H", "H", "H"],
- ["H", "H", "H", "D", "D", "H", "H", "H", "H", "H"],
- ["H", "H", "D", "D", "D", "H", "H", "H", "H", "H"],
- ["H", "H", "D", "D", "D", "H", "H", "H", "H", "H"],
- ["H", "D", "D", "D", "D", "H", "H", "H", "H", "H"],
- ["S", "D", "D", "D", "D", "S", "S", "H", "H", "H"]]
- column = my_hand[hand.score]
- row = self.find_house_card() - 2
- result = soft_hand_matrix[column][row]
- return matrix_data[result]
- def split_hand(self, hand, matrix_data):
- if hand.is_soft_hand:
- return Move.SPLIT
- elif hand.score == 20:
- return Move.STAND
- else:
- my_hand = {4: 0, 6: 1, 8: 2, 10: 3, 12: 4, 14: 5, 16: 6, 18: 7}
- split_matrix = [["P", "P", "P", "P", "P", "P", "H", "H", "H", "H"],
- ["P", "P", "P", "P", "P", "P", "H", "H", "H", "H"],
- ["H", "H", "H", "P", "P", "H", "H", "H", "H", "H"],
- ["D", "D", "D", "D", "D", "D", "D", "D", "H", "H"],
- ["P", "P", "P", "P", "P", "H", "H", "H", "H", "H"],
- ["P", "P", "P", "P", "P", "P", "H", "H", "H", "H"],
- ["P", "P", "P", "P", "P", "P", "P", "P", "P", "P"],
- ["P", "P", "P", "P", "P", "S", "P", "P", "S", "S"]]
- column = my_hand[hand.score]
- row = self.find_house_card() - 2
- result = split_matrix[column][row]
- return matrix_data[result]
- def find_house_card(self):
- house_cards = [card.__str__() for card in self.house.cards]
- house_card_value = house_cards[0] if house_cards[0] != "??" else house_cards[1]
- house_card_value = house_card_value[0]
- values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 10,
- 'J': 10, 'Q': 10, 'K': 10, 'A': 11}
- return values[house_card_value]
- def on_card_drawn(self, card) -> None:
- """Called every time card is drawn."""
- def on_game_end(self) -> None:
- """Called on game end."""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement