简体   繁体   中英

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

This is a game of war. I tried to add the code which it should ask the user if they want to play again. It starts the game again but never resets the player's cards and so they have more than 26 cards each time it plays again. I cant understand what should i add to reset the cards each time the player wants to play again

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

I believe that your problem is that you have state stored in each Player object that you aren't resetting before the next game. I ran your code, reproduced the problem, and then fixed it as follows:

I added this method to your Player class:

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

I added calls to this method at the beginning of each new game:

while play_again:

    player_one.reset()
    player_two.reset()

After doing this, your code seemed to be much better behaved. I'm not sure if it was doing everything right on subsequent games, but the card counts didn't grow beyond 52 cards like they did with the original version.

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