Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle
- #Creating deck and hands
- numbers = {1: "A", 11: "J", 12: "Q", 13: "K", 14: "A"}
- suits = ["♠", "♦", "♥", "♣"]
- deck = []
- for suit in suits:
- deck += [[x, suit] for x in range(2, 15)]
- shuffle(deck)
- player_hand, computer_hand = [], []
- players = {"player": player_hand, "computer": computer_hand}
- def draw_card(player):
- cards = players[player]
- cards.append(deck.pop())
- change_ace_if_needed(player)
- def value_of_cards(player):
- cards = players[player]
- count = sum([x[0] for x in cards if x[0] < 10]) #NORMAL CARDS
- count += sum([10 for x in cards if 10 <= x[0] < 14]) #PICTURED CARDS
- count += sum([11 for x in cards if x[0] == 14]) #ACES
- return count
- def show_hand(player):
- cards = players[player]
- print(f"{player.capitalize()}: ", end="")
- for card in cards:
- number, suit = card
- print(f"{numbers.get(number, number)}{suit}", end=" ")
- print(f" => {value_of_cards(player)}")
- def find_index_of_first_ace(cards):
- numbers = [x[0] for x in cards]
- if 14 in numbers:
- return numbers.index(14)
- return False
- def change_ace_if_needed(player):
- cards = players[player]
- if value_of_cards(player) > 21:
- index = find_index_of_first_ace(cards)
- if index:
- number, suit = cards[index]
- cards[index] = [1, suit]
- draw_card("player")
- draw_card("computer")
- #PLAYERS TURN
- player_action = "1"
- while value_of_cards("player") <= 21 and player_action == "1":
- draw_card("player")
- show_hand("player")
- show_hand("computer")
- player_action = input("1., Hit, 2., Stand\n")
- #COMPUTERS TURN
- while value_of_cards("computer") < 17:
- draw_card("computer")
- #EVALUATION
- if value_of_cards("computer") < value_of_cards("player") <= 21:
- print("You win")
- elif value_of_cards("player") <= 21 and value_of_cards("computer") > 21:
- print("You win")
- elif value_of_cards("player") == value_of_cards("computer") and value_of_cards("player") <= 21:
- print("Tie")
- else:
- print("You lost")
- show_hand("player")
- show_hand("computer")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement