One option is to use collections.namedtuples
:
from collections import namedtuple
from itertools import product
Carta = namedtuple('Carta', ['face', 'naipe'])
faces = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'}
naipes = {'O', 'P', 'C', 'E'}
baralho = [Carta(face, naipe) for face, naipe in product(faces, naipes)]
print('Seu baralho possui', len(baralho), 'cartas')
# Seu baralho possui 52 cartas
See working on Ideone | Repl.it
To generate all cards in the deck, I used the function itertools.product
. So for each card in the deck you can do, for example:
carta = baralho[0]
print(carta.face, carta.naipe)
Displaying the face and suit of the respective card.
To randomly draw two cards for a single player, you can use the function random.sample
:
from random import sample
mao = sample(baralho, 2)
print('Mão do jogador:', mao)
# Mão do jogador: [Carta(face='Q', naipe='O'), Carta(face='A', naipe='P')]
See working on Ideone | Repl.it
Given the new information about the deck being stored in a file, the logic does not differ much, just, instead of generating all the possible cards, read them from the respective file. Assuming the file has the name of baralho1.txt
, the definition of the deck would be something like:
from collections import namedtuple
Carta = namedtuple('Carta', ['face', 'naipe'])
baralho = []
with open('baralho1.txt') as ficheiro:
for linha in ficheiro:
carta = linha.strip().split()
baralho.append(Carta(*carta))
See working on Repl.it
Thus the object baralho
will be a list of tuples Carta
following the values that were stored in the file. For the snippet of the file given in the question, the result of print(baralho)
would be:
[
Carta(face='10', naipe='C'),
Carta(face='8', naipe='C'),
Carta(face='A', naipe='P'),
Carta(face='2', naipe='P'),
Carta(face='7', naipe='P'),
Carta(face='4', naipe='C'),
Carta(face='10', naipe='O'),
Carta(face='7', naipe='E'),
Carta(face='9', naipe='E'),
Carta(face='8', naipe='E')
]
You can shuffle the cards through the function random.shuffle
:
random.shuffle(baralho)
And then, you could, for example, distribute the card between four players:
jogador_a = baralho[0:2]
jogador_b = baralho[2:4]
jogador_c = baralho[4:6]
jogador_d = baralho[6:8]
See working on Repl.it
Each one with two separate cards. Obviously, the process of dealing cards can be modified according to your needs and even the way you represent the player, if there are other requirements to be met.
I edited the post, forgot to mention that the deck is loaded from a file each round. The hand you have set is of what type?
– alves
@Alves File? And what would be the content of it? Anyway, I believe that with what I put in the answer you can already do.
– Woss
It is a . txt with face and suit, one in each line Example: Q 0 A P
– alves
@Alves place in the question a snippet of this file, please.
– Woss
I edited the post with an excerpt from a file.
– alves
@Alves added to the response an example of how it could be read from the file. I believe that from this you can do it yourself.
– Woss
Got the answer, thanks for the help ;)
– alves