0

Im trying to shuffle the cards, and from the shuffled deck print out 2 hands like in poker (so 10 cards total). but rather than connecting it to the original code itself i made a seperate block that'll shuffle and get the 2 hands and dont know how to connect it to the original code.

need to shuffle whats below and get two hands of the cards



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

^thats the original code

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)

this is the code i made to shuffle the cards. I know i made a comepletely new set that's nothing to do witht the one on top.

how would I connect the last block to the one on top ?

my expected output is:

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

this is what i normally get

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']}
  • 1
    Why not just use `random.shuffle`? – chepner Nov 20 '22 at 01:53
  • It's not clear what you're trying to do, and it's hard to make sense of your explanation. As a first, specific, question: why do you **want** an output that begins `3 Hearts 3`, `4 Hearts 4`,..., `A Hearts 14`? What are these lines supposed to mean? – Ben Grossmann Nov 20 '22 at 01:55
  • Thats just the values of the cards, E.X J has a value of 10, say the card is a diamonds it'll be ```J Diamond 10```,all i need is to shuffle the cards and print 2 hands @BenGrossmann – William Rivas Nov 20 '22 at 02:14

1 Answers1

0

I am not completely clear on your question, but as you told you are learning python, I decided to help you with some implementation that, as I hope, could inspire you and motivate to learn new coding concepts and idioms.

Some of the features I use here are: enums, dataclasses, itertools, overriding __repr__, fstrings and slice syntax.

Check the comments for additional hints.

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')
Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36