Data structure representing a deck of cards

Asked

Viewed 1,588 times

9

I’m developing a project in Python, where I have to play a card game. I am in doubt about which data structure to use, according to the description provided:

Each card must be represented by a pair (,), in which both elements are strings. The possible faces are: ː A', ?2', ?3', ..., ?10', ?J', ?Q', ?K'. The suits are: ?O', ?P', 'C' and 'E'.

For example:

  • The ace of spades is represented by the pair (i.e., A','E');
  • The Queen of clubs, by the pair (i.e., Q','P');

A complete deck is a list of the 52 existing cards. A hand is also a list of cards. The game has several rounds, in each round a new deck is used, whose sequence of cards should be read
of a file prabaralho_.txt', where it should be replaced by the round number. Where the hand is the set of two cards a player has at any given time. I’m new to the world of Python, I don’t know which one is more suitable. The deck could be a dictionary but then I can’t represent the hand.

Excerpt from the deck file:

10 C
8 C
A P
2 P
7 P
4 C
10 O
7 E
9 E
8 E

2 answers

11


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 File? And what would be the content of it? Anyway, I believe that with what I put in the answer you can already do.

  • It is a . txt with face and suit, one in each line Example: Q 0 A P

  • @Alves place in the question a snippet of this file, please.

  • I edited the post with an excerpt from a file.

  • @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.

  • Got the answer, thanks for the help ;)

Show 2 more comments

0

One way I found was to make only a list with the value of the cards like this:

baralho = ['A♥', 'K♥', 'Q♥', 'J♥', 'T♥', '9♥', '8♥', '7♥', '6♥', '5♥', '4♥','3♥', '2♥','A♠', 'K♠', 'Q♠', 'J♠', 'T♠','9♠', '8♠', '7♠', '6♠', '5♠', '4♠', '3♠', '2♠','A♦','K♦','Q♦', 'J♦', 'T♦', '9♦', '8♦', '7♦', '6♦', '5♦', '4♦', '3♦', '2♦', 'A♣', 'K♣', 'Q♣', 'J♣', 'T♣', '9♣', '8♣', '7♣', '6♣', '5♣', '4♣', '3♣', '2♣']

Easy to handle; You can use a random.shuffle() to shuffle... Note: You can replace emojis with O P C E, Diamonds, Clubs... T = 10

Browser other questions tagged

You are not signed in. Login or sign up in order to post.