~piotr-machura/python-miniprojects

45b51da26027948199a9d55b47e6634741e25a95 — Piotr Machura 2 years ago 43e15d9
Working on the main game loop begins
3 files changed, 43 insertions(+), 12 deletions(-)

M blackjack/deck.py
M blackjack/main.py
M blackjack/player.py
M blackjack/deck.py => blackjack/deck.py +2 -2
@@ 44,14 44,14 @@ class Deck:
    """

    def __init__(self):
        self.cards = 4*[Card(symbol) for symbol, _ in Card.TYPES]
        self.cards = 4*[Card(symbol) for symbol in Card.TYPES]

    def __len__(self) -> int:
        return len(self.cards)

    def __repr__(self) -> str:
        str_repr = 'Deck:\n'
        for symbol, _ in Card.TYPES:
        for symbol in Card.TYPES:
            counter = self.cards.count(Card(symbol))
            if counter != 0:
                str_repr += f'[{symbol}]: {counter} '

M blackjack/main.py => blackjack/main.py +32 -0
@@ 0,0 1,32 @@
"""This is the `__main__` module."""
import os
import sys
from deck import Deck
from player import Dealer, Human

if __name__ == '__main__':
    deck = Deck()
    dealer = Dealer(deck)
    player = Human(deck, 1000)

    while True:
        os.system('cls' if os.name == 'nt' else 'clear')
        print('------------------------ BLACKJACK ------------------------', end='\n\n')
        print(dealer, end='\n\n')
        print(player, end='\n\n')
        print('------------')
        if player.calculate_points()[0] > 21:
            print('Busted!')
            sys.exit()
        print('[1] Hit [2] Stand [0] Quit ')
        while True:
            try:
                choice = int(input('Choose an option: '))
                break
            except ValueError:
                pass
        if choice == 0:
            print('Exiting...')
            sys.exit()
        elif choice == 1:
            player.hit(deck, 1)

M blackjack/player.py => blackjack/player.py +9 -10
@@ 2,6 2,7 @@
`Dealer` and `Human`.
"""
from deck import Card, Deck
from exception import BlackJackException


class Player:


@@ 66,38 67,36 @@ class Dealer(Player):
        self.hit(deck, 1)

    def hit_continuosly(self, deck: Deck, oponent: Player):
        """Hit until you have more points than `oponent` or until you exceed 21
        """Hit until you have more points than `oponent` or exceed 21.
        """
        while self.calculate_points() < 21:
            self.hit(deck, 1)
            points = self.calculate_points()
            oponent_points = oponent.calculate_points()
            if oponent_points < points <= 21:
                print(f'The {self.name} wins!')
                break
        else:
            print(f'The {oponent.name} wins!')
                raise BlackJackException(f'The {self.name} wins!')
        raise BlackJackException(f'The {oponent.name} wins!')


class Human(Player):
    """The Human class. Hits 2 cards when created. Extends `Player` with `bet`
    and `stand` methods
    and `stand` methods.
    """

    def __init__(self, deck: Deck, balance: int):
        super().__init__()
        self.name = 'HUMAN'
        self.name = 'PLAYER'
        self.balance = balance
        self.hit(deck, 2)

    def bet(self, amount: int) -> int:
        """Bet `amount` on winning the current hand."""
        if self.balance - amount < 0:
            print(f"Not enough money to bet ${amount}.")
            print(f'Not enough money to bet ${amount}.')
        else:
            self.balance -= amount
            print(f"Bet of ${amount} placed.")
        print(f"Current balance: ${self.balance}")
            print(f'Bet of ${amount} placed.')
        print(f'Current balance: ${self.balance}')

    def stand(self):
        """Do nothing."""