# blackjack.py # # Jim M, Nov 2008 """ Way too much playing around with Card() and Hand() classes. """ from card import Card from hand import Hand from doctest import testmod Win = 'win' Lose = 'lose' Draw = 'draw' class UserPlayer: """A black jack player that decides what to do from user input.""" def getHitMe(self, myHand, houseHand, houseHoleHidden): """Return True or False after asking the player if he wants another card.""" import sys answer = raw_input(" Another card? (yes|no|quit) ") firstCharacter = answer[0].lower() if firstCharacter == 'q': print "Goodbye, you quitter you." sys.exit(0) return firstCharacter == 'y' class RandomPlayer: def getHitMe(self, myHand, houseHand, houseHoleHidden): """Return True or False with 50/50 probability.""" from random import randrange return randrange(2)==0 def playBlackJack(player=None, verbose=True): if verbose: print print "-- Black Jack --" if player==None: player = UserPlayer() yourHand = Hand(2) houseHand = Hand(2) if verbose: print " Your cards are: " yourHand.printHand(indent=4) if houseHand.isBlackJack(): if verbose: print " The house has: " houseHand.printHand(indent=4) if yourHand.isBlackJack(): if verbose: print " Both you and the house have blackjack. It's a draw." return Draw else: if verbose: print " The house has blackjack. You lose." return Lose else: if verbose: print " The house card showing is: " + str(houseHand.getCard()) if yourHand.isBlackJack(): if verbose: print " You have blackjack, and win." return Win houseHoleHidden = True hitMe = True while True: if hitMe: if verbose: print " Your total is " + str(yourHand.blackJackValue()) hitMe = player.getHitMe(yourHand, houseHand, houseHoleHidden) if not hitMe: if verbose: print " You stand pat." if hitMe: yourHand.addCard() if verbose: print " Your new card: " + str(yourHand.getCard()) if yourHand.isBust(): if verbose: print " You're busted." return Lose if houseHoleHidden: if verbose: print " The house's hidden card is: " + str(houseHand.getCard(1)) houseHoleHidden = False if verbose: print " The house total is " + str(houseHand.blackJackValue()) if not hitMe: if houseHand.blackJackValue() > yourHand.blackJackValue(): if verbose: print " The house hand is closer to 21, so you lose." return Lose if houseHand.blackJackValue() == yourHand.blackJackValue() and \ houseHand.blackJackValue() >= 17 : if verbose: print " The house stands, and has the same as you. Draw." return Draw if houseHand.blackJackValue() < 17 or \ houseHand.blackJackValue() < yourHand.blackJackValue(): houseHand.addCard() if verbose: print " House's new card: " + str(houseHand.getCard()) if houseHand.isBust(): if verbose: print " The house is busted, so you win." return Win def playRandomGames(howMany): """An example of running a bunch of games.""" print print "Playing %i random games..." % howMany stats = {Win: 0, Lose: 0, Draw: 0} for i in range(howMany): player = RandomPlayer() result = playBlackJack(player, False) # print " " + result stats[result] += 1 print "Done." format = " %6s %6s %6s " print format % ('win', 'lose', 'draw') print format % ('-'*6, '-'*6, '-'*6) print " %6i %6i %6i " % (stats[Win], stats[Lose], stats[Draw]) if __name__=="__main__": _tests() testmod() playBlackJack() playRandomGames(200)