Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle
- import os
- def shuffled_shoe():
- shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
- shuffle(shoe)
- return shoe
- def deal_card(shoe, person, number):
- for _ in range(number):
- person.append(shoe.pop())
- def deal_hand(shoe, player, dealer):
- deal_card(shoe, player, 2)
- deal_card(shoe, dealer, 2)
- def score(person):
- non_aces = [c for c in person if c != 'A']
- aces = [c for c in person if c == 'A']
- total = 0
- for card in non_aces:
- if card in 'JQK':
- total += 10
- else:
- total += int(card)
- for card in aces:
- if total <= 10:
- total += 11
- else:
- total += 1
- return total
- def display_info(player, dealer, player_stands):
- os.system('cls' if os.name == 'nt' else 'clear')
- print("Your cards [{}] ({})".format("][".join(player), score(player)))
- if player_stands:
- print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
- else:
- print(f"Dealer cards: [{dealer[0]}] [?]")
- def hit_or_stand():
- while True:
- print("What do you choose?")
- print("[1] Hit")
- print("[2] Stand")
- ans = input("> ")
- if ans in '12':
- return ans
- # ------------BUG-ZONE-------------
- def player_play(shoe, player, dealer, player_stands, player_plays, dealer_plays):
- while not player_stands:
- if hit_or_stand() == '2':
- player_plays = False
- player_stands = True
- dealer_plays = True
- display_info(player, dealer, player_stands)
- elif not player_stands:
- deal_card(shoe, player, 1)
- display_info(player, dealer, player_stands)
- still_playing = (False if score(player) > 21 else True)
- results(player, dealer, player_stands, still_playing)
- return (player_stands, player_plays, dealer_plays)
- def dealer_play(shoe, dealer):
- while score(dealer) <= 16:
- deal_card(shoe, dealer, 1)
- return False
- def results(player, dealer, player_stands, still_playing):
- if score(player) == 21:
- print("Blackjack! You won")
- still_playing = False
- elif score(player) > 21:
- print("Busted! You lost!")
- still_playing = False
- elif player_stands:
- if score(dealer) > 21:
- print("Dealer busted! You won")
- elif score(player) > score(dealer):
- print("You beat the dealer! You won!")
- elif score(player) < score(dealer):
- print("Dealer has beaten you. You lost!")
- else:
- print("Push. Nobody wins or losses.")
- still_playing = False
- return still_playing
- def main():
- shoe = shuffled_shoe()
- player = []
- dealer = []
- player_plays = True
- still_playing = True
- player_stands = False
- dealer_plays = False
- deal_hand(shoe, player, dealer)
- while still_playing:
- display_info(player, dealer, player_stands)
- still_playing = results(player, dealer, player_stands, still_playing)
- while player_plays:
- (player_stands, player_plays, dealer_plays) = player_play(shoe, player, dealer, player_stands, player_plays, dealer_plays)
- while dealer_plays:
- dealer_plays = dealer_play(shoe, dealer)
- still_playing = results(player, dealer, player_stands, still_playing)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement