python 我如何洗牌的值并打印2手牌?

pn9klfpd  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(131)

我试图洗牌,并从洗牌甲板打印出2手像在扑克(所以10卡总数)。但不是连接到原始代码本身,我做了一个单独的块,将洗牌,并得到2手,不知道如何连接到原始代码。
我需要洗牌什么下面,并得到两个手的卡

dCardNames = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
dCardValues = ['2','3','4','5','6','7','8','9','10','11','12','13','14']
dSuits = ["Clubs","Spades","Diamonds","Hearts"]

# Build a two dimensional deck with Cards suits and values.
aCards =  [['' for i in range(52)] for j in range(3)]
i = 0
n = 0
while i < 13:
    aCards[0][i] = dCardNames[i]
    aCards[0][i + 13] = dCardNames[i]
    aCards[0][i + 26] = dCardNames[i]
    aCards[0][i + 39] = dCardNames[i]

    aCards[1][i] = dSuits[0]
    aCards[1][i + 13] = dSuits[1]
    aCards[1][i + 26] = dSuits[2]
    aCards[1][i + 39] = dSuits[3]
    aCards[2][i] = dCardValues[i]
    aCards[2][i + 13] = dCardValues[i]
    aCards[2][i + 26] = dCardValues[i]
    aCards[2][i + 39] = dCardValues[i]
    
    i = i + 1 
    
i = 0
while i < 52:
    print (aCards[0][i], " ", aCards[1][i], " ", aCards[2][i])
    i = i + 1

^这是原始代码

import random
hands = {}
card_values = {1:"1", 2:"2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "10", 11: "J", 12: "Q", 13: "K", 14: "A"}
card_types = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}
deck = []
for i_type in range(1,5):
    for i_value in range(1, 15):
        deck.append(card_types[i_type] + " " + card_values[i_value])
        
# Could be handled as inputs
#hands_amt = int(input("How many players?: "))
#cards_per_hand = int(input("How many cards per player?: "))

#or set value
hands_amt = 2
cards_per_hand = 5
for i_hands in range(1, hands_amt+1):
    my_cards = []
    for i_cardamt in range(1, cards_per_hand + 1):
        my_card = random.choice(deck)
        my_cards.append(my_card)
        deck.remove(my_card)
    hands[i_hands] = my_cards
print(hands)

这是我用来洗牌的代码我知道我做了一套全新的牌和上面的那一套完全没有关系
我该如何将最后一个方块与最上面的方块连接起来呢?
我的预期输出是:

3   Hearts   3
4   Hearts   4
5   Hearts   5
6   Hearts   6
7   Hearts   7
8   Hearts   8
9   Hearts   9
10   Hearts   10
J   Hearts   11
Q   Hearts   12
K   Hearts   13
A   Hearts   14

#piece below should be randomized
Hand 1:
Hearts 1
Clubs J
Diamonds 3 
Diamonds J
Clubs 1 

Hand 2
Diamonds 5
Clubs K
Spades 4
Clubs 3
Clubs 6

这是我通常得到的

2   Clubs   2
3   Clubs   3
4   Clubs   4
5   Clubs   5
6   Clubs   6
7   Clubs   7
8   Clubs   8
9   Clubs   9
10   Clubs   10
J   Clubs   11
Q   Clubs   12
K   Clubs   13
A   Clubs   14
2   Spades   2
3   Spades   3
4   Spades   4
5   Spades   5
6   Spades   6
7   Spades   7
8   Spades   8
9   Spades   9
10   Spades   10
J   Spades   11
Q   Spades   12
K   Spades   13
A   Spades   14
2   Diamonds   2
3   Diamonds   3
4   Diamonds   4
5   Diamonds   5
6   Diamonds   6
7   Diamonds   7
8   Diamonds   8
9   Diamonds   9
10   Diamonds   10
J   Diamonds   11
Q   Diamonds   12
K   Diamonds   13
A   Diamonds   14
2   Hearts   2
3   Hearts   3
4   Hearts   4
5   Hearts   5
6   Hearts   6
7   Hearts   7
8   Hearts   8
9   Hearts   9
10   Hearts   10
J   Hearts   11
Q   Hearts   12
K   Hearts   13
A   Hearts   14
{1: ['Hearts K', 'Diamonds 9', 'Hearts 6', 'Hearts 5', 'Clubs 9'], 2: ['Hearts 1', 'Diamonds 1', 'Hearts Q', 'Diamonds A', 'Diamonds 4']}
8fq7wneg

8fq7wneg1#

我不是很清楚你的问题,但是你告诉我你正在学习python,我决定帮你做一些实现,我希望这能激励你学习新的编码概念和习惯用法。
我在这里使用的一些功能包括:enumsdataclassesitertools,覆盖__repr__,f字符串和slice syntax
查看注解以获取其他提示。

from dataclasses import dataclass
from enum import Enum
from itertools import product, starmap
from random import shuffle

# an enum is a special class that you can use when you want to 
# limit the instances to specific values
Suit = Enum('Suit', {'Clubs':'♣','Spades':'♠','Diamonds':'♦','Hearts':'♥'})
# by starting at 2, values are incremental up to 14
Pip = Enum('Pip', ['2','3','4','5','6','7','8','9','10','J','Q','K','A'], start=2)

# the nice thing of dataclasses, is that it automatically implements 
# most of the class behavior, including constructors
@dataclass
class Card:
    suit: Suit
    pip: Pip
    # overriding __repr__ to make it print like 2♣
    def __repr__(self):
        return f'{self.pip.name}{self.suit.value}'
    # calculate the value of the card based on the pip
    def value(self):
        return self.pip.value

# product makes every possible combination of suits and pips
deck = list(starmap(Card, product(Suit, Pip)))
print(*(f'{card}: {card.value()}' for card in deck), sep='\n')

ncards, nhands = 5, 2
shuffle(deck)
# this line is all you need to deals the hands
hands = [deck[i:i+ncards] for i in range(0, ncards*nhands, ncards)]
print(*hands, sep='\n')

相关问题