Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Blackjack."""
- import importlib
- import os
- import pkgutil
- import random
- from deck import Deck, Card
- from game_view import GameView, FancyView, Move
- from strategy import Strategy, HumanStrategy
- class ValueError(Exception):
- """ValueError."""
- pass
- class Hand:
- """Hand."""
- def __init__(self, cards: list = None):
- """Init."""
- if cards is None:
- self.cards = []
- else:
- self.cards = cards
- self.is_double_down = False
- self.is_surrendered = False
- def add_card(self, card: Card) -> None:
- """Add card to hand."""
- self.cards.append(card)
- def double_down(self, card: Card) -> None:
- """Double down."""
- self.cards.append(card)
- self.is_double_down = True
- def split(self):
- """Split hand."""
- if self.can_split is True:
- one_hand = Hand([self.cards[0]])
- self.cards.remove(self.cards[0]) # remove sest split teeb 1 handist 2 handi
- return one_hand
- else:
- raise ValueError("Invalid hand to split!")
- @property
- def can_split(self) -> bool:
- """Check if hand can be split."""
- if self.cards[0].value == self.cards[1].value and len(self.cards) == 2:
- return True
- return False
- @property
- def is_blackjack(self) -> bool:
- """Check if is blackjack"""
- if len(self.cards) == 2 and self.score == 21:
- return True
- return False
- @property
- def is_soft_hand(self):
- """Check if is soft hand."""
- if self.cards[0].value == "ACE" or self.cards[1] == "ACE":
- return True
- return False
- @property
- def score(self) -> int:
- """Get score of hand."""
- score = 0
- for i in self.cards:
- if i.value in ["JACK", "QUEEN", "KING"]:
- score += 10
- elif i.value not in ["JACK", "QUEEN", "KING", "ACE"]:
- score += int(card.value)
- aces = 0
- for i in self.cards:
- if i == "ACE":
- aces += 1
- if aces == 0:
- score += 0
- elif aces == 1:
- if score + 11 <= 21:
- score += 11
- else:
- score += 1
- else:
- if score + 11 + aces <= 21:
- score += 11 + aces
- else:
- score += aces
- return score
- class Player:
- """Player."""
- def __init__(self, name: str, strategy: Strategy, coins: int = 100):
- """Init."""
- pass
- def join_table(self):
- """Join table."""
- pass
- def play_move(self, hand: Hand) -> Move:
- """Play move."""
- pass
- def split_hand(self, hand: Hand) -> None:
- """Split hand."""
- pass
- class GameController:
- """Game controller."""
- PLAYER_START_COINS = 200
- BUY_IN_COST = 10
- def __init__(self, view: GameView):
- """Init."""
- pass
- def start_game(self) -> None:
- """Start game."""
- pass
- def play_round(self) -> bool:
- """Play round."""
- pass
- def _draw_card(self, top_down: bool = False) -> Card:
- """Draw card."""
- @staticmethod
- def load_strategies() -> list:
- """
- Load strategies.
- @:return list of strategies that are in same package.
- DO NOT EDIT!
- """
- pkg_dir = os.path.dirname(__file__)
- for (module_loader, name, is_pkg) in pkgutil.iter_modules([pkg_dir]):
- importlib.import_module(name)
- return list(filter(lambda x: x.__name__ != HumanStrategy.__name__, Strategy.__subclasses__()))
- if __name__ == '__main__':
- game_controller = GameController(FancyView())
- game_controller.start_game()
- while game_controller.play_round():
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement