【问题标题】:I tried to add the code which should ask the user if they want to play again我尝试添加应该询问用户是否想再次播放的代码
【发布时间】:2021-01-03 07:05:49
【问题描述】:

这是一场战争游戏。 我试图添加它应该询问用户是否想再次播放的代码。 它再次开始游戏,但从不重置玩家的牌,因此每次再次播放时他们拥有超过 26 张牌。 我不明白每次玩家想再次玩时我应该添加什么来重置卡片

more than 26 cards

import random

values = {"Two":2, "Three":3, "Four":4, "Five":5, "Six":6,
          "Seven":7, "Eight":8, "Nine":9, "Ten":10, "Jack":11,
                    "Queen":12, "King":13, "Ace":14}

suits = ("Hearts", "Diamonds", "Clubs", "Spades")

ranks = ("Two", "Three", "Four", "Five", "Six",
          "Seven", "Eight", "Nine", "Ten", "Jack",
                    "Queen", "King", "Ace")
class Card:
    
    def __init__(self,suit,rank):
        
        self.suit = suit
        self.rank = rank
        self.value = values[rank]
        
    def __str__(self):
        
        return self.rank + " of " + self.suit
    
class Deck:
    
    def __init__(self):
        
        self.all_cards = []
        
        for suit in suits:
            for rank in ranks:
                
                created_card = Card(suit,rank)
                
                self.all_cards.append(created_card)
                
    def shuffle(self):
        
        random.shuffle(self.all_cards)
        
    def deal_one(self):
        
        return self.all_cards.pop()
    
class Player:
        
    def __init__(self, name):
        
        self.name = name
        self.all_cards = []
        
    def remove_one(self):
        return self.all_cards.pop(0)
    
    def add_cards(self, new_cards):
        
        if type(new_cards) == type([]):
            self.all_cards.extend(new_cards)
        else:
            self.all_cards.append(new_cards)


            
    def __str__(self):
        return f"{self.name}"

                            #*****SCRIPT*****#

player_one = Player(input("Player One: Enter Name "))
player_two = Player(input("Player Two: Enter Name "))







play_again = True

while play_again:

    game_on = True

    round_num = 0



    new_deck = Deck()
    new_deck.shuffle()



    for x in range(26):
        player_one.add_cards(new_deck.deal_one())
        player_two.add_cards(new_deck.deal_one())

    print(f"player one cards: {len(player_one.all_cards)}")
    print(f"player two cards: {len(player_two.all_cards)}") 

    while game_on:

        round_num += 1
        print(f"Round Number: {round_num}")

        if len(player_one.all_cards) == 0:
            print(f"{player_one} is out of cards! \n{player_two} won the game!! \nGAME OVER")
            game_on = False
            a = input("Do you want to play agian? ")
            if a == "Y":
                play_again = True
            else: 
                play_again = False
            break

        if len(player_two.all_cards) == 0:
            print(f"{player_two} is out of cards! \n{player_one} won the game!! \nGAME OVER")
            game_on = False
            a = input("Do you want to play agian? ")
            if a == "Y":
                play_again = True
            else: 
                play_again = False
            break

        
        player_one_cards = []
        player_one_cards.append(player_one.remove_one())
             
        player_two_cards = []
        player_two_cards.append(player_two.remove_one())
        

        at_war = True

        while at_war:

            if player_one_cards[-1].value > player_two_cards[-1].value:
                player_one.add_cards(player_one_cards)
                player_one.add_cards(player_two_cards)
                at_war = False

            elif player_one_cards[-1].value < player_two_cards[-1].value:
                player_two.add_cards(player_two_cards)
                player_two.add_cards(player_one_cards)
                at_war = False

            else: 
                print("****WAR HAS BEGUN****")

                if len(player_one.all_cards) < 5:
                    print(f"{player_one} is unable to play war! \n{player_two} won! \nGAME OVER")
                    game_on = False
                    a = input("Do you want to play agian? ")
                    if a == "Y":
                        play_again = True
                    else: 
                        play_again = False
                    break

                elif len(player_two.all_cards) < 5:
                    print(f"{player_two} is unable to play war! \n{player_one} won! \nGAME OVER")
                    game_on = False
                    a = input("Do you want to play agian? ")
                    if a == "Y":
                        play_again = True
                    else: 
                        play_again = False
                    break

                else:
                    for num in range(5):
                        player_one_cards.append(player_one.remove_one())
                        player_two_cards.append(player_two.remove_one())

【问题讨论】:

  • 请分享一个最小的示例。
  • 在快速查看您的代码之后,我打算分享一个较小的 sn-p。我猜你应该在if a == "Y": 中使用def deal_one() 来重置牌组。
  • 我添加了显示两位玩家牌的图像

标签: python


【解决方案1】:

我相信您的问题是您在每个 Player 对象中存储了您在下一场比赛之前没有重置的状态。我运行了您的代码,重现了问题,然后将其修复如下:

我将此方法添加到您的 Player 类中:

def reset(self):
    self.all_cards = []

我在每个新游戏开始时都添加了对该方法的调用:

while play_again:

    player_one.reset()
    player_two.reset()

执行此操作后,您的代码似乎表现得更好。我不确定它在后续游戏中是否一切正常,但卡片数量并没有像原始版本那样超过 52 张。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2020-02-15
    • 2016-09-22
    相关资源
    最近更新 更多