Skip to main content
Code Review

Return to Answer

added 519 characters in body
Source Link
Veedrac
  • 9.8k
  • 23
  • 38

The calls to format can be removed by making card like:

CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
""".format('{rank: <2}', '{suit: <2}', '{rank: >2}')

and doing:

def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 # add the individual card on a line by line basis
 return CARD.format(rank=rank, suit=name_to_symbol[card.suit])
CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
""".format('{rank: <2}', '{suit: <2}', '{rank: >2}')
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack
 :return: String consisting of the horizontally stacked input
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :return: A string, the nice ascii version of cards
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]

 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rankrank=rank, ' >2'suit=name_to_symbol[card.suit])
 )
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))
CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
"""
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack
 :return: String consisting of the horizontally stacked input
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :return: A string, the nice ascii version of cards
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]

 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))

The calls to format can be removed by making card like:

CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
""".format('{rank: <2}', '{suit: <2}', '{rank: >2}')

and doing:

def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 # add the individual card on a line by line basis
 return CARD.format(rank=rank, suit=name_to_symbol[card.suit])
CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
""".format('{rank: <2}', '{suit: <2}', '{rank: >2}')
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack
 :return: String consisting of the horizontally stacked input
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :return: A string, the nice ascii version of cards
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 # add the individual card on a line by line basis
 return CARD.format(rank=rank, suit=name_to_symbol[card.suit])
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))
deleted 156 characters in body
Source Link
Veedrac
  • 9.8k
  • 23
  • 38
CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
"""
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack.
 :return: String consisting of the horizontally stacked input. 
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :param return_string: By default we return: theA string version of the card, but the dealer hide the 1st card and we
 keep it as a list so that the dealer can add a hidden cardnice inascii frontversion of the listcards
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]
 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))
CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
"""
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack.
 :return: String consisting of the horizontally stacked input. 
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :param return_string: By default we return the string version of the card, but the dealer hide the 1st card and we
 keep it as a list so that the dealer can add a hidden card in front of the list
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]
 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))
CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
"""
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack
 :return: String consisting of the horizontally stacked input
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :return: A string, the nice ascii version of cards
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]
 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))
Source Link
Veedrac
  • 9.8k
  • 23
  • 38

I know you're not seeking advice about Card, but I feel I should point out that you don't need to inherit from object in Python 3.

I would remove the return_string option; if someone wants a list they should just call splitlines.

Your

suits_name = ['Spades', 'Diamonds', 'Hearts', 'Clubs']
suits_symbols = ['♠', '♦', '♥', '♣']

should probably be tuples:

suits_name = 'Spades', 'Diamonds', 'Hearts', 'Clubs'
suits_symbols = '♠', '♦', '♥', '♣'

but they're only ever used as a mapping:

name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
}

Your

# add the individual card on a line by line basis
lines[0].append('┌─────────┐')
lines[1].append('│{}{} │'.format(rank, space)) # use two {} one for char, one for space or char
lines[2].append('│ │')
lines[3].append('│ │')
lines[4].append('│ {} │'.format(suit))
lines[5].append('│ │')
lines[6].append('│ │')
lines[7].append('│ {}{}│'.format(space, rank))
lines[8].append('└─────────┘')

looks a bit ugly, but I can see why you did that. What might be better is making an auxillary function to stack lines of strings first:

def join_lines(strings):
 string_lines = [string.splitlines() for string in strings]
 return '\n'.join(''.join(out_line) for out_line in zip(*string_lines))

And then do:

card = (
 '┌─────────┐\n'
 '│{r}{_} │\n'
 '│ │\n'
 '│ │\n'
 '│ {s} │\n'
 '│ │\n'
 '│ │\n'
 '│ {_}{r}│\n'
 '└─────────┘\n'
).format(r=rank, _=space, s=suit)
card_strings.append(card)

Since the formatting of the card matters for prettiness, I'd be tempted to do:

 card = (
 '┌─────────┐\n'
 '│{} │\n'
 '│ │\n'
 '│ │\n'
 '│ {} │\n'
 '│ │\n'
 '│ │\n'
 '│ {}│\n'
 '└─────────┘\n'
 ).format(
 format(card.rank, ' <2'),
 format(card.suit, ' <2'),
 format(card.rank, ' >2')
 )

Of course, since card is from a static, you can move it out to get just

 card = CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )

This removes the need for space. You can then make the main loop a comprehension if you make a function, and stick it inside the join_lines call:

def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]
 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )
return join_lines(map(card_to_string, cards))

ascii_version_of_hidden_card can then be just:

def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))

Here's the full thing:

CARD = """\
┌─────────┐
│{} │
│ │
│ │
│ {} │
│ │
│ │
│ {}│
└─────────┘
"""
HIDDEN_CARD = """\
┌─────────┐
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
│░░░░░░░░░│
└─────────┘
"""
def join_lines(strings):
 """
 Stack strings horizontally.
 This doesn't keep lines aligned unless the preceding lines have the same length.
 :param strings: Strings to stack.
 :return: String consisting of the horizontally stacked input. 
 """
 liness = [string.splitlines() for string in strings]
 return '\n'.join(''.join(lines) for lines in zip(*liness))
def ascii_version_of_card(*cards):
 """
 Instead of a boring text version of the card we render an ASCII image of the card.
 :param cards: One or more card objects
 :param return_string: By default we return the string version of the card, but the dealer hide the 1st card and we
 keep it as a list so that the dealer can add a hidden card in front of the list
 """
 # we will use this to prints the appropriate icons for each card
 name_to_symbol = {
 'Spades': '♠',
 'Diamonds': '♦',
 'Hearts': '♥',
 'Clubs': '♣',
 }
 def card_to_string(card):
 # 10 is the only card with a 2-char rank abbreviation
 rank = card.rank if card.rank == '10' else card.rank[0]
 suit = name_to_symbol[card.suit]
 # add the individual card on a line by line basis
 return CARD.format(
 format(rank, ' <2'),
 format(suit, ' <2'),
 format(rank, ' >2')
 )
 return join_lines(map(card_to_string, cards))
def ascii_version_of_hidden_card(*cards):
 """
 Essentially the dealers method of print ascii cards. This method hides the first card, shows it flipped over
 :param cards: A list of card objects, the first will be hidden
 :return: A string, the nice ascii version of cards
 """
 return join_lines((HIDDEN_CARD, ascii_version_of_card(*cards[1:])))
lang-py

AltStyle によって変換されたページ (->オリジナル) /