+/IBCS
git clone https://git.julia.blue/+/IBCS
c0ce159
master
1import random 2 3rankValue = {"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 } 4 5 6class Card : 7 8def __init__ (self ,n ,s ): 9self .suit = s 10self .rank = n 11self .value = rankValue [n ] 12 13def __str__ (self ): 14return self .rank + " of " + self .suit 15 16 17class Hand : 18 19def __init__ (self ): 20self .cards = [] 21self .bet = 0 22 23def __str__ (self ): 24return ", " .join ([i .__str__ ()for i in self .cards ]) 25 26def score (self ): 27s = [0 ] 28for c in self .cards : 29for i in range (len (s )): 30s [i ]+= c .value 31if c .rank == 'Ace' : 32s .append (s [- 1 ]- 10 ) 33if any (i < 21 for i in s ): 34return max ([i for i in s if i <= 21 ]) 35return max (s ) 36 37def hit (self ,c ): 38self .cards .append (c ) 39 40 41class Deck : 42 43def __init__ (self ): 44self .reset () 45self .shuffle () 46 47def reset (self ): 48self .cards = [Card (n ,s )for n in rankValue .keys ()for s in ["Spades" ,"Hearts" ,"Diamonds" ,"Clubs" ]] 49 50def shuffle (self ): 51random .shuffle (self .cards ) 52 53def deal (self ): 54temp = self .cards [- 1 ] 55del (self .cards [- 1 ]) 56return temp 57 58 59class Player : 60 61def __init__ (self ,n ): 62self .money = 100 63self .name = n 64self .hands = [Hand ()] 65 66def __str__ (self ): 67return self .name + " ($" + str (self .money )+ ")" 68 69def currentBet (self ): 70return sum ([h .bet for h in self .hands ]) 71 72players = [Player (raw_input ("Player " + str (i + 1 )+ "'s name: " ))for i in range (input ("How many players?\n" ))] 73 74while True : 75d = Deck () 76dealer = Hand () 77for i in range (len (players )): 78if players [i ].money < 10 : 79players [i ].name ,"is out of money, and is removed from the game." 80players = [players [i ]for i in range (len (players ))if players [i ].money > 9 ] 81if len (players )== 0 : 82"Dealer wins!" 83break 84for i in range (2 ): 85dealer .hit (d .deal ()) 86for p in players : 87p .hands = [Hand ()] 88for i in range (2 ): 89p .hands [0 ].hit (d .deal ()) 90p .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 ) 91p .money -= p .hands [0 ].bet 92"The dealer has a" ,dealer .cards [- 1 ],"showing.\n" 93for p in players : 94p ,"Current bet:" ,p .currentBet () 95"Would you like to surrender? (yes,no)" 96if (raw_input ()== "yes" ): 97"You get" ,p .currentBet ()/ 2 ,"back" 98p .money += p .currentBet ()/ 2 99continue 100for h in p .hands : 101while h .score ()< 22 : 102p .name + "'s hand: " + str (h ) 103if h .score ()== 21 and len (p .hands )== 1 and len (h .cards )== 2 : 104if raw_input ("BLACKJACK!\n would you like even money? (yes,no)\n" )== "yes" : 105p .money += p .currentBet ()* 2 106"You take your" ,p .currentBet ()* 2 ,"and run" 107break 108"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" 109choice = raw_input () 110if choice == "double down" : 111h .hit (d .deal ()) 112p .money -= h .bet 113h .bet *= 2 114break 115elif choice == "hit" : 116h .hit (d .deal ()) 117elif choice == "split" : 118p .hands .append (Hand ()) 119p .hands [- 1 ].cards .append (h .cards [1 ]) 120del (h .cards [1 ]) 121h .hit (d .deal ()) 122p .hands [- 1 ].hit (d .deal ()) 123p .hands [- 1 ].bet = h .bet 124p .money -= h .bet 125else : 126break 127"Final hand:" ,h 128"bust!\n" if h .score ()> 21 else "" 129 130"Dealer reveals a" ,dealer .cards [0 ] 131if dealer .score ()== 17 and (dealer .cards [0 ].rank == "Ace" or dealer .cards [1 ].rank == "Ace" ): 132dealer .hit (d .deal ()) 133"Dealer hits on a soft 17, and gets a" ,dealer .cards [- 1 ] 134while dealer .score ()< 17 : 135dealer .hit (d .deal ()) 136"Dealer hits, and gets a" ,dealer .cards [- 1 ] 137"Dealer's final hand:" ,dealer 138if dealer .score ()> 21 : 139"Dealer busted" 140for p in players : 141winnings = 0 142for h in p .hands : 143if h .score ()== 21 and dealer .score ()!= 21 and len (h .cards )== 2 and len (p .hands )== 1 : 144winnings += h .bet * 2.5 145elif h .score ()< 22 and dealer .score ()> 21 or h .score ()> dealer .score ()and h .score ()< 22 : 146winnings += h .bet * 2 147elif h .score ()== dealer .score (): 148winnings += h .bet 149if winnings > 0 : 150p .name ,"wins" ,winnings - p .currentBet (),"dollars." 151p .money += winnings 152else : 153p .name ,"loses" ,p .currentBet ()- winnings 154 155# dealer has soft 17, hits and busts 156# player has ace ace splits to get ace jack stay, ace ace, splits to get ace 8 stay, ace three double down get 2 157 158# dealer has ace showing, but no blackjack, player takes even money