我该如何为4位玩家洗牌,并确保他们都获得不同的牌? 我不断为不同的玩家获得相同的卡。
正如您在下面的代码中看到的>>>>>,我试图通过使用以下方法解决此问题:“ carddeck.remove(carddeck [i])”。
显然不是那么有用-出于简单的原因,它只是!!!阻止下一位玩家获得与前一张相同的卡。 但这并没有阻止向玩家4和玩家1获得相同卡的选择。
我认为这里有些我想念的东西。
随机播放甲板
import itertools
import random
carddeck = list(itertools.product(range(1,14),["Spade", "Club", "Diamond","Heart"]))
#print(carddeck)
#random.shuffle(carddeck)
#print (carddeck)
def player1(n):
for i in range(n):
carddeck = list(itertools.product(range(1,14),["Spade", "Club", "Diamond", "Heart"]))
random.shuffle(carddeck)
print("Player1:",carddeck[i][0],carddeck[i][1])
carddeck.remove(carddeck[i])
#print (carddeck)
def player2(n):
for j in range(n):
random.shuffle(carddeck)
print("Player2:",carddeck[j][0],carddeck[j][1])
carddeck.remove(carddeck[j])
def player3(n):
for f in range(n):
random.shuffle(carddeck)
print("Player3:",carddeck[f][0],carddeck[f][1])
carddeck.remove(carddeck[f])
def player4(n):
for g in range(n):
random.shuffle(carddeck)
print("Player4:", carddeck[g][0],carddeck[g][1])
player1(2)
player2(2)
player3(2)
player4(2)
输出
Player1: 3 Club
Player1: 5 Spade
Player2: 7 Diamond
Player2: 10 Diamond
Player3: 3 Club
Player3: 2 Spade
Player4: 13 Diamond
Player4: 1 Spade
答案 0 :(得分:0)
我正在尝试做这样的事情。希望对您有帮助!
# Python program to shuffle a deck of card using the module random and draw 5 cards
# import modules
import itertools, random
# make a deck of cards
deck = list(itertools.product([2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'],
['Spade', 'Heart', 'Diamond', 'Club']))
# shuffle the cards
random.shuffle(deck)
# draw cards, a total of 48 cards divided among 6 players
print("Player 1 got:")
for i in range(8):
print(deck[i][0], "of", deck[i][1])
deck.remove(deck[i])
print("Player 2 got:")
for j in range(8):
print(deck[j][0], "of", deck[j][1])
deck.remove(deck[j])
print("Player 3 got:")
for k in range(8):
print(deck[k][0], "of", deck[k][1])
deck.remove(deck[k])
print("Player 4 got:")
for l in range(8):
print(deck[l][0], "of", deck[l][1])
deck.remove(deck[l])
print("Player 5 got:")
for m in range(8):
print(deck[m][0], "of", deck[m][1])
deck.remove(deck[m])
print("Player 6 got:")
for n in range(8):
print(deck[n][0], "of", deck[n][1])