洗一副牌并将其分发给四名玩家。当有人有 4 颗心时你需要停止游戏并说出谁是赢家

Shuffle a pack of cards and distribute it among four players. You need to stop the game when someone has 4 hearts and say who is the winner

我需要编写一个 python 代码来洗牌并分发给四位玩家。当有人有 4 颗心时,您需要停止游戏并说出谁是赢家。作为 python 的初学者,我直接完成了它。谁能给我优化的编码方式??。这是我的尝试..

import random
colour=["Heart","Spade","Club","Diamond"] 
a=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"] 
player1,player2,player3,player4=[],[],[],[]
shuffle=[{x:y} for x in colour for y in a ]
cnt=0
while len(shuffle)!=0:
    p1,p2,p3,p4=random.sample(population=shuffle,k=4) 

    i1=shuffle.index(p1)
    del shuffle[i1]
    i2=shuffle.index(p2)
    del shuffle[i2]
    i3=shuffle.index(p3)
    del shuffle[i3]
    i4=shuffle.index(p4)
    del shuffle[i4]

    if "Heart" in p1:
        player1.append(p1)
        if len(player1)>=4:
            print("player 1 is win ,heart cards are:",player1)
            break
    if "Heart" in p2:
        player2.append(p2)
        if len(player2)>=4:
            print("player 2 is win,heart cards are:",player2)
            break
    if "Heart" in p3:
        player3.append(p3)
        if len(player3)>=4:
            print("player 3 is win,heart cards are:",player3)
            break
    if "Heart" in p4:
        player4.append(p4)
        if len(player4)>=4:
            print("player 4 is win,heart cards are:",player4)
            break

这副牌有 52 张牌。将它们洗牌一次。发给每位玩家一张牌,直到牌组为空或其中一位玩家有 4 颗心。红心是一张牌,数字是0 mod 4。其他套房是1 mod 4、2 mod 4和3 mod 4.

from random import shuffle

deck = range(52)
shuffle(deck)

from itertools import cycle

players = [[] for _ in range(4)]

for i, p in cycle(enumerate(players)):
    if not deck: break
    p.append(deck.pop())
    if sum(1 for c in p if c % 4 == 0) == 4:
        print i
        break

print players

假设卡片 1-12 是红心。我使用随机播放功能来分发卡片,然后将它们分成 4 个数组。我只是计算每个数组中小于 13 的卡片的最小索引,然后返回 palyer 索引

from random import shuffle
import numpy as np
x = range(52)
shuffle(x)
x = [(i<13)*1 for i in x]
y = [np.cumsum([x[i*4+n] for i in range(13)]) for n in range(4)]
def findmin(x, v):
    try:
        return x.index(v)
    except:
        return 9999

y = [findmin(list(y[i]), 4)for i in range(4)]
y.index(min(y))