在Python

时间:2017-11-01 02:11:09

标签: python

因此,我必须使用卡片对象重新制作纸牌游戏War。我遇到的问题是如何比较两个卡对象列表,看看哪个更大。我没有尝试过比较两个列表的任何东西,因为我真的不知道从哪里开始。我需要比较哪一个更大,并给予该名单/人获胜的积分。一个ace是1但是击败了一个13岁的国王我在课堂上有重载方法,因为我知道这是从哪里开始的,但这是我到目前为止所做的:

import random
class War:
    def __init__(self):
        randomCard = random.randint(1,13)
        self._cNumber = randomCard
        randomSuit = random.randint(1,4)
        self._cSuit = randomSuit

        if self._cSuit == 1:
            self._cSuit = "H"
        elif self._cSuit == 2:
            self._cSuit = "S"
        elif self._cSuit == 3:
            self._cSuit = "C"
        elif self._cSuit == 4:
            self._cSuit = "D"


    def getNumber(self):
        return self._cNumber

    def getSuit(self):
        return self._cSuit

    def __str__(self):
        if self._cNumber > 1 and self._cNumber < 11:
            return str(self._cNumber) + self._cSuit
        elif self._cNumber == 1:
            return "A" + self._cSuit
        elif self._cNumber == 11:
            return "J" + self._cSuit
        elif self._cNumber == 12:
            return "Q" + self._cSuit
        elif self._cNumber == 13:
            return "K" + self._cSuit

    def __gt__(self,other):
        if self._cNumber == 1:
            self._cNumber = 14
        if other._cNumber == 1:
            other._cNumber = 14

        if self._cNumber > other._cNumber:
            return True


    def __lt__(self,other):
        if self._cNumber == 1:
            self._cNumber = 14
        if other._cNumber == 1:
            other._cNumber = 14

        if self._cNumber < other._cNumber:
            return True
        return False

    def __eq__(self,other):
        if self._cNumber == 1:
            self._cNumber = 14
        if other._cNumber == 1:
            other._cNumber = 14

        if self._cNumber == other._cNumber:
            return True
        return False

cardList1 = []
cardList2 = []

list1C1 = War()
list1C2 = War()
list1C3 = War()
list1C4 = War()
list1C5 = War()
cardList1.append(list1C1)
cardList1.append(list1C2)
cardList1.append(list1C3)
cardList1.append(list1C4)
cardList1.append(list1C5)

list2C1 = War()
list2C2 = War()
list2C3 = War()
list2C4 = War()
list2C5 = War()
cardList2.append(list2C1)
cardList2.append(list2C2)
cardList2.append(list2C3)
cardList2.append(list2C4)
cardList2.append(list2C5)

print("Player 1:")
for x in range(5):
    print(cardList1[x])

print("Player 2:")
for x in range(5):
    print(cardList2[x])

2 个答案:

答案 0 :(得分:1)

对于重载战争游戏的卡片方法,您只需要关注卡片的等级而不是套装,以下内容可能适用于此...

def cmp(self, other):
    if self.rank > other.rank: return 1
    if self.rank < other.rank: return -1
    return 0

def __eq__(self, other):
    return self.cmp(other) == 0

def __le__(self, other):
    return self.cmp(other) <= 0

def __ge__(self, other):
    return self.cmp(other) >= 0

def __gt__(self, other):
    return self.cmp(other) > 0

def __lt__(self, other):
    return self.cmp(other) < 0

def __ne__(self, other):
    return self.cmp(other) != 0 

在编写Card类之前,您可能还想要实现Deck类,Hand类和War类。至少需要Card类来利用以前发布的代码,因为它将两个对象(卡片)作为参数

答案 1 :(得分:0)

'''
Class that models a standard playing card.
Objects of this type will have a number between 1 and 13 (inclusive) and a suit (Spade, Heart, Club or Diamond).
Models the card game War by comparing two card objects and determining the winner based on the bigger card number
'''
import random
class War:
    def __init__(self):
        '''
        Constructs the card number and suit of the card object and assigns random values within playing card range. Also accounts for cards higher than 10 going into face cards.
        '''
        randomCard = random.randint(1,13)
        self._cNumber = randomCard
        randomSuit = random.randint(1,4)
        self._cSuit = randomSuit

        if self._cSuit == 1:
            self._cSuit = "H"
        elif self._cSuit == 2:
            self._cSuit = "S"
        elif self._cSuit == 3:
            self._cSuit = "C"
        elif self._cSuit == 4:
            self._cSuit = "D"


    def getNumber(self):
        '''
        Accessor method that returns the number on the card object
        '''
        return self._cNumber

    def getSuit(self):
        '''
        Accessor method that returns the suit on the card object
        '''
        return self._cSuit

    def __str__(self):
        '''
        Allows for the user to print out the card objects values instead of a memory location
        '''
        if self._cNumber > 1 and self._cNumber < 11:
            return str(self._cNumber) + self._cSuit
        elif self._cNumber == 1:
            return "A" + self._cSuit
        elif self._cNumber == 11:
            return "J" + self._cSuit
        elif self._cNumber == 12:
            return "Q" + self._cSuit
        elif self._cNumber == 13:
            return "K" + self._cSuit

    def __gt__(self,other):
        '''
        Overloading the greater than operator to use for comparing the card objects
        '''
        if self._cNumber == 1:
            self._cNumber = 14
        if other._cNumber == 1:
            other._cNumber = 14

        if self._cNumber > other._cNumber:
            return True
        return False


    def __lt__(self,other):
        '''
        Overloading the less than operator to use for comparing the card objects
        '''
        if self._cNumber == 1:
            self._cNumber = 14
        if other._cNumber == 1:
            other._cNumber = 14

        if self._cNumber < other._cNumber:
            return True
        return False

    def __eq__(self,other):
        '''
        Overloading the equals to operator to use for comparing the card objects
        '''
        if self._cNumber == 1:
            self._cNumber = 14
        if other._cNumber == 1:
            other._cNumber = 14

        if self._cNumber == other._cNumber:
            return True
        return False
#Create two lists that will be used for storing the card objects
cardList1 = []
cardList2 = []

#Creating 5 card objects for each list then appending them to the list
list1C1 = War()
list1C2 = War()
list1C3 = War()
list1C4 = War()
list1C5 = War()
cardList1.append(list1C1)
cardList1.append(list1C2)
cardList1.append(list1C3)
cardList1.append(list1C4)
cardList1.append(list1C5)

list2C1 = War()
list2C2 = War()
list2C3 = War()
list2C4 = War()
list2C5 = War()
cardList2.append(list2C1)
cardList2.append(list2C2)
cardList2.append(list2C3)
cardList2.append(list2C4)
cardList2.append(list2C5)

#Prints out the card object values such as suit and number on the card
print("Player 1:")
for x in range(5):
    print(cardList1[x])

print("Player 2:")
for x in range(5):
    print(cardList2[x])

#Initializes point system for the game of War
Player1Points = 0
Player2Points = 0

#Loops through the lists comparing the two card objects to each other and determining whether Player 1: Wins, Ties, or Loses
for x in range(5):
    if War.__gt__(cardList1[x], cardList2[x]) == True:
        Player1Points += 1
        print("Player 1 Wins")
    elif War.__eq__(cardList1[x], cardList2[x]) == True:
        print("No points awarded. Tie.")
    else:
        Player2Points += 1
        print("Player 2 Wins")
    print("Player 1 points: ",Player1Points," Player 2 Points: ",Player2Points)

#Determines the winner of the game based on the number of points each player had
if Player1Points > Player2Points:
    print("Player 1 Wins the game")
elif Player2Points > Player1Points:
    print("Player 2 Wins the game")
else:
    print("It is a tie")