如何创建一个手与13张扑克牌在我

2024-05-08 16:55:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个计算机科学的项目,但是我不知道如何把13张卡片拿到手里。在hand类中有一个内存错误,但是我不知道为什么,它不是每只手放13张卡。以下是我目前的代码:

#Keshav Ramesh
#Project 8

import random

class card:         
    def __init__(self, suit, value):
        self.suit=suit
        self.value=value

    #methods
    def printer(self):
        print("{0}  {1}".format(self.suit, self.value))

    def __lt__(self, other):
        if(self.value == other.value):
            return self.suit < other.suit
        elif (self.value != other.value):
            return self.value < other.value
    def __gt__(self, other):
        return not(self<other)

    def __str__(self):
        return "Suit: " + str(self.suit) + " value: " + str(self.value)

class deck:
    def __init__(self):
        self.x=[]
        self.load()
        random.shuffle(self.x)

    def load(self):
        for i in range(1, 5):
            for j in range(2, 14):
                self.x.append(card(i, j))

    def deal(self):
        return self.x.pop()

p = deck() 
class hand:
    def __init__(self):
        self.x=[]
        self.hand_count=0
        while len(self.x) != 13:
            self.x.append(p.deal())

    def accept(self, a):
        self.x.append(a)
        self.hand_count= self.hand_count + 1

    def play(self):
        self.hand_count = self.hand_count - 1
        return self.x.pop() 

    def handPrinter(self):
        while len(self.x) != 0: 
            result = (self.pop())
            print("{0}  {1}".format(result.suit, result.value))

Tags: selfreturninitvaluedefcountrandomresult
1条回答
网友
1楼 · 发布于 2024-05-08 16:55:27

当你这么做的时候

result = (self.pop())

用手印的方法也许你想

result = (self.x.pop())

在执行代码时导致错误。另外,在Card类的__init__方法中,应该识别self.suit = suitself.value = value。你知道吗

除此之外,通过添加

h = hand()
h.handPrinter()

最后,我觉得一切都很顺利。你知道吗

相关问题 更多 >

    热门问题