/
sejeenn
/
python_basic
Обзор
Документация
Войти
/
sejeenn
/
python_basic
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Module24/08_blackjack/main.py
87 строк
3 KB
Eugene Vorontsov
Copy repo python basic
03 фев 2025, 20:23
03 фев 2025, 20:23
a8fce4a
Код
Авторство
О чём код?
import random class Card: def __init__(self, value, color): self.value = value self.color = color class Deck: def __init__(self): self.suit = ['heart', 'diamonds', 'spades', 'clubs'] self.rank = [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"] self.cards = [Card(rank, suit) for rank in self.rank for suit in self.suit] def get_cards(self): random.shuffle(self.cards) first_card = random.choice(self.cards) second_card = random.choice(self.cards) self.cards.remove(first_card) self.cards.remove(second_card) return [first_card.value, second_card.value] def get_new_card(self): random.shuffle(self.cards) new_card = random.choice(self.cards) self.cards.remove(new_card) return new_card.value class Player: def __init__(self, name, cards): self.name = name self.cards = cards def count_cards(some_cards): summa = 0 for card in some_cards: if card in ["Jack", "Queen", "King"]: summa += 10 elif card == "Ace": if summa + 11 <= 21: summa += 11 else: summa += 1 else: summa += card return summa deck = Deck() human = Player('Евгений', deck.get_cards()) computer = Player('Computer', deck.get_cards()) while True: print("\nВаши карты, {}: {} и их сумма:{}".format(human.name, human.cards, count_cards(human.cards))) if count_cards(human.cards) > 21 or count_cards(computer.cards) > 21: if count_cards(human.cards) > count_cards(computer.cards): print("{}, вы проиграли! У вас перебор: {}".format(human.name, count_cards(human.cards))) break else: print("{}, вы проиграли! Карты {}. У вас перебор: {}".format(computer.name, computer.cards, count_cards(computer.cards))) break choice = int(input(""" 1) Взять карту 2) Не брать карту Выберите действие: """)) if choice == 1: human.cards.append(deck.get_new_card()) computer.cards.append(deck.get_new_card()) elif choice == 2: if count_cards(human.cards) > count_cards(computer.cards): print("{}, вы выиграли! \nКарты {} и количество очков дилера: {}".format(human.name, computer.cards, count_cards(computer.cards))) elif count_cards(human.cards) == count_cards(computer.cards): print("Ничья! \nКарты {} и количество очков дилера: {}".format(computer.cards, count_cards(computer.cards))) else: print("{}, вы проиграли! \nКарты {} и количество очков дилера: {}".format(human.name, computer.cards, count_cards(computer.cards))) break