2017-05-18 6 views
0

I'am。Pythonで廃棄ポーカーカード方式の作成手に(ユーザーによって示される)一つまたは複数のカードを削除し、デッキからカードに置き換える廃棄方法を作成

私は私のリストにあるすべてのカードのリストを持っていたと私は、ウィンドウ、ボタン、入力ボックスを作成しました。私がやることを計画何

は、入力ボックスから入力を取り、ランダムカードで示されたカードを交換することでした。その後、手を返します。

しかし、私のremove_card機能はwork.Iが正しく入力を取得していないの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関数が正しく入力を取得していない推測していwork.Iしていないようです。

いいえ、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 

ハンドテストこのコードを、現在のテストコードに依存しないでください。

あなたがこれらに類似のループの4つのすべて置き換えることができます。ただ一つのループ(再びlist - >card_list)で

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

を:

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

ありがとうございました! – Jane

+0

@Janeは、どういたしまして。この 'remove_card()'関数はあなたの手の割り当て(ランダムサンプル)コードと同じように欠陥があることに注意してください - 彼らはカードを取り外さないでください、彼らはcard_list' 'から選択するので、カードは、いつ新たに割り当てられてしまうかもしれませんそれはすべきではない。しかし、修正するのは比較的簡単な問題のようです。 – cdlane

関連する問題