【问题标题】:Creating a discard poker card method in python在python中创建一个丢弃扑克牌的方法
【发布时间】:2017-05-18 16:53:45
【问题描述】:

我正在创建一种丢弃方法,该方法可以移除手中的一张或多张牌(由用户指定),并用牌堆中的牌替换它们。

我有一个列表中所有卡片的列表,我创建了一个窗口、按钮和一个输入框。

我打算做的是从输入框中获取输入并将指示的卡替换为随机卡;然后返回手牌。

但是,我的 remove_card 函数似乎不起作用。我猜 getText 函数没有正确获取输入。

from graphics import*
suits = ["c","d","h","s"]
ranks=["a","2","3","4","5","6","7","8","9","t","j","q","k"]
list=[]
x=0
for items in ranks:
    list.append(ranks[x]+suits[0])
    x=x+1

x=0
for items in ranks:
    list.append(ranks[x]+suits[1])
    x=x+1

x=0
for items in ranks:
    list.append(ranks[x]+suits[2])
    x=x+1

x=0
for items in ranks:
    list.append(ranks[x]+suits[3])
    x=x+1

#creat a list of all the card#

import random
hand=random.sample(list,5)

#select 5 random cards#

def shuffle(hand):
        x=50
        for cards in hand:
                i=Image(Point(x,100),"C:/ICS/cards/"+cards+".gif")
                i.draw(w)
                x=x+20
#a function that prints card#

def remove_card(cards):
    g=[]
    inputStr=inputBox.getText()
    for card in cards:
        if card==inputStr:
            card=cards.replace(inputStr,random.choice(list))
            g.append(card)
        else:
            g.append(card)
    return g


from graphics import *
w=GraphWin('My Window', 400, 200)
i=Point(100,100)
aRectangle=Rectangle(Point(10,150),Point(100,190))
aRectangle.setFill('green')
message=Text(Point(55,170),"Deal")
aRectangle2=Rectangle(Point(150,150),Point(240,190))
aRectangle2.setFill('red')
aRectangle3=Rectangle(Point(150,10),Point(250,50))
message2=Text(Point(195,170),"Quit")
aRectangle.draw(w)
aRectangle2.draw(w)
aRectangle3.draw(w)
message.draw(w)
message2.draw(w)
#drawing all the basics of the deck#
hand=random.sample(list,5)
shuffle(hand)#shuffle cards#
remove=Text(Point(340,130),"Chance to Discards")
remove.draw(w)
inputBox=Entry(Point(350,150),20)
inputBox.draw(w)
hand=remove_card(hand)

while True:
        p3=w.getMouse() #if the point is in the range of quit, close the window#
        if p3.getX()>150 and p3.getX()<240 and p3.getY()>150 and p3.getY()<190:
                w.close()
                break
        elif p3.getX()>10 and p3.getX()<100 and p3.getY()>150 and p3.getY()<190:
                hand=random.sample(list,5)#if the point is inside the range of deal, deal card#
                shuffle(hand) #change hand#

【问题讨论】:

  • 不相关—list 是一个错误的变量名,因为它隐藏了名为 list 的类型。

标签: python poker zelle-graphics


【解决方案1】:

remove_card 功能似乎不起作用。我猜是 getText 函数没有正确获取输入。

不,您用于测试 cards.replace() 的代码已损坏,这行 remove_card() 不起作用:

card=cards.replace(inputStr,random.choice(list))

类型列表没有.replace() 方法,你也不需要它。修复此函数,并将变量list 替换为card_list

def remove_card(cards):
    hand = []

    text = inputBox.getText()

    for card in cards:
        if card == text:
            hand.append(random.choice(card_list))
        else:
            hand.append(card)

    return hand

手动测试此代码,不要依赖您当前的测试代码。

您可以替换所有这四个类似的循环:

x=0
for items in ranks:
    list.append(ranks[x]+suits[0])
    x=x+1

只有一个循环(再次list -> card_list):

for suit in suits:
    for rank in ranks:
        card_list.append(rank + suit)

【讨论】:

  • 非常感谢!
  • @Jane,不客气。请注意,这个remove_card() 函数的缺陷方式与您的手牌分配(随机样本)代码相同——它们不会删除他们从card_list 中选择的牌,因此可能会在应该重新分配牌时重新分配牌不。然而,这似乎是一个相对容易解决的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多