I
I
Ivan2021-06-05 20:25:06
Python
Ivan, 2021-06-05 20:25:06

How to improve the code?

Beginner coder, decided to make a simple blackjack.
You can point out obvious errors in the design of the code.
And how would you design this or that piece.
PS I know that the class here is useless, I just used it for training

import random


class BlackJack:
  def __init__(self):
    self.suits = ['♠', '♣', '♥', '♦']
    self.faces = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'T']
    self.value = {2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10, 'J':10, 'Q':10, 'K':10, 'T':11}
    self.deck = [] # Колода
    self.hand = [] # Карты в руке
    for self.suit in self.suits:
      for self.face in self.faces:
        self.deck.append((self.suit, self.face))



  def hit(func):
    def wrapper(self):
      self.card = random.choice(self.deck)
      self.deck.remove(self.card)
      self.hand.append(self.card)
      self.b_hand = [] #Красивое отображение карт
      for x in range(len(self.hand)):
        for self.y in self.hand[x]:
          self.b_hand.append(self.y)
      func(self)
    return wrapper
          
  @hit
  @hit
  def gamer(self):
    self.result_gamer = self.value.get(self.hand[0][1]) + self.value.get(self.hand[1][1])
    print(f'Ваши карты {self.b_hand} Cчет: {self.result_gamer}', end='')
    if self.result_gamer == 21 or self.result_gamer == 22:
      #print(f'Ваши карты {self.b_hand}', end='')
      print('\nУ вас BlackJack\n')
    else:
      while True:
        self.answer = input('\nНужна еще карта? Y/N ').lower()
        if self.answer == 'y' or self.answer == 'yes' or self.answer == 'н':
          self.card = random.choice(self.deck)
          self.deck.remove(self.card)
          self.hand.append(self.card)
          self.b_hand = []
          for x in range(len(self.hand)):
            for self.y in self.hand[x]:
              self.b_hand.append(self.y)
          self.result_gamer += self.value.get(self.hand[-1][-1])
          if self.result_gamer > 21:
            print(f'Ваши карты {self.b_hand} \nВаш счет {self.result_gamer}', end='')
            print('\nПеребор')
            break
          elif self.result_gamer == 21:
            print(f'Ваши карты {self.b_hand} Cчет: {self.result_gamer}', end='')
            print('\n У вас BlackJack')
          else:
            print(f'Ваши карты {self.b_hand} \nВаш счет {self.result_gamer}', end='')
            continue

            self.hand.clear()
        elif self.answer == 'n' or self.answer == 'no' or self.answer == 'т':
          self.hand.clear()
          self.croupier()
          break


  @hit
  @hit
  def croupier(self):
    print(f'Карты курпье {self.b_hand}', end='')
    self.result_croupier = self.value.get(self.hand[0][1]) + self.value.get(self.hand[1][1])
    while True:
      if self.result_croupier > self.result_gamer and self.result_croupier <= 21:
        print(f'Cчет: {self.result_croupier} \nВы проиграли\n', end='')
        break
      else:
        if self.result_croupier == self.result_gamer and self.result_croupier >= 11:
          print(f'Cчет: {self.result_croupier} \nНичья\n', end='')
          break
        else:	
          self.card = random.choice(self.deck)
          self.deck.remove(self.card)
          self.hand.append(self.card)
          self.b_hand = []
          for x in range(len(self.hand)):
            for self.y in self.hand[x]:
              self.b_hand.append(self.y)
          self.result_croupier += self.value.get(self.hand[-1][-1])
          print('Крупье добирает карту')
          #print(self.b_hand)

          if self.result_croupier > self.result_gamer and self.result_croupier <= 21:
            print(f'Карты курпье {self.b_hand} Cчет: {self.result_croupier} \nВы проиграли\n', end='')
            break
          elif self.result_croupier == self.result_gamer:
            print(f'Карты курпье {self.b_hand}', end='')
            print(' Cчет: ', self.result_croupier)
            print('Ничья')
            break
          elif self.result_croupier > 21:
            print(f'Карты курпье {self.b_hand} Cчет: {self.result_croupier} \nВы победили\n', end='')
            break
          else:
            continue
      


while True:
  game = BlackJack()
  game.gamer()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yupiter7575, 2021-06-05
@yupiter7575

The function calls the function created inside the ._ function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question