2

I made a simple blackjack game using python. I made the rest of the game but i am struggling to put in the ascii cards, so this is just a fraction of the code. I have tried putting * len(phand) at the end of the append lines. While this does give me multiple cards side by side, it doesn't seem to let me edit them.

#Phand is the hand you draw so it could be any combination of cards

phand = ["2 of Hearts", "King of Diamonds", "Ace of Clubs"]
print(phand)

for xx in range(0, len(phand)):
  pcarddisplay = [] 
  pcarddisplay.append("┌─────────┐")
  pcarddisplay.append("│{}{}. . .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . {}. .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . .{}{}│")
  pcarddisplay.append("└─────────┘")

  x = ("│.", phand[xx][:1], ". . . .│")
  pcarddisplay[1] = "".join(x)

  x = ("│. . . .", phand[xx][:1], ".│")
  pcarddisplay[7] = "".join(x)

  if "Diamonds" in phand[xx]:
    pcarddisplay[4] = "│. . ♦ . .│"
  if "Clubs" in phand[xx]:
    pcarddisplay[4] = "│. . ♣ . .│"
  if "Hearts" in phand[xx]:
    pcarddisplay[4] = "│. . ♥ . .│"
  if "Spades" in phand[xx]:
    pcarddisplay[4] = "│. . ♠ . .│"

  print("\n".join(pcarddisplay))  
  

This outputs:

['2 of Hearts', 'King of Diamonds', 'Ace of Clubs']
┌─────────┐
│.2. . . .│
│. . . . .│
│. . . . .│
│. . ♥ . .│
│. . . . .│
│. . . . .│
│. . . .2.│
└─────────┘
┌─────────┐
│.K. . . .│
│. . . . .│
│. . . . .│
│. . ♦ . .│
│. . . . .│
│. . . . .│
│. . . .K.│
└─────────┘
┌─────────┐
│.A. . . .│
│. . . . .│
│. . . . .│
│. . ♣ . .│
│. . . . .│
│. . . . .│
│. . . .A.│
└─────────┘

How could I go about making these print out side by side?

  • You can append each line to a "holding" variable like `pcarddisplay[4] = pcarddisplay[4] + "│. . X . .│"` and then print them all at once – yuuuu Nov 12 '21 at 16:39

1 Answers1

5

You have to change your code to produce one card (here the mk_card function).

Then produce all the cards chunks and use a combination of zip and join to produce the lines:

phand = ["2 of Hearts", "King of Diamonds", "Ace of Clubs"]

def mk_card(s):
  pcarddisplay = [] 
  pcarddisplay.append("┌─────────┐")
  pcarddisplay.append("│{}{}. . .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . {}. .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . . . .│")
  pcarddisplay.append("│. . .{}{}│")
  pcarddisplay.append("└─────────┘")

  x = ("│.", s[:1], ". . . .│")
  pcarddisplay[1] = "".join(x)

  x = ("│. . . .", s[:1], ".│")
  pcarddisplay[7] = "".join(x)

  if "Diamonds" in s:
    pcarddisplay[4] = "│. . ♦ . .│"
  if "Clubs" in s:
    pcarddisplay[4] = "│. . ♣ . .│"
  if "Hearts" in s:
    pcarddisplay[4] = "│. . ♥ . .│"
  if "Spades" in s:
    pcarddisplay[4] = "│. . ♠ . .│"

  return pcarddisplay

print('\n'.join(map('  '.join, zip(*(mk_card(c) for c in phand)))))

output:

┌─────────┐  ┌─────────┐  ┌─────────┐
│.2. . . .│  │.K. . . .│  │.A. . . .│
│. . . . .│  │. . . . .│  │. . . . .│
│. . . . .│  │. . . . .│  │. . . . .│
│. . ♥ . .│  │. . ♦ . .│  │. . ♣ . .│
│. . . . .│  │. . . . .│  │. . . . .│
│. . . . .│  │. . . . .│  │. . . . .│
│. . . .2.│  │. . . .K.│  │. . . .A.│
└─────────┘  └─────────┘  └─────────┘
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thank you so much for the quick reply. This works perfectly thank you. – ORKUN CIRAK Nov 12 '21 at 16:42
  • You're welcome @ORKUNCIRAK happy coding ;) – mozway Nov 12 '21 at 16:42
  • Nice opportunity to show how `map` and generator expressions are related. The answer used both. If you prefer generator expressions it would be `print('\n'.join(' '.join(mc) for mc in zip(*(mk_card(c) for c in phand))))` and if you prefer `map()`: `print('\n'.join(map(' '.join, zip(*map(mk_card, phand)))))` – VPfB Nov 12 '21 at 17:10