# Project Three MCS 260 due 22 October 2007 # # In what follows below, the user of the program is called # the player, and our friend the computer is named the dealer. # The rules of our simplified blackjack game are # (1) A random number between 1 and 11 is given # to the dealer and player in each round. # (2) No more cards are dealt if the value of the hand # of either dealer or player is equal to 21 or more. # (3) The player can choose to take another card # or to the end the game. # (4) The hand of the dealer is not shown until # the dealing of cards has ended. # (5) At the end of the game the hands of player # and dealer are compared to see who wins. # A player whose value of the hand exceeds 21 is busted # and we will call it a blackjack if the value equals 21. # The rules to decide who wins are # (1) At a tie, the hands of both dealer and player # have the same value, or if both are busted. # (2) The player has a winning hand if its value is less # than or equal to 21 and it is higher than the value # of the hand of the dealer or the dealer is busted. # (3) The player loses in all other cases. # After reporting the outcome of each game (win, tie, or loss), # the user is prompted the question "another game? (y/n) " and # the program then continues if 'y' answers the question. # At the very end of the program, the total tally of wins, ties, # and losses is printed to the screen. # def play ( hand, sumhand ): "evaluates hand and returns boolean stop" print 'hand =', hand, 'sum = ', sumhand y_or_n = raw_input('another card ? (y/n) ') stop = (y_or_n != "y") return stop def show_hands ( deal, hand, sumdeal, sumhand ): "returns two strings to show hands of dealer and player" ps1 = 'dealers hand = ' + str(deal) + ' sum = ' + str(sumdeal) ps2 = 'players hand = ' + str(hand) + ' sum = ' + str(sumhand) if sumdeal == 21: ps1 += ' blackjack' elif sumdeal > 21: ps1 += ' busted' if sumhand == 21: ps2 += ' blackjack' elif sumhand > 21: ps2 += ' busted' return (ps1,ps2) def compare ( deal, hand, sumdeal, sumhand ): "compares hand of dealer with player" (ps1,ps2) = show_hands(deal,hand,sumdeal,sumhand) if (sumdeal > 21 and sumhand > 21) or (sumdeal == sumhand): ps2 += ' tie' result = 0 else: # decide win or loss if sumhand == 21: ps2 += ' win'; result = +1 elif sumhand < 21: if (sumdeal < sumhand) or (sumdeal > 21): ps2 += ' win'; result = +1 else: ps2 += ' loss'; result = -1 else: ps2 += ' loss'; result = -1 print ps1 + '\n' + ps2 return result def update ( tally, score ): "updates the tally of wins, ties, and losses\ with the score +1 (win), 0 (tie), or -1 (loss)" nt = tally if score == +1: nt[0] += 1 elif score == 0: nt[1] += 1 else: nt[2] += 1 return nt def main(): import random print 'welcome to our simplified blackjack game' t = [0,0,0] # wins, ties, and losses while True: hand = []; deal = [] while True: hand.append(random.randint(1,11)) deal.append(random.randint(1,11)) sd = sum(deal); sh = sum(hand) if sd >= 21 or sh >= 21: break if play(hand,sh): break s = compare(deal,hand,sd,sh) t = update(t,s) if (raw_input('another game? (y/n) ') != 'y'): break print '#wins = %d, #ties = %d, #losses = %d' % (t[0],t[1],t[2]) main()