import random rankValue = {"Two": 2, "Three": 3, "Four": 4, "Five": 5, "Six": 6, "Seven": 7, "Eight": 8, "Nine": 9, "Ten": 10, "Jack": 10, "Queen": 10, "King": 10, "Ace": 11} class Card: def __init__(self, n, s): self.suit = s self.rank = n self.value = rankValue[n] def __str__(self): return self.rank + " of " + self.suit class Hand: def __init__(self): self.cards = [] self.bet = 0 def __str__(self): return ", ".join([i.__str__() for i in self.cards]) def score(self): s = [0] for c in self.cards: for i in range(len(s)): s[i] += c.value if c.rank == 'Ace': s.append(s[-1] - 10) if any(i < 21 for i in s): return max([i for i in s if i <= 21]) return max(s) def hit(self, c): self.cards.append(c) class Deck: def __init__(self): self.reset() self.shuffle() def reset(self): self.cards = [Card(n, s) for n in rankValue.keys() for s in ["Spades", "Hearts", "Diamonds", "Clubs"]] def shuffle(self): random.shuffle(self.cards) def deal(self): temp = self.cards[-1] del(self.cards[-1]) return temp class Player: def __init__(self, n): self.money = 100 self.name = n self.hands = [Hand()] def __str__(self): return self.name + " ($" + str(self.money) + ")" def currentBet(self): return sum([h.bet for h in self.hands]) players = [Player(raw_input("Player " + str(i + 1) + "'s name: ")) for i in range(input("How many players?\n"))] while True: d = Deck() dealer = Hand() for i in range(len(players)): if players[i].money < 10: print players[i].name, "is out of money, and is removed from the game." players = [players[i] for i in range(len(players)) if players[i].money > 9] if len(players) == 0: print "Dealer wins!" break for i in range(2): dealer.hit(d.deal()) for p in players: p.hands = [Hand()] for i in range(2): p.hands[0].hit(d.deal()) p.hands[0].bet = max(min(input("How much would you like to bet, " + p.name + "? (min $10) you have $" + str(p.money) + "\n"), p.money), 10) p.money -= p.hands[0].bet print "The dealer has a", dealer.cards[-1], "showing.\n" for p in players: print p, "Current bet:", p.currentBet() print "Would you like to surrender? (yes,no)" if(raw_input() == "yes"): print "You get", p.currentBet() / 2, "back" p.money += p.currentBet() / 2 continue for h in p.hands: while h.score() < 22: print p.name + "'s hand: " + str(h) if h.score() == 21 and len(p.hands) == 1 and len(h.cards) == 2: if raw_input("BLACKJACK!\n would you like even money? (yes,no)\n") == "yes": p.money += p.currentBet() * 2 print "You take your", p.currentBet()*2,"and run" break print "Would", p.name, "like to:", "split," if h.cards[0].rank == h.cards[1].rank and len(h.cards) == 2 and p.money > h.bet else "", "double down," if len(h.cards) == 2 and p.money > h.bet else "", "hit, or stay" choice = raw_input() if choice == "double down": h.hit(d.deal()) p.money -= h.bet h.bet *= 2 break elif choice == "hit": h.hit(d.deal()) elif choice == "split": p.hands.append(Hand()) p.hands[-1].cards.append(h.cards[1]) del(h.cards[1]) h.hit(d.deal()) p.hands[-1].hit(d.deal()) p.hands[-1].bet = h.bet p.money -= h.bet else: break print "Final hand:", h print "bust!\n" if h.score() > 21 else "" print "Dealer reveals a", dealer.cards[0] if dealer.score() == 17 and (dealer.cards[0].rank == "Ace" or dealer.cards[1].rank == "Ace"): dealer.hit(d.deal()) print "Dealer hits on a soft 17, and gets a", dealer.cards[-1] while dealer.score() < 17: dealer.hit(d.deal()) print "Dealer hits, and gets a", dealer.cards[-1] print "Dealer's final hand:", dealer if dealer.score() > 21: print "Dealer busted" for p in players: winnings = 0 for h in p.hands: if h.score() == 21 and dealer.score() != 21 and len(h.cards) == 2 and len(p.hands) == 1: winnings += h.bet * 2.5 elif h.score() < 22 and dealer.score() > 21 or h.score() > dealer.score() and h.score() < 22: winnings += h.bet * 2 elif h.score() == dealer.score(): winnings += h.bet if winnings > 0: print p.name, "wins", winnings - p.currentBet(), "dollars." p.money += winnings else: print p.name, "loses", p.currentBet() - winnings # dealer has soft 17, hits and busts # player has ace ace splits to get ace jack stay, ace ace, splits to get ace 8 stay, ace three double down get 2 # dealer has ace showing, but no blackjack, player takes even money