简体   繁体   中英

If-statement does not work(?)

I am a happy amateur who tries to create a "more or less" game. I'm not right on point allocation. My if statement is not working properly. I get no error message but everything is running in the "else". The conditions are met never, although K, Q, J, Ace randomly ... Why?

class Card(object):

    totPoints = 0

    VALUE = {"A":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}

    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    SUIT = ["Python/projekt/bilder/hearts.png", "Python/projekt/bilder/spades.png", "Python/projekt/bilder/diamond.png", "Python/projekt/bilder/clubs.png"]

    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        rep = self.rank + self.suit
        return rep

    def draw(self):

        bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))

        cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1, relief='solid', highlightthickness=2)
        cardGraph.photo=bg
        cardGraph.pack(side = "left", anchor=NW)

class Hand(object):
    def __init__(self):
        self.cards = []

    def __str__ (self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep += str(card) + " "
        else:
            rep = "<empty>"
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)

class Deck(Hand):

    def populate(self):
        for suit in Card.SUIT:
            for rank in Card.RANKS:
                self.add(Card(rank, suit))

    def shuffle(self):
        import random
        random.shuffle(self.cards)
        DrawCard = self.cards[0]
        DrawCard.draw()

    def deal(self, hands, per_hand = 0):
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                         top_card = self.cards[0]
                         self.give(top_card, hand)         
                else:
                    print("Cant continue deck. Out of cards!!")

    def setValue(self):
            if self.cards[0] == "K":
                Card.totPoints += 10
                print Card.totPoints
            elif self.cards[0] == "Q":
                Card.totPoints += 10
            elif self.cards[0] == "J":
                Card.totPoints += 10
            elif self.cards[0] == "A":
                Card.totPoints += 10
            else:
                Card.totPoints += self.cards
                print Card.totPoints

Try restructuring it so that you return after you find the value you want; that makes it less prone to confusion about the order of elifs. Also, it's easier to is 'in' tests for large numbers of values:

def setValue(self):
   first_card = self.cards[0]
   face_cards = ['K','Q','J','A']
   if first_card in face_cards: 
      card.totPoints += 10
      return
   card.totPoints += self.cards

As an aside: this will result in a score of 10 if the first card is a face card and the total of all your cards if it's not. Is that what you want?

Lastly, It seems like you're updating the value for the card from the deck object. Perhaps you should just set the card's score in the init so you don't have to do it from the outside?

Your code never represents cards as mere strings. Instead, you are using instances of the class Card() :

def populate(self):
    for suit in Card.SUIT:
        for rank in Card.RANKS:
            self.add(Card(rank, suit))

You'll need to test against the .rank attribute instead:

def setValue(self):
    if self.cards[0].rank == "K":
        Card.totPoints += 10
        print Card.totPoints
    elif self.cards[0].rank == "Q":
        Card.totPoints += 10
    elif self.cards[0].rank == "J":
        Card.totPoints.rank += 10
    elif self.cards[0].rank == "A":
        Card.totPoints += 10
    else:
        Card.totPoints += int(self.cards[0].rank)

Note that .rank is always a string, so you'll need to turn it into an integer when it is a number card. I've assumed that that is the goal of the else: branch, in any case.

Your code in that function can be greatly simplified:

def setValue(self):
    rank = self.cards[0].rank
    Card.totPoints += 10 if rank in 'KQJA' else int(rank)

Alternatively, you could just use the Class.VALUE mapping you already have:

def setValue(self):
    Card.totPoints += Card.VALUE[self.cards[0].rank]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM