В настоящее время я работаю над текстовой игрой в блэкджек на Python для задания курса, но когда я запускаю этот код:
import assets
def show_hands(pl, dl):
print('\n' + pl.__str__())
print(f'Total: {pl.evaluate_hand()}')
print(dl.hidden_card() + '\n')
def ask_for_bet(money):
while True:
try:
bet = int(input(f'Your current amount of money is {money}, how much would you like to bet? (50 or up): '))
except ValueError:
print('That is not a number!')
else:
while bet > money or bet < 50:
if bet > money:
print("You don't have that much money!")
break
else:
print("Your bet is too low! (50 or up)")
break
else:
break
print('Alright! The game is on!')
return bet
if __name__ == '__main__':
print("-_-_-_-_-_-_-_-_-_Welcome to Omar's game of BlackJack!-_-_-_-_-_-_-_-_-_")
print('\n')
# Creating instances of both the player and the dealer, initializing the player's money
current_money = 500
player = assets.Player()
dealer = assets.Dealer()
while current_money >= 50:
# Ask for the player's bet
player_bet = ask_for_bet(current_money)
# Deal two cards to the player
print('\n' + 'The cards are being dealt...')
player.add_card(dealer.deal_cards(2))
# Show both the hand of the player and the dealer (one face down card)
show_hands(player, dealer)
# Continuously ask for the player's decision (hit/stand) as long as he chooses to hit and doesn't bust
continue_hitting = True
while continue_hitting and player.evaluate_hand() <= 21:
hit_or_stand = input('Do you want to hit or stand? (hit/stand): ').lower()
while hit_or_stand not in ('hit', 'stand'):
hit_or_stand = input('PLease input a correct value (hit/stand): ').lower()
else:
if hit_or_stand == 'stand':
continue_hitting = False
else:
player.add_card(dealer.deal_cards(1))
show_hands(player, dealer)
else:
if player.evaluate_hand() > 21:
print('You have busted!')
# Reveal the hand of the dealer and compare it with the player's
print(dealer)
и вот модуль ресурсов:
import random
# -------------------------------------------------------------------------
class Player:
def __init__(self):
self.hand = []
def add_card(self, card_to_add):
self.hand += card_to_add
def evaluate_ace(self):
ace_value = input("Choose your ace's value (1/11): ")
while ace_value not in ('1', '11'):
ace_value = input("Input a correct value (1/11): ")
return int(ace_value)
def evaluate_hand(self):
hand_value = 0
for rank, suit in self.hand:
if rank.isalpha():
if rank in ('jack', 'king', 'queen'):
hand_value += 10
else:
hand_value += self.evaluate_ace()
else:
hand_value += int(rank)
return hand_value
def __str__(self):
hand_str = 'Your current hand is: '
for rank, suit in self.hand:
hand_str += f'({rank} of {suit})'
return hand_str
# -------------------------------------------------------------------------
class Dealer(Player):
suits = ['hearts', 'spades', 'diamonds', 'clubs']
ranks = ['king', 'queen', 'jack', 'ace', '2', '3', '4', '5', '6', '7', '8', '9', '10']
def __init__(self):
Player.__init__(self)
self.deck = {}
for suit in self.suits:
ranks_copy = self.ranks.copy()
random.shuffle(ranks_copy)
self.deck[suit] = ranks_copy
self.add_card(self.deal_cards(2))
def deal_cards(self, num_of_cards):
dealt_cards = []
for x in range(num_of_cards):
rand_suit = self.suits[random.randint(0, 3)]
dealt_cards.append((self.deck[rand_suit].pop(), rand_suit))
return dealt_cards
def evaluate_hand(self):
hand_value = 0
for rank, suit in self.hand:
if rank.isalpha():
if rank in ('jack', 'king', 'queen'):
hand_value += 10
else:
if hand_value > 10:
hand_value += 1
else:
hand_value += 10
else:
hand_value += int(rank)
return hand_value
def __str__(self):
hand_str = "The dealer's current hand is: "
for rank, suit in self.hand:
hand_str += f'({rank} of {suit})'
return hand_str
def hidden_card(self):
hidden_card = "The dealer's current hand is: "
for rank, suit in self.hand:
if rank != 'ace':
hidden_card += f'({rank} of {suit}) (hidden card)'
break
return hidden_card
Он запрашивает значение туза (если игрок получает один) дважды, даже если он получает только один туз, и дает что-то вроде:
Я пробовал разные вещи, но до сих пор не могу найти ответ на эту проблему, и, честно говоря, это меня очень расстраивает.