Python Game" House of Cards"需要帮助

时间:2014-07-25 18:41:48

标签: python oop object comparison

我目前是一名python新手,我正在使用python创建一些游戏。我写了一个游戏“纸牌屋”,基本上我试图用我在OOP学到的东西来做。规则很简单 - 你需要最大的牌来赢。玩家会首先比较数字,然后是卡片的套装 - 拥有最大卡片的人获胜。我写了以下代码:

import itertools
import random

class Card (object):
    def __init__ (self):
        self.suit = ["Cubs", "Diamonds", "Hearts", "Spades"]
        self.value = ["2","3","4","5","6","7","8","9","10", "J", "Q", "K", "A"]
        self.deck = []
        for card in itertools.product(self.value, self.suit):
            self.deck.append(card)

class Player(object):
    def __init__ (self, name, card = None):
        self.name = name
        self.carddeck = Card()
        self.card_available = self.carddeck.deck
    def draw_a_card(self):
        self.card = random.choice(self.card_available)
        print "%s has gotten a %s" %(self.name, self.card)
    def __cmp__(self, other):
        if self.card_available.index(self.card) > self.card_available.index(other.card):
            print "%s wins!" %(self.name)
        elif self.card_available.index(self.card) < self.card_available.index(other.card):
            print "%s wins!" %(other.name)

player_1= Player("Player 1")
player_2 = Player("Player 2")
player_3 = Player("Player 3")
player_1.draw_a_card()
player_2.draw_a_card()
player_3.draw_a_card()
cards_deck = Card()
player_1.__cmp__(player_2)

经过多方努力,我成功地比较了1号和2号球员之间的牌,但我不知道如何将队员3纳入比较中。

2 个答案:

答案 0 :(得分:3)

cmp函数用于覆盖比较,因此您可以执行

之类的操作

if player1 > player2: print "Player1 beat player2!"

不是这样你可以在函数内打印一些消息......不返回任何你基本上说它们是相等的(我认为在python中没有从cmp返回任何东西大致相当于返回0),或者更糟糕的是可能提高如果您尝试正常比较会出错...

class Player(object):
    def __init__ (self, name, card = None):
        self.name = name
        self.carddeck = Card()
        self.card_available = self.carddeck.deck
    def draw_a_card(self):
        self.card = random.choice(self.card_available)
        print "%s has gotten a %s" %(self.name, self.card)
    def __cmp__(self, other):
        if self.card_available.index(self.card) > self.card_available.index(other.card):
            return 1
        elif self.card_available.index(self.card) < self.card_available.index(other.card):
            return -1
        return 0

print max([player1,player2,player3]), "Wins!" 
print min([player1,player2,player3]), "Loses!!!" 

是最简单的方法......

你真的在这里忽略了OOP设计的几个方面

class Card:
    def __init__(self,val,suite):
        self.val = val
        self.suite = suite
    def __int__(self):
        return self.val
    def __str__(self):
         card_name = ["2","3","4","5","6","7","8","9","10", "J", "Q", "K", "A"][self.val]
         suite_name = ["Clubs","Spades","Diamonds","Hearts"][self.suite]
         return card_name + " of " + suite_name
    def __cmp__(self,other):
         return cmp(int(self),int(other))

print Card(1,3)
print Card(4,2) < Card(3,2)

注意卡片与甲板无关......它属于甲板

deck = [Card(i,j) for i in range(13) for j in range(4)]
random.shuffle(deck.shuffle)

答案 1 :(得分:0)

正如Joran所说,.__cmp__()是一个运算符重载函数,所以你只需要调用player1 > player2而不是player_1.__cmp__(player_2),你也可以将玩家存储在一个列表中,并在每个回合后对列表进行排序。你仍然可以正常方式引用玩家。例如:

player_1= Player("Player 1")
player_2 = Player("Player 2")
player_3 = Player("Player 3")
exampleList = [player1,player2,player3]
player_1.draw_a_card()
player_2.draw_a_card()
player_3.draw_a_card()
exampleList.sort()
winner = exampleList[0]

有关列表排序的更多信息:https://wiki.python.org/moin/HowTo/Sorting