2017-05-18 16 views
0

손에 하나 또는 여러 개의 카드 (사용자가 표시 한 카드)를 제거하고 카드를 데크에서 카드로 교체하는 삭제 방법을 만듭니다.파이썬에서 폐기 포커 카드 방법 만들기

나는 내 목록에있는 모든 카드 목록을 가지고 있으며 창, 버튼 및 항목 상자를 만들었습니다.

내가 계획 한 것은 입력 상자에서 입력을 가져 와서 임의의 카드로 표시된 카드를 교체 한 다음 손을 반환하는 것입니다.

그러나 내 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 

손 시험이 코드를, 현재의 테스트 코드에 의존하지 않습니다.

당신이 유사한 루프의 네 대체 할 수있다 : (다시 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