Blackjack game error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nytgangsta101
    New Member
    • Apr 2010
    • 1

    Blackjack game error

    Im a novice in programming and i have these codes for a blackjack game.but it failed
    Code:
    from random import *
    from math import *
    
    #GLOBAL VARIABLES
    
    cards = range(0,52)
    
    def randRange(in_lower,in_upper):
    	""" generates a random number between in_lower and in_upper"""
    	temp_range = in_upper - in_lower
    	return int(round((temp_range)*random() + (in_lower)))
    
    
    def popRandArray(in_list):
    	return in_list.pop(randRange(0,len(in_list)-1))
    	
    def realDealCard():
    	
    	global cards
    	if len(cards)==0:
    		print "new deck"
    		cards = range(0,52)
    	return popRandArray(cards)
    
    
    def cardAsString(in_card):
    	
    	value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
    	suit = ["hearts","diamonds","spades","clubs"]
    	return value[in_card%13]+ " of " + suit[in_card/13]
    	
    	
    
    def cardScore(in_card):
    
    	score = in_card%13+1
    	if score > 10:
    		score = 10
    	return score
    from random import *
    from math import *
    
    #GLOBAL VARIABLES
    
    cards = range(0,52)
    
    def randRange(lower,upper):
    	"""return a value in the range in_lower to in_upper
    	inclusive
    	"""
    	temp_range = upper - lower
    	return int(round((temp_range+0.5)*random() + (lower - 0.5)))
    
    def dealCard():
    	"""simulate a deal from a hand of cards
    	"""
    	return randRange(0,51)
    
    def randArray(in_list):
    	return in_list[randRange(0,len(in_list)-1)]
    	
    def popRandArray(in_list):
    	""" return a random value from an array with no replacement
    	"""
    	return in_list.pop(randRange(0,len(in_list)-1))
    	
    def dealCardTuple():
    	"""return a tuple with a random card assigned
    	"""
    	return int(randRange(0,12)),int(randRange(0,3))
    
    def cardTupleAsString(in_tuple):
    	"""turn a deal from a hand of cards as a tuple into a string
    	"""
    	temp_value,temp_suite = in_tuple 
    	value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
    	suite = ["hearts","diamonds","spades","clubs"]
    	return value[temp_value]+ " of " + suite[temp_suite]
    
    def cardAsString(in_card):
    	"""turn a deal from a hand of cards as a tuple into a string
    	"""
    	value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
    	suit = ["hearts","diamonds","spades","clubs"]
    	return value[in_card%13]+ " of " + suit[in_card/13]
    	
    def realDealCard():
    	"""performs a non replacement randomised lookup
    	"""
    	global cards
    	#check to see if the array still has a card in it
    	if len(cards)==0:
    		print "new deck"
    		cards = range(0,52)
    	return popRandArray(cards)
    	
    def testCards():
    	"""tests the distribution of card when dealing a mulitple
    	number of hands"""
    	hands=int(raw_input('how many hands do you want to play?:'))
    	histo = 52 * [0]
    	for i in range(1,hands*52+1):
    		n = realDealCard()
    		histo[n] = histo[n] + 1
    	for i,v in enumerate(histo):
    		print cardAsString(i) + " " + str(v)
    #print dealCard()
    def cardScore(in_card):
    	"""converts a card into a numerical score"""
    	score = in_card%13+1
    	if score > 10:
    		score = 10
    	return score
    	
    def bestScore(in_array):
    	"""take an array and produces the best possible score without
    	going bust"""
    	#what is total score with ace = 1
    	score_array = map(cardScore,in_array)
    	print score_array
    	#what's the difference between 21 and the score
    	#if it's >= 10 then add on 10
    	out_score = sum(score_array)
    	if 1 in score_array:
    		if 21-out_score >=10:
    			return out_score+10
    		else:
    			return out_score
    	else:
    		return out_score
    	
    def exampleCards():
    	for i in range(100):
    		player_hand =	[realDealCard() for i in range(3)]
    		print player_hand
    		score = bestScore(player_hand)
    		if score>21:
    			print "bust"
    		else:
    			print score
    
    exampleCards()
    Last edited by bvdet; Apr 21 '10, 10:50 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    nytgangsta101,

    I renamed the thread to a more appropriate title. I also added code tags for you. Please see posting guidelines here.

    If you would post the actual error you received including the traceback, it would help us pinpoint the problem.

    BV - Moderator

    Comment

    Working...