【问题标题】:Dealing cards and returning leftovers发牌和返还剩菜
【发布时间】:2016-11-22 23:47:50
【问题描述】:

我正在尝试制作卡片交易程序。

  • 有 5 名玩家。
  • 已给出甲板顺序。
  • 程序从牌堆的顶部一次发一张牌。
    • 桌子的顶部是堆栈的开始。
  • 程序返回:
    • 5 人手和
    • 卡包中的卡片。

功能:

def deal_cards(FIVE,card_list):
    card_list = = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] * 4
    hand_one = []
    hand_two = []
    hand_three = []
    hand_four = []
    hand_five = []
    for i in card_list:
        hand_one = card_list.append(i)
        hand_two = card_list.pop(i + 1)
        hand_three = card_list.pop(i + 2)
        hand_four = card_list.pop(i + 3)
        hand_five = card_list.pop(i + 4)

    return hand_one, hand_two, hand_three, hand_four, hand_five

主要:

print("Deck - ", end="")
print(*card_list)
hand_one, hand_two, hand_three, hand_four, hand_five = deal_cards(card_list)
print("Player 1 - ", end="")
print(*hand_one)
print("Player 2 - ", end="")
print(*hand_two)
print("Player 3 - ", end="")
print(*hand_three)
print("Player 4 - ", end="")
print(*hand_four)
print("Player 5 - ", end="")
print(*hand_five)

【问题讨论】:

  • 好的,你的问题是什么?
  • 我不知道从这里做什么,也无法让代码工作。返回剩余值也是如此。我主要质疑我将牌放在玩家手中以查看其是否最佳的方式。我得到空白打印这个
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布代码并准确描述问题之前,我们无法有效地帮助您。
  • 您给定的代码无法编译,更不用说运行了。你没有具体说明问题。 “我不知道从这里做什么,无法让代码工作”不是问题规范。

标签: python string python-3.x


【解决方案1】:

我以前用过这个:

colors=[('spades','♠'), ('hearts','♥'),('diamonds','♦'),('clubs','♣')]
colors=[s for (n,s) in colors]
faces=['A','2','3','4','5','6','7','8','9','1','J','Q','K']

cards=list(__import__("itertools").product(colors, faces))

__import__("random").shuffle(cards)

nbplayers=5
nbcards=6

def cardstostr(cards):
    return ",".join(map(lambda (c,f): f+c, cards))

for i in range(nbplayers):
    print cardstostr(cards[nbcards*i:nbcards*(i+1)])
print cardstostr(cards[nbplayers*nbcards:])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多