2013-03-18 2 views
0

다음 프로그램을 작성하여 사용자와 블랙 잭을 플레이했지만, 플레이어가 잭, 퀸 또는 킹을 가질 때마다 total_value 함수의 if 문은이를 감지하지 못합니다. 이 문제를 해결하려면 어떻게해야합니까? 또한, 내 코드를 정리하거나 구문을 개선하는 일반적인 포인터가 있습니까?변수가 파이썬의 블랙 잭 프로그램에서 감지되지 않습니다.

import random 
from random import randint 

class deck(): 
    """This class holds the deck information""" 
    clubs = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"] 
    spades = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"] 
    hearts = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"] 
    diamonds = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"] 
    suites = ["clubs", "spades", "hearts", "diamonds"] 

    def drawcard(self): 
     #This method removes a card from the deck and returns it. 
     #Do not draw cards when there are none left. 
     if self.suites: 
      suite = random.choice(self.suites) 
      suite_number = randint(0, len(self.suites) - 1) 
      if suite == "clubs": 
       card = (self.clubs).pop(randint(0, len(self.clubs) - 1)) 
       if not self.clubs: 
        (self.suites).remove("clubs") 
      elif suite == "spades": 
       card = (self.spades).pop(randint(0, len(self.spades) - 1)) 
       if not self.spades: 
        (self.suites).remove("spades") 
      elif suite == "hearts": 
       card = (self.hearts).pop(randint(0, len(self.hearts) - 1)) 
       if not self.hearts: 
        (self.suites).remove("hearts") 
      elif suite == "diamonds": 
       card = (self.diamonds).pop(randint(0, len(self.diamonds) - 1)) 
       if not self.diamonds: 
        (self.suites).remove("diamonds") 
      return card, suite 
     else: 
      return "OUT", "CARDS ERROR" 

def total_value(hand): 
    #This function returns the current total value of a player's hand. 
    #For example, ["A", "K"] would return 21. 
    value = 0 
    aces = 0 
    for card in hand: 
     if card == "A": 
      aces = aces + 1 
     elif card == "J": 
      value == value + 10 
     elif card == "Q": 
      value == value + 10 
     elif card == "K": 
      value == value + 10 
     else: 
      value = value + card 
    if aces == 1: 
     if value <= 10: 
      value = value + 11 
     elif value > 10: 
      value = value + 1 
    elif aces == 2: 
     if value <= 9: 
      value = value + 12 
     elif value > 9: 
      value = value + 2 
    elif aces == 3: 
     if value <= 8: 
      value = value + 13 
     elif value > 8: 
      value = value + 3 
    elif aces == 4: 
     if value <= 7: 
      value = value + 14 
     elif value > 7: 
      value = value + 4 
    return value 

new_deck = deck() 
player_hand = [ ] 
card1 = new_deck.drawcard() 
card2 = new_deck.drawcard() 
print "You have drawn a " + str(card1[0]) + " of " + card1[1] + " and a " + str(card2[0]) + " of " + card2[1] + "!" 
player_hand.append(card1[0]) 
player_hand.append(card2[0]) 

dealer_hand = [ ] 
card3 = new_deck.drawcard() 
card4 = new_deck.drawcard() 
dealer_hand.append(card3[0]) 
dealer_hand.append(card4[0]) 

gameover = False 
win = False 
dealer_finished = False 
player_finished = False 
while not gameover: 
    while dealer_finished == False: 
     if total_value(dealer_hand) < 17: 
      card = new_deck.drawcard() 
      dealer_hand.append(card[0]) 
     elif total_value(dealer_hand) >= 17: 
      dealer_finished = True 
    if total_value(dealer_hand) > 21: 
     print "Dealer Busts!" 
     win = True 
     gameover = True 
     break 
    while player_finished == False: 
     choice = raw_input("Hit or Stay? ") 
     choice.capitalize() 
     if choice == "Hit": 
      card = new_deck.drawcard() 
      player_hand.append(card[0]) 
      print "You drew a", card[0], "of " + card[1] 
      if total_value(player_hand) > 21: 
       player_finished = True 
       break 
     elif choice == "Stay": 
      player_finished = True 
      gameover = True 
      break 
     else: 
      print "Invalid Option" 
    if total_value(player_hand) > 21: 
     win == False 
     gameover == True 
     break 
    gameover == True 

if win == True: 
    print "Congratulations!" 
else: 
    print total_value(player_hand), total_value(dealer_hand) 
    if total_value(player_hand) > 21: 
     print "You bust!" 
    elif total_value(dealer_hand) > total_value(player_hand): 
     print "The dealer had", str(total_value(dealer_hand)), "but you only had", str(total_value(player_hand)) + "." 
     print "You lose!" 
    elif total_value(dealer_hand) == total_value(player_hand): 
     print "You tie the dealer with", total_value(dealer_hand) 
    elif total_value(dealer_hand) < total_value(player_hand): 
     print "The dealer had", str(total_value(dealer_hand)), "and you had", str(total_value(player_hand)) + ", so you win!" 

답변

0

당신은 J, QK가지 경우 표지판을 더블 - 동일이 : 모든이가하는

if card == "A": 
     aces = aces + 1 
    elif card == "J": 
     value == value + 10 #xxx 
    elif card == "Q": 
     value == value + 10 #xxx 
    elif card == "K": 
     value == value + 10 #xxx 
    else: 
     value = value + card 

valuevalue + 10 여부를 확인하고 True 또는 False로 평가합니다. 더 나은 아직

if card == "A": 
     aces = aces + 1 
    elif card == "J": 
     value = value + 10 
    elif card == "Q": 
     value = value + 10 
    elif card == "K": 
     value = value + 10 
    else: 
     value = value + card 

나 :

if card == "A": 
     aces = aces + 1 
    elif card == "J": 
     value += 10 
    elif card == "Q": 
     value += 10 
    elif card == "K": 
     value += 10 
    else: 
     value += card 
+0

아, 감사합니다, 나는 그것을 놓친 당신은 값을 할당합니다. – Taylor

관련 문제