0

I am making a card game and I want to display the cards graphically with strings. However, with my current method that are placed underneath each other. How can I put them next to each other without messing up the visuals?

def display_card(card):
    
    suit = card[0]
    value = card[1]
    
    graphic_card = (
        '┌─────────┐\n'
        '│{}       │\n'
        '│         │\n'
        '│         │\n'
        '│    {}   │\n'
        '│         │\n'
        '│         │\n'
        '│       {}│\n'
        '└─────────┘'
    ).format(
        format(value, ' <2'),
        format(suit, ' <2'),
        format(value, ' >2')
    )

    print(graphic_card)
    
 
cards= ["♥2", "♥3", "♥4"]
for card in cards:
    display_card(card)
Y.Ynot
  • 337
  • 5
  • 15
  • You can use Ansi-Escape sequences (http://ascii-table.com/ansi-escape-sequences.php) to move the cursor position and write to where you want. – luizinho Jan 12 '21 at 10:52
  • Thanks for the hint. I don't really understand this documentation though. What is cursor position? Should I then put it on the left-center of the card? – Y.Ynot Jan 12 '21 at 13:14
  • Normally, we can use this [solution](https://stackoverflow.com/a/493399/9500955). But in your case, it has a problem with `\n` at the end of each line, it just cannot print next to each other card because of it. So you should come up with another idea of creating `graphic_card`. – huy Jan 12 '21 at 13:29
  • Instead of looping over `cards`, you need to loop over the rows and print each row with all cards – Tomerikoo Jan 12 '21 at 14:25

2 Answers2

3

It's probably simpler if you change your method to just return a list of the lines for the cards, then you can zip those together and print them.

def get_card(card):
    suit = card[0]
    value = card[1:]  # 1: for '10'
    return (
        '┌─────────┐\n'
        '│{}       │\n'
        '│         │\n'
        '│         │\n'
        '│    {}   │\n'
        '│         │\n'
        '│         │\n'
        '│       {}│\n'
        '└─────────┘'
    ).format(
        format(value, ' <2'),
        format(suit, ' <2'),
        format(value, ' >2')
    ).splitlines()

def display_cards(cards):
    for lines in zip(*map(get_card, cards)):
        print(*lines)
 
cards= ["♥2", "♥3", "♥4"]
display_cards(cards)
tobias_k
  • 81,265
  • 12
  • 120
  • 179
1

You can try this code:

from collections import defaultdict

def solution(cards):
    
    # Create a dictionary with
    # key: the line number
    # value: a list contain value of each line from each card
    dic = defaultdict(list)
    
    for card in cards:
        # Loop through each card
        
        # Get value from each card
        suit = card[0]
        value = card[1]
        
        for i in range(0, 9):
            # Add value to each line
            # Example value for key 1 after 3 loops:
            # dic[1] = ['│2        │', '│3        │', '│4        │']
            if i == 0:
                dic[i].append('┌─────────┐')
            elif i == 1:
                dic[i].append('│{}       │'.format(format(value, ' <2')))
            elif i == 4:
                dic[i].append('│    {}   │'.format(format(suit, ' <2')))
            elif i == 7:
                dic[i].append('│       {}│'.format(format(value, ' <2')))
            elif i == 8:
                dic[i].append('└─────────┘')
            else:
                dic[i].append('│         │')
                
    for i in range(0, 9):
        for a in range(0, len(cards)):
            # end=" " to not add a newline to the end of the string
            print(dic[i][a], end=" ")
        print("")

a = ["♥2", "♥3", "♥4"]

solution(a)

Output:

┌─────────┐ ┌─────────┐ ┌─────────┐ 
│2        │ │3        │ │4        │ 
│         │ │         │ │         │ 
│         │ │         │ │         │ 
│    ♥    │ │    ♥    │ │    ♥    │ 
│         │ │         │ │         │ 
│         │ │         │ │         │ 
│       2 │ │       3 │ │       4 │ 
└─────────┘ └─────────┘ └─────────┘

You can try it here.

This is not a good way to implement it but it will give you some idea for your problem. With my code, your input has to be a list.

For example with 1 card: a = ["♥2"]

huy
  • 1,648
  • 3
  • 14
  • 40