簡體   English   中英

我嘗試添加應該詢問用戶是否想再次播放的代碼

[英]I tried to add the code which should ask the user if they want to play again

這是一場戰爭游戲。 我試圖添加它應該詢問用戶是否想再次播放的代碼。 它再次開始游戲,但從不重置玩家的牌,因此每次再次播放時他們擁有超過 26 張牌。 我不明白每次玩家想再次玩時我應該添加什么來重置卡片

超過26張卡

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())

我相信你的問題是你有 state 存儲在每個播放器 object 中,你沒有在下一場比賽之前重置。 我運行了您的代碼,重現了問題,然后將其修復如下:

我將此方法添加到您的播放器 class:

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

我在每個新游戲開始時添加了對該方法的調用:

while play_again:

    player_one.reset()
    player_two.reset()

這樣做之后,您的代碼似乎表現得更好。 我不確定它在后續游戲中是否一切正常,但卡片數量並沒有像原始版本那樣超過 52 張卡片。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM