使用导入模块的Python最高卡牌游戏

62o28rlo  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(76)

所以我已经在Python中创建了一个最高卡游戏,我想添加一个功能,但我正在努力与代码.所以我想添加一个功能,其中平局是不可能的,所以如果两个玩家画相同的卡,然后他们必须画另一张卡,看看谁赢.有点像平局断路器.新的Python,并会非常感谢帮助.

最高卡游戏,最多2 - 7名玩家

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..", , "\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()

字符串

flvtvl50

flvtvl501#

重新思考一下你的play()代码部分。你走在正确的道路上。画一个小的铅笔和纸流程图可能会有所帮助,这将有助于你编写逻辑循环。这里有几件事需要考虑:
1.直到最高的牌被建立,你才知道是否有平局,所以在你的第一个循环中删除识别玩家的代码。
1.那么,如果4个玩家都抽到了同一张牌呢?好吧,在我们确定了最高的牌是什么之后,我们可以再次循环玩家,并 * 制作一个新的名单 *。
1.然后呢,我们得再画一次,只画新系列的人.
1.然后会发生什么(和以前一样)?好吧,1个玩家可以用最高的牌来识别,或者2个玩家再次平局.这听起来像while循环的控制逻辑,对吗?while len(players with winning card) > 1: do another round.
因此,上面的内容应该描述了初学者程序的一般方法。需要有一个循环,因为我们不知道需要多少次平局才能解决平局。需要有一个新的容器(list就可以了),我们可以构建(最容易的是在外部循环中另一个小循环)来保存回合的获胜者。
给予一个尝试,评论回来,并编辑您的问题与任何尝试,如果你卡住了!

相关问题