Created
February 16, 2012 04:49
-
-
Save anonymous/1842131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python | |
# MacPorts version of python | |
# Luke Thomas Mergner February 10, 2012 | |
from __future__ import division | |
import random, math, sys, os | |
def deal(): | |
"""deal a card from 1 to 52 and return it's points""" | |
return getValue(int(math.floor(random.uniform(1, 52)))) | |
def hitMe(hand): | |
"""Appends another card to the hand list""" | |
return hand.append(deal()) | |
def showHand(h): | |
"""Takes a list of cards and prints them.""" | |
print "\nYour cards are: ", | |
for i in iter(h): | |
print i, | |
if score(h) == 21: | |
print "\nBlackjack!" | |
return False | |
elif score(h) < 22: | |
print "\nYour score is: %s " % score(h) | |
return True | |
else: | |
print "\nYour score of %s is over 21!" % score(h) | |
return False | |
def showDealer(h): | |
print "\nThe dealer is showing: %s" % h[0] #Show only the first dealer card | |
def getValue(card): | |
"""Converts the values 1 - 52 into a 1 - 13 and returns the correct blackjack score based on remainder.""" | |
if (card % 13 == 0 or card % 13 == 11 or card % 13 == 12): | |
#Face Cards are 10 points | |
return 10 | |
elif (card % 13 == 1): | |
return 11 | |
else: | |
#Regular cards, return their value | |
return card % 13 | |
def again(string): | |
'''Return True for 'y'; False for all other cases.''' | |
answer = raw_input(string).lower() | |
#print "answer is " + answer | |
if answer[:1] == "y": | |
#print "Control question is True." | |
return True | |
else: | |
#print "Control question is False." | |
return False | |
def score(h): | |
return sum(checkForAces(h)) | |
def checkForAces(h): | |
for i in h: | |
if i is 11 and sum(h) > 21: | |
ace = raw_input("Is your ace worth 1 or 11? ") | |
try: | |
h[i] = int(ace) | |
except: | |
print "Cannot convert ace input into an integer." | |
return h | |
def ante(bank): | |
bet = None | |
while not checkAnte(bet, bank): | |
try: | |
bet = float(raw_input("What would you like to bet? Please enter a number. ")) | |
except: | |
print "Cannot convert bet into a floating decimal." | |
return bet | |
def checkAnte(b, bank): | |
#Make sure raw input is correct type and within our range | |
if bank > b > 0: | |
return True | |
else: | |
return False | |
def payout(h,d,bet): | |
if score(h) < 21 and score(d) > 21 or score(d) < score(h): | |
print "\nYou beat the house: %s to %s" % (score(h), score(d)) | |
return 3 * bet / 2 | |
else: | |
print "\nThe House always wins." | |
return -bet | |
def main(): | |
#Define our initial bankroll | |
bankroll = float(100) | |
while True: | |
os.system("clear") | |
print "=" * 27 | |
print ' ' * 9, | |
print 'BLACKJACK' | |
print '=' * 27 | |
if bankroll <= 0: | |
print "You are out of money. Please open a line of credit at our window." | |
sys.exit() | |
print "\nThis table is $5 per hand and pays 3:2. Your current bankroll is %.2f." % bankroll | |
bet = ante(bankroll) | |
#Deal the initial hand | |
hand = [deal(), deal()] | |
dealer = [deal(), deal()] | |
showDealer(dealer) | |
#Show me my hand once | |
showHand(hand) | |
#Deal more cards | |
while again("\nHit or Stand? ") and showHand(hand): | |
hitMe(hand) | |
#Dealer control | |
while score(dealer) < 17: | |
hitMe(dealer) | |
print score(dealer) | |
bankroll = bankroll + payout(hand, dealer, bet) | |
#Control loop variable for game. | |
if again("\nDo you want to play again?"): | |
pass | |
else: | |
print "That's it!" | |
sys.exit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment