Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. """Blackjack."""
  2. import importlib
  3. import os
  4. import pkgutil
  5. import random
  6. from deck import Deck, Card
  7. from game_view import GameView, FancyView, Move
  8. from strategy import Strategy, HumanStrategy
  9.  
  10.  
  11. class ValueError(Exception):
  12. """ValueError."""
  13. pass
  14.  
  15.  
  16. class Hand:
  17. """Hand."""
  18.  
  19. def __init__(self, cards: list = None):
  20. """Init."""
  21. if cards is None:
  22. self.cards = []
  23. else:
  24. self.cards = cards
  25. self.is_double_down = False
  26. self.is_surrendered = False
  27.  
  28. def add_card(self, card: Card) -> None:
  29. """Add card to hand."""
  30. self.cards.append(card)
  31.  
  32. def double_down(self, card: Card) -> None:
  33. """Double down."""
  34. self.cards.append(card)
  35. self.is_double_down = True
  36.  
  37. def split(self):
  38. """Split hand."""
  39. if self.can_split is True:
  40. one_hand = Hand([self.cards[0]])
  41. self.cards.remove(self.cards[0]) # remove sest split teeb 1 handist 2 handi
  42. return one_hand
  43. else:
  44. raise ValueError("Invalid hand to split!")
  45.  
  46. @property
  47. def can_split(self) -> bool:
  48. """Check if hand can be split."""
  49. if self.cards[0].value == self.cards[1].value and len(self.cards) == 2:
  50. return True
  51. return False
  52.  
  53. @property
  54. def is_blackjack(self) -> bool:
  55. """Check if is blackjack"""
  56. if len(self.cards) == 2 and self.score == 21:
  57. return True
  58. return False
  59.  
  60. @property
  61. def is_soft_hand(self):
  62. """Check if is soft hand."""
  63. if self.cards[0].value == "ACE" or self.cards[1] == "ACE":
  64. return True
  65. return False
  66.  
  67. @property
  68. def score(self) -> int:
  69. """Get score of hand."""
  70. score = 0
  71. for i in self.cards:
  72. if i.value in ["JACK", "QUEEN", "KING"]:
  73. score += 10
  74. elif i.value not in ["JACK", "QUEEN", "KING", "ACE"]:
  75. score += int(card.value)
  76. aces = 0
  77. for i in self.cards:
  78. if i == "ACE":
  79. aces += 1
  80. if aces == 0:
  81. score += 0
  82. elif aces == 1:
  83. if score + 11 <= 21:
  84. score += 11
  85. else:
  86. score += 1
  87. else:
  88. if score + 11 + aces <= 21:
  89. score += 11 + aces
  90. else:
  91. score += aces
  92. return score
  93.  
  94.  
  95. class Player:
  96. """Player."""
  97.  
  98. def __init__(self, name: str, strategy: Strategy, coins: int = 100):
  99. """Init."""
  100. pass
  101.  
  102. def join_table(self):
  103. """Join table."""
  104. pass
  105.  
  106. def play_move(self, hand: Hand) -> Move:
  107. """Play move."""
  108. pass
  109.  
  110. def split_hand(self, hand: Hand) -> None:
  111. """Split hand."""
  112. pass
  113.  
  114.  
  115. class GameController:
  116. """Game controller."""
  117.  
  118. PLAYER_START_COINS = 200
  119. BUY_IN_COST = 10
  120.  
  121. def __init__(self, view: GameView):
  122. """Init."""
  123. pass
  124.  
  125. def start_game(self) -> None:
  126. """Start game."""
  127. pass
  128.  
  129. def play_round(self) -> bool:
  130. """Play round."""
  131. pass
  132.  
  133. def _draw_card(self, top_down: bool = False) -> Card:
  134. """Draw card."""
  135.  
  136. @staticmethod
  137. def load_strategies() -> list:
  138. """
  139. Load strategies.
  140. @:return list of strategies that are in same package.
  141. DO NOT EDIT!
  142. """
  143. pkg_dir = os.path.dirname(__file__)
  144. for (module_loader, name, is_pkg) in pkgutil.iter_modules([pkg_dir]):
  145. importlib.import_module(name)
  146. return list(filter(lambda x: x.__name__ != HumanStrategy.__name__, Strategy.__subclasses__()))
  147.  
  148.  
  149. if __name__ == '__main__':
  150. game_controller = GameController(FancyView())
  151. game_controller.start_game()
  152. while game_controller.play_round():
  153. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement