blackjack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    #16
    black jack

    thnaks for ur help with updating the score, it now works, but i have two main problem, firstly how would i now get the ace to be 1 and 11, and a way of displaying the total hands played at the end of the game.

    thanks for the help


    below is my code,

    Code:
    import random 
        
    deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
    player=[]
    computer=[]
    random.shuffle(deck)  #this will shuffle the cards in the deck
    nextCard = 0
    player.append(deck[nextCard]) 
    nextCard += 1 
    player.append(deck[nextCard]) # nextCard is now 1. 
    nextCard += 1 
    computer.append(deck[nextCard]) 
    nextCard += 1 
    computer.append(deck[nextCard]) 
    nextCard += 1 
    ptotal = sum(player)
    ctotal = sum(computer)
    print "players hand", player, "\tplayers total value", ptotal   #this will display the players hand
    #hit or stand,are players options.
    def game():
        global nextCard
        while (True):
            options = raw_input ("do you wana [h]it / [s]tand / [q]uit: ")
            if options == 'h': 
                 player.append(deck[nextCard]) 
                 ptotal = sum(player)  
                 nextCard += 1  
                 print "players hand", player, "\tplayers total value", ptotal
                 
            elif options == 's':
                ptotal = sum(player)
                print "players hand", player, "\tplayers total value", ptotal
                break
            elif options == 'q':
                playAgain = raw_input ("do u wana play again, [y] / [n]")
                if playAgain == "y":
                    game()
                elif playAgain == "n":
                    print "see ya later, bye!"
                    return
                    break 
            
        while (True):
            if ctotal < 16:
                computer.append(deck[nextCard])
                nextCard += 1
            if ptotal > 21:
                print "computer hand", computer, "\tcomputers total value", ctotal
                print "player busted. computer wins"
                break
            if ctotal > 21:
                print "computer hand", computer, "\tcomputers total value", ctotal
                print "computer busted. player wins"
                break
            if ctotal == ptotal:
                print "computer hand", computer, "\tcomputers total value", ctotal
                print "draw. computer wins"
                break
            if ptotal == 21:
                print "computer hand", computer, "\tcomputers total value", ctotal
                print "player gets a BlackJack. player wins"
                break
            if ctotal == 21:
                print "computer hand", computer, "\tcomputers total value", ctotal
                print "computer gets a BlackJack. computer wins"
                break
            if ptotal > ctotal:
                if ptotal < 21:
                    print "computer hand", computer, "\tcomputers total value", ctotal
                    print "player wins"
                    break
            if ctotal > ptotal:
                if ctotal < 21:
                    print "computer hand", computer, "\tcomputers total value", ctotal
                    print "computer wins"
                    break
           
    game()

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #17
      I showed you how to count an ace as 1 or 11 already by defining a deck of cards with an "A" instead of a 1 and 11, and defining a function to determine the score of a hand. Here is another way, using a 1 instead of an "A".
      Code:
      >>> def sum_score(hand):
      ...     score = sum(hand)
      ...     for card in hand:
      ...         if card == 1 and score < 12:
      ...             score += 10
      ...     return score
      ... 
      >>> deck = [1,2,3,4,5,6,7,8,9,10,10,10,10]*4
      >>> import random
      >>> random.shuffle(deck)
      >>> hand1 = [deck.pop() for i in range(3)] # a hand with 3 cards
      >>> hand2 = [deck.pop() for i in range(3)] # another hand with 3 cards
      >>> hand1
      [5, 10, 10]
      >>> sum_score(hand1)
      25
      >>> hand2
      [1, 4, 5]
      >>> sum_score(hand2)
      20
      >>>
      hand2 contains a 1 which represents an ace. Function sum_score() adds 10 to the score because the initial sum is less than 12.

      -BV

      Comment

      • imran akhtar
        New Member
        • Nov 2008
        • 64

        #18
        black jack

        yeh thanks, after few changes it is now wroking, is there a way of how i would display total hands played, at the end of game, for both computer and player

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #19
          Your code is designed for one hand. You will need a way to deal additional hands, therefore another option is required. Create an empty list at the beginning of the script, and append each hand's results to it.

          -BV

          Comment

          • imran akhtar
            New Member
            • Nov 2008
            • 64

            #20
            ok, a bit confused, dont understand how i would start it, thanks

            Comment

            • imran akhtar
              New Member
              • Nov 2008
              • 64

              #21
              so is it possiable of you can explain bit more, how i would display the hands.

              thanks

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #22
                Create an empty list names results, and append the result of each hand to the list after the winner of the hand is determined. When the user decides to quit playing, ite3rate on the list to display the all results.
                Code:
                results = []
                results.append(['player',[1,5,5],[6,9,4]])
                results.append(['computer',[6,5,6],[9,10]])
                for result in results:
                    print "Winner: %s" % result[0]
                    print "    Player hand: %s" % (', '.join([str(i) for i in result[1]]))
                    print "    Computer hand: %s" % (', '.join([str(i) for i in result[2]]))

                Comment

                Working...