Advertisement
Guest User

Blackjack Checker

a guest
Jul 8th, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. def read_player_hands():
  2.     player_hands = []
  3.     lines = open('input').readlines()
  4.     for line in lines:
  5.         tokens = line.split(": ")
  6.         name = tokens[0]
  7.         cards = map(lambda s : s.split(" ")[0], tokens[1].split(", "))
  8.         player_hands.append((name, cards))
  9.     return player_hands
  10.  
  11. def eval_hand(hand):
  12.     card_values = {
  13.         'Two'   : 2,
  14.         'Three' : 3,
  15.         'Four'  : 4,
  16.         'Five'  : 5,
  17.         'Six'   : 6,
  18.         'Seven' : 7,
  19.         'Eight' : 8,
  20.         'Nine'  : 9,
  21.         'Ten'   : 10,
  22.         'Jack'  : 10,
  23.         'Queen' : 10,
  24.         'King'  : 10 }
  25.     val = 0
  26.     aces = 0
  27.     for card in hand:
  28.         if card == 'Ace':
  29.             aces += 1
  30.         else:
  31.             val += card_values[card]
  32.     if aces > 0:
  33.         aces -= 1
  34.         val += aces
  35.         if aces > 0:
  36.             if val + 11 <= 21:
  37.                 val += 11
  38.             else:
  39.                 val += 1
  40.  
  41.     if val <= 21 and len(hand) >= 5:
  42.         return 22
  43.     elif val > 21:
  44.         return 0
  45.     else:
  46.         return val
  47.  
  48. def get_winner(hand_values):
  49.     winning_val = max(map(lambda v : v[2], hand_values))
  50.     if winning_val == 0:
  51.         print("Everyone busts.")
  52.     else:
  53.         winners = [hand_val for hand_val in hand_values if hand_val[2] == winning_val]
  54.         if len(winners) == 1:
  55.             if winning_val == 22:
  56.                 print("%s wins with the 5 card trick." % winners[0][0])
  57.             else:
  58.                 print("%s wins." % winners[0][0])
  59.         else:
  60.             print("Draw between %s." % " and ".join(map(lambda v : v[0], winners)))
  61.  
  62. def play():
  63.     hand_values = []
  64.     for hand in read_player_hands():
  65.         hand_values.append((hand[0], hand[1], eval_hand(hand[1])))
  66.     get_winner(hand_values)
  67.  
  68. play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement