首页 > 解决方案 > Python War纸牌游戏运行正常但无限

问题描述

from random import shuffle
import random

# Two useful variables for creating Cards.
SUITE = 'H D S C'.split()
RANKS = '2 3 4 5 6 7 8 9 10 J Q K A'.split()

class Deck:
    """
    This is the Deck Class. This object will create a deck of cards to initiate
    play. You can then use this Deck list of cards to split in half and give to
    the players. It will use SUITE and RANKS to create the deck. It should also
    have a method for splitting/cutting the deck in half and Shuffling the deck.
    """
    newlist = []
    playerone = []
    playertwo = []
    
    def __init__(self) -> None:
        pass
    
    def create(self, suite, ranks):
        self.suite = suite
        self.ranks = ranks
        
        for i in suite:
            for j in ranks:
                Deck.newlist.append([i,j])
        
        
    def splitshuffle(self):
        random.shuffle(Deck.newlist)
        for i in range(26):
            Deck.playerone.append(Deck.newlist[i])
            Deck.playertwo.append(Deck.newlist[i+26])
        
    
    

class Hand:
    '''
    This is the Hand class. It is to remove two cards from deck and add them to one player's deck whichever is higher also war 
    happens here
    '''
    commonlist = []
    war = 0
    def __init__(self, playerone, playertwo):
        self.playerone = playerone
        self.playertwo = playertwo
        
    
    def remove(self):

        self.removed_card1 = self.playerone.pop()
        self.removed_card2 = self.playertwo.pop()
        print("Card removed are: [", self.removed_card1, ", ", self.removed_card2, "]")
        
        return [self.removed_card1, self.removed_card2]
        

    def add(self, cardone, cardtwo):
        self.cardone = cardone
        self.cardtwo = cardtwo
        self.commonlist.clear()
        
        
        if RANKS.index(self.cardone[1])!=RANKS.index(self.cardtwo[1]):
            if RANKS.index(self.cardone[1])>RANKS.index(self.cardtwo[1]):
                self.playerone.insert(0,self.cardone)
                self.playerone.insert(0,self.cardtwo)
                print("Player one has higher card, adding")
                print("Length =", len(self.playerone)+len(self.playertwo))
            elif RANKS.index(self.cardone[1])<RANKS.index(self.cardtwo[1]):
                self.playertwo.insert(0,self.cardone)
                self.playertwo.insert(0,self.cardtwo)
                print("Player two has higher card, adding")
                print("Length =", len(self.playerone)+len(self.playertwo))
        else:
            self.war+=1
            if len(self.playerone)<3:
                self.playertwo.insert(0,self.cardone)
                self.playertwo.insert(0,self.cardtwo)
                while i in self.playerone:
                    self.playertwo.insert(0,self.playerone.pop())
            
            elif len(self.playertwo)<3:
                self.playerone.insert(0,self.cardone)
                self.playerone.insert(0,self.cardtwo)
                while i in self.playertwo:
                    self.playerone.insert(0,self.playertwo.pop())
            
            else:
                while RANKS.index(self.cardone[1])==RANKS.index(self.cardtwo[1]):
                    if RANKS.index(self.cardone[1])==RANKS.index(self.cardtwo[1]):
                        self.commonlist.append([self.cardone][0])
                        self.commonlist.append([self.cardtwo][0])
                        self.commonlist.append([self.playerone.pop()][0])
                        self.commonlist.append([self.playertwo.pop()][0])
                        self.cardone=self.playerone.pop()
                        self.cardtwo=self.playertwo.pop()
                
                print("Commonlist = ", self.commonlist)
                
                if RANKS.index(self.cardone[1])>RANKS.index(self.cardtwo[1]):
                    for i in range(len(self.commonlist)):
                        self.playerone.insert(0,self.commonlist[i])
                    self.playerone.insert(0,self.cardone)
                    self.playerone.insert(0,self.cardtwo)
                    print("Player one has higher card, adding")
                    # print("P1 = ", self.playerone)
                    # print("P2 = ", self.playertwo)
                    print("Length =", len(self.playerone)+len(self.playertwo))
                elif RANKS.index(self.cardone[1])<RANKS.index(self.cardtwo[1]):
                    for i in range(len(self.commonlist)):
                        self.playertwo.insert(0,self.commonlist[i])
                    self.playertwo.insert(0,self.cardone)
                    self.playertwo.insert(0,self.cardtwo)
                    print("Player two has higher card, adding")
                    # print("P1 = ", self.playerone)
                    # print("P2 = ", self.playertwo)
                    print("Length =", len(self.playerone)+len(self.playertwo))
                
                
        print("Wars: = ", self.war)
        
        
        
        
        


class Player:
    """
    This is the Player class, which takes in a name and an instance of a Hand
    class object. It returns result and also checks if player still have cards
    """
    
    
    def __init__(self, name, myhand):
        self.name=name
        self.myhand=myhand
    
    def game(self):
        
        self.count=0
        
        while len(self.myhand.playerone)!=0 or len(self.myhand.playertwo)!=0:
            
            if len(self.myhand.playerone)==0:
                break
            elif len(self.myhand.playertwo)==0:
                break
            x = self.myhand.remove()
            self.myhand.add(x[0], x[1])
            self.count+=1
            print("Count = ", self.count, "turns")
        
        print("Great game it lasted ", self.count, "turns")
        


# ######################
# #### GAME PLAY #######
# ######################
print("Welcome to War, let's begin...")
name = input("What is your name? ")
mydeck = Deck()
mydeck.create(SUITE, RANKS)
mydeck.splitshuffle()

myhand = Hand(mydeck.playerone, mydeck.playertwo)


myplayer = Player(name, myhand)
myplayer.game()

一切正常,没有额外的卡添加,整个游戏的卡总数也一样,战争也正确发生,但游戏无限运行,代码有错误还是游戏是这样的可以无限运行。总转数4000以上

标签: python

解决方案


推荐阅读