Python 中最高的纸牌游戏

问题描述 投票:0回答:1

所以我用python创建了这个最高卡牌游戏,代码运行良好并且程序打开。我遇到的唯一问题是,当每个玩家都抽到牌时,它不会显示卡号和花色。它显示 (<main.HC_Card 对象位于 0x02EB2670>;) 附加图像。有人能帮忙吗 ?将非常感激。

卡片模块:

卡片模块

import sys

class Card(object):
# A playing card
    CARDS=[ "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace" ]
    SUITS=[ "Clubs", "Diamonds", "Hearts", "Spades" ]
    def __init__(self, card, suit):
        self.card = card
        self.suit = suit
        
class Hand(object):
# A hand of playing cards
    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):
# A deck of playing cards
    def populate(self):
        for suit in Card.SUITS:
            for card in Card.CARDS:
                self.add(Card(card, suit))
    def shuffle(self):
        import random
        random.shuffle(self.cards)
    def deal(self, hands, per_hand = 1):
        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("Deck has run out of cards. Game over.")
                    sys.exit()
if __name__ == "__main__":
    print("This is a module with classes for playing cards.")
    
#input("\nPress Enter to exit")

游戏模块:

游戏模块

class Player(object):
# Defines a player in a game
    def __init__(self, name, score = 0):
        self.name = name
        self.score = score
    def __str__(self):
        rep = self.name + ":\t" + str(self.score)
        return rep
def askYesNo(question):
    response = None
    while response not in ("y", "n"):
        response = input(question).lower()
    return response
def askForNumber(question, low, high):
    response = None
    while response not in range (low, high):
        response = int(input(question))
    return response
if __name__ == "__main__":
    print("You ran this module directly (not imported).")
    #input("\nPress Enter to exit.")

主要代码:

import Cards, Games

class HC_Card(Cards.Card):
    @property
    def value(self):
        val = HC_Card.CARDS.index(self.card) + 1
        return val

class HC_Deck(Cards.Deck):
    def populate(self):
        for suit in HC_Card.SUITS:
            for card in HC_Card.CARDS:
                self.cards.append(HC_Card(card, suit))

class HC_Hand(Cards.Hand):
    def __init__(self, name):
        super(HC_Hand, self).__init__()
        self.name = name
    def __str__(self):
        rep = self.name + ": " + super(HC_Hand, self).__str__()
        return rep
    @property
    def total(self):
        t = 0
        for card in self.cards:
            t += card.value
        return t
 
class HC_Game(object):
    def __init__(self, names):
        self.players = []
        for name in names:
            player = HC_Hand(name)
            self.players.append(player)
        self.deck = HC_Deck()
        self.deck.populate()
        self.deck.shuffle()
    
    def play(self):
        self.deck.deal(self.players, per_hand = 1)
        highestCard = 0
        highestName = ""
        for player in self.players:
            print(player)
            if player.total == highestCard:
                highestName = "Draw"
            elif player.total > highestCard:
                highestCard = player.total
                highestName = player.name
        print("\nAnd the winner is...", highestName, "With the..", highestCard, "\b.")
        
        
        for player in self.players:
            player.clear()
 
def main():
    print("\nThis is the Highest Card game...")
    print("The player with the Highest card wins.. Good Luck!\n")
    names = []
    number = Games.askForNumber("How many players? (2-7): ", low = 2, high = 8)
    print()
    i = 1
    for i in range(number):
        name = input("Enter player name: ")
        if name == "":
            names.append("Anon")
            print()
            i += 1
        else:
            names.append(name)
            print()
            #i += 1
    game = HC_Game(names)
    again = "Y"
    rounds = 0
    while again == "y" or again == "Y":
        rounds += 1
        num_cards_dealt = number * rounds
        if num_cards_dealt > 52 - number:
            game.__init__(names)
            rounds = 0
        game.play()
        again = Games.askYesNo("\nDo you want to play again? ")
        print()
    print("I hope you enjoyed the game..")
main()
python python-3.x inheritance module
1个回答
0
投票

请在

__str__
类下添加
card
函数以及
suit
Card
变量来打印。

代码:

class Card(object):
    ...
    def __str__(self):
        return f"{self.card} of {self.suit}"

输出:

...
How many players? (2-7): 2

Enter player name: alec
Enter player name: lluke

alec: Queen of Spades;
lluke: 9 of Diamonds;
...
© www.soinside.com 2019 - 2024. All rights reserved.