我一直在努力弄清楚如何将这个程序转换成两个玩家之间的一个而不仅仅是经销商和我。它是否应该以某种方式循环,将循环的第一次迭代给予玩家1而将另一次迭代给予玩家2?我不太确定如何使它发挥作用。
def main():
deck = create_deck()
num_cards = int(input('How many cards should I deal? '))
deal_cards(deck, num_cards)
def create_deck():
deck = {'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3,
'4 of Spades':4, '5 of Spades':5, '6 of Spades':6,
'7 of Spades':7, '8 of Spades':8, '9 of Spades':9,
'10 of Spades':10, 'Jack of Spades':10,
'Queen of Spades':10, 'King of Spades': 10,
'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3,
'4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6,
'7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9,
'10 of Hearts':10, 'Jack of Hearts':10,
'Queen of Hearts':10, 'King of Hearts': 10, 'Ace of Clubs':1,
'2 of Clubs':2, '3 of Clubs':3, '4 of Clubs':4, '5 of Clubs':5,
'6 of Clubs':6,'7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9,
'10 of Clubs':10, 'Jack of Clubs':10,
'Queen of Clubs':10, 'King of Clubs': 10,
'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3,
'4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6,
'7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9,
'10 of Diamonds':10, 'Jack of Diamonds':10,
'Queen of Diamonds':10, 'King of Diamonds': 10}
return deck
def deal_cards(deck, number):
hand_value = 0
if number > len(deck):
number = len(deck)
for count in range(number):
card, value = deck.popitem()
print(card)
hand_value += value
print('Value of this hand:', hand_value)
main()
答案 0 :(得分:1)
您发布的代码仅处理用户指定数量的卡片(它们不是随机处理的),总结它们的值并终止。如果你想让这个游戏连续发牌,你必须在主函数中添加一个循环,如下所示:
def main():
deck = create_deck()
while True:
num_cards = int(input('How many cards should I deal? '))
deal_cards(deck, num_cards)
请注意,这不会在交易之间汇总卡。要做到这一点,你需要在main函数中创建一个像p1或p2这样的变量,并将其传递给deal_cards。我不太确定这是不是你想要的。
答案 1 :(得分:1)
我建议尝试这样的事情:
import random
deck = {'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3,
'4 of Spades':4, '5 of Spades':5, '6 of Spades':6,
'7 of Spades':7, '8 of Spades':8, '9 of Spades':9,
'10 of Spades':10, 'Jack of Spades':10,
'Queen of Spades':10, 'King of Spades': 10,
'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3,
'4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6,
'7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9,
'10 of Hearts':10, 'Jack of Hearts':10,
'Queen of Hearts':10, 'King of Hearts': 10, 'Ace of Clubs':1,
'2 of Clubs':2, '3 of Clubs':3, '4 of Clubs':4, '5 of Clubs':5,
'6 of Clubs':6,'7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9,
'10 of Clubs':10, 'Jack of Clubs':10,
'Queen of Clubs':10, 'King of Clubs': 10,
'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3,
'4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6,
'7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9,
'10 of Diamonds':10, 'Jack of Diamonds':10,
'Queen of Diamonds':10, 'King of Diamonds': 10}
def value(hand):
hand_value = 0
for i in hand:
hand_value += deck[i]
return hand_value
def main():
keys = list(deck)
random.shuffle(keys)
player1_hand = []
player2_hand = []
#dealing to player 1
while True:
print ("Player 1's hand is ", player1_hand, "The value is ", value(player1_hand))
response = int(input('How many cards should I deal to player1? '))
if response == 0:
break
else:
for i in range(int(response)):
player1_hand.append(keys.pop())
#dealing to player 2
while True:
print ("Player 2's hand is ", player2_hand, "The value is ", value(player2_hand))
response = int(input('How many cards should I deal to player2? '))
if response == 0:
break
else:
for i in range(int(response)):
player2_hand.append(keys.pop())
print (player1_hand, value(player1_hand))
print (player2_hand, value(player2_hand))
if __name__ == "__main__":
main()
一些变化: 1)create_deck函数是不必要的,因为它只返回一个不变的变量,因此它被删除了。 2)在牌组上使用Random.shuffle,这样游戏就不会一样。 3)添加'如果name == main'。 这是一个简单的公式,玩家1可以获得所需数量的牌,然后玩家2也会这样做。你可以循环处理while循环,或者写一些单独的函数来指定轮数。在玩家完成发牌后,您还可以尝试打印很多新的线条角色,以便其他玩家无法看到发给他们的内容。