Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. class AutoPlayer(Strategy):
  2. """My strategy."""
  3.  
  4. def __init__(self, other_players: list, house, decks_count, view: GameView):
  5. """Init."""
  6. super().__init__(other_players, house, decks_count)
  7. self.view = view
  8.  
  9. def play_move(self, hand) -> Move:
  10. """Get next move."""
  11. matrix_data = {"H": Move.HIT, "S": Move.STAND, "D": Move.DOUBLE_DOWN, "P": Move.SPLIT, "R": Move.SURRENDER}
  12. if hand.can_split:
  13. result = self.split_hand(hand, matrix_data)
  14. elif hand.is_soft_hand and len(hand.cards) == 2:
  15. result = self.soft_hand(hand, matrix_data)
  16. else:
  17. result = self.hard_hand(hand, matrix_data)
  18. return result
  19.  
  20. def hard_hand(self, hand, matrix_data):
  21. my_hand = {9: 0, 10: 1, 11: 2, 12: 3, 13: 4, 14: 5, 15: 6, 16: 7}
  22. hard_hand_matrix = [["H", "D", "D", "D", "D", "H", "H", "H", "H", "H"],
  23. ["D", "D", "D", "D", "D", "D", "D", "D", "H", "H"],
  24. ["D", "D", "D", "D", "D", "D", "D", "D", "D", "H"],
  25. ["H", "H", "S", "S", "S", "H", "H", "H", "H", "H"],
  26. ["S", "S", "S", "S", "S", "H", "H", "H", "H", "H"],
  27. ["S", "S", "S", "S", "S", "H", "H", "H", "H", "H"],
  28. ["S", "S", "S", "S", "S", "H", "H", "H", "R", "H"],
  29. ["S", "S", "S", "S", "S", "H", "H", "R", "R", "R"]]
  30. if hand.score <= 8:
  31. return Move.HIT
  32. elif hand.score >= 17:
  33. return Move.STAND
  34. else:
  35. column = my_hand[hand.score]
  36. row = self.find_house_card() - 2
  37. result = hard_hand_matrix[column][row]
  38. if result == "R" and len(hand.cards) > 2:
  39. return Move.HIT
  40. return matrix_data[result]
  41.  
  42. def soft_hand(self, hand, matrix_data):
  43. if hand.score >= 19:
  44. return Move.STAND
  45. my_hand = {13: 0, 14: 1, 15: 2, 16: 3, 17: 4, 18: 5}
  46. soft_hand_matrix = [["H", "H", "H", "D", "D", "H", "H", "H", "H", "H"],
  47. ["H", "H", "H", "D", "D", "H", "H", "H", "H", "H"],
  48. ["H", "H", "D", "D", "D", "H", "H", "H", "H", "H"],
  49. ["H", "H", "D", "D", "D", "H", "H", "H", "H", "H"],
  50. ["H", "D", "D", "D", "D", "H", "H", "H", "H", "H"],
  51. ["S", "D", "D", "D", "D", "S", "S", "H", "H", "H"]]
  52. column = my_hand[hand.score]
  53. row = self.find_house_card() - 2
  54. result = soft_hand_matrix[column][row]
  55. return matrix_data[result]
  56.  
  57. def split_hand(self, hand, matrix_data):
  58. if hand.is_soft_hand:
  59. return Move.SPLIT
  60. elif hand.score == 20:
  61. return Move.STAND
  62. else:
  63. my_hand = {4: 0, 6: 1, 8: 2, 10: 3, 12: 4, 14: 5, 16: 6, 18: 7}
  64. split_matrix = [["P", "P", "P", "P", "P", "P", "H", "H", "H", "H"],
  65. ["P", "P", "P", "P", "P", "P", "H", "H", "H", "H"],
  66. ["H", "H", "H", "P", "P", "H", "H", "H", "H", "H"],
  67. ["D", "D", "D", "D", "D", "D", "D", "D", "H", "H"],
  68. ["P", "P", "P", "P", "P", "H", "H", "H", "H", "H"],
  69. ["P", "P", "P", "P", "P", "P", "H", "H", "H", "H"],
  70. ["P", "P", "P", "P", "P", "P", "P", "P", "P", "P"],
  71. ["P", "P", "P", "P", "P", "S", "P", "P", "S", "S"]]
  72. column = my_hand[hand.score]
  73. row = self.find_house_card() - 2
  74. result = split_matrix[column][row]
  75. return matrix_data[result]
  76.  
  77. def find_house_card(self):
  78. house_cards = [card.__str__() for card in self.house.cards]
  79. house_card_value = house_cards[0] if house_cards[0] != "??" else house_cards[1]
  80. house_card_value = house_card_value[0]
  81. values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 10,
  82. 'J': 10, 'Q': 10, 'K': 10, 'A': 11}
  83. return values[house_card_value]
  84.  
  85. def on_card_drawn(self, card) -> None:
  86. """Called every time card is drawn."""
  87.  
  88. def on_game_end(self) -> None:
  89. """Called on game end."""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement