在制作纸牌游戏时,我该怎么做,这样就不能两次绘制同一张纸牌

时间:2018-11-13 22:46:25

标签: python-3.x

我正试图弄清楚如何使程序识别出已抽出的卡并将其从卡座中取出,以便无法再次抽出。我知道我可能没有52个清单,但我将每个项目都设为卡名并将其从该清单中拉出并放入新清单中,但是用这种方式看来似乎是不可能的。 那我该怎么做,这样它只能抽一张卡?

import random
import time

played = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0]

def loop():
keepLooping = True
while(keepLooping):
    global played
    print ('The player with the lower card goes first')
    print(' ')

    # player One draw
    draw = input("Player one would you like to draw?(y,n): ")
    if draw == 'y':
                 CardNumber = random.randint(2,14)
                 Num2 = random.randint(2,4)
                 Royal = {11: "Jack",12: "Queen",13: "King",14: "Ace"}
                 cardnum1 = Royal.get(CardNumber, CardNumber)
                 suits = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}
                 cardnum2 = suits[Num2]
                 DrawOne = [cardnum1, cardnum2]
                 print(DrawOne)
                 print(' ')
    if draw == 'n':
        print ('ok')

    # player two draw
    draw = input("Player Two would you like to draw?(y,n): ")
    if draw == 'y':
                 CardNumber2 = random.randint(2,13)
                 Num3 = random.randint(2,4)
                 Royal = {11: "Jack",12: "Queen",13: "King",14: "Ace"}
                 cardnum3 = Royal.get(CardNumber2, CardNumber2)
                 suits = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}
                 cardnum4 = suits[Num3]
                 DrawTwo = (cardnum3, cardnum4)
                 print(DrawTwo)
    if draw == 'n':
        print ('Then you lose')

    # Win/lose/tie   
    if CardNumber == CardNumber2:
        time.sleep(1)
        print(' ')
        print("it was a tie, lets re-draw")
        print(' ')
        keepLooping = True
    else:
        if CardNumber < CardNumber2:
            keepLooping = False
            time.sleep(.5)
            print(' ')
            print (DrawOne, 'Is the lower card, player 1 youre going first')
        if CardNumber > CardNumber2:
            keepLooping = False
            time.sleep(.5)
            print(' ')
            print (DrawTwo, 'Is the lower card, player 2 youre going first') 

    loop()

1 个答案:

答案 0 :(得分:1)

从概念上讲简便的方法(而不使用numpy):

[python-setup]
# Using the modern Pants python backend will allow us to set:
#   compatibility=[ "CPython>=3" ]
# on any python_target we want to enforce as Python3.
interpreter_constraints: ["CPython>=2.7,<3"]
interpreter_search_paths: [
   '%(buildroot)s/.venv/py2/bin',
   '%(buildroot)s/.venv/py3/bin',
  ]

现在,每次绘制时,您都可以从列表中弹出一个。

>>> cards = list(range(52))
>>> random.shuffle(cards)
>>> cards
[7, 28, 1, 49, 27, 36, 26, 16, 32, 23, 45, 19, 31, 13, 44, 5, 37, 3, 39, 29, 42, 11, 46, 6, 2, 0, 15, 14, 48, 38, 9, 51, 10, 20, 43, 25, 18, 12, 8, 21, 47, 4, 33, 24, 41, 50, 35, 17, 40, 22, 34, 30]

然后获取西装和号码,您可以执行以下操作:

>>> card = cards.pop()
>>> card
30

如果不熟悉模(suit = card % 4 # e.g. 0 is hearts, etc. (arbitrary) number = card % 13 + 1 # so that 1 is 1, and 11 is Jack ),请在这里看看:How does % work in Python?