2017-05-18 82 views
0

我創建了一個丟棄方法,用一隻手移除一張或幾張卡片(由用戶指定),並用卡片從卡片中取代它們。在python中創建一個丟棄撲克牌方法

我有一個列表中的所有名片,我創建了一個窗口,按鈕和一個輸入框。

我打算做的是從輸入框中取出輸入,並用隨機卡取代卡片,然後放回手中。

但是,我的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# 
+0

無關,'list'是一個糟糕的變量名,因爲它會隱藏名爲'list'的類型。 –

回答

0

remove_card功能似乎並沒有幹活。我猜測 的getText函數沒有正確地獲得輸入不。

不,你的代碼來測試cards.replace()被打破,這條線的remove_card()不起作用:

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

沒有爲類型列表中沒有.replace()方法,你不需要也無妨。修復此功能,並與card_list替換變量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) 
+0

非常感謝! – Jane

+0

@Jane,不客氣。請注意,這個'remove_card()'函數的缺陷方式與您的手分配(隨機樣本)代碼相同 - 它們不會從card_list中移除它們選擇的卡,因此可能會在重新分配卡時重新分配卡它不應該。然而,這似乎是一個相對容易解決的問題。 – cdlane