'how can I each element of an array into one line instead of 6
I'm trying to make a sort of cribbage game in Python, and it's actually going quite well. I've run into a problem though. Whenever I try to print the six 'cards' dealt to the player, it always prints them to 6 different lines. What would I need to use and how would I make it so they just print in one line right next to each other? My code is below:
#If a card has a 'T' on it, it is a 10
import math
import random
from cardArtGenerator import *
cardFaceSeparator = ''
def generateHeartFace(cardValues):
print(cardFaceSeparator.join(" ┌───┐" * len(cardValues)))
print(cardFaceSeparator.join(f" | {c} |" for c in cardValues))
print(cardFaceSeparator.join(" | ♥ |" * len(cardValues)))
print(cardFaceSeparator.join(" └───┘" * len(cardValues)))
def generateClubFace(cardValues):
print(cardFaceSeparator.join(" ┌───┐" * len(cardValues)))
print(cardFaceSeparator.join(f" | {c} |" for c in cardValues))
print(cardFaceSeparator.join(" | ♣ |" * len(cardValues)))
print(cardFaceSeparator.join(" └───┘" * len(cardValues)))
def generateDiamondFace(cardValues):
print(cardFaceSeparator.join(" ┌───┐" * len(cardValues)))
print(cardFaceSeparator.join(f" | {c} |" for c in cardValues))
print(cardFaceSeparator.join(" | ♦ |" * len(cardValues)))
print(cardFaceSeparator.join(" └───┘" * len(cardValues)))
def generateSpadeFace(cardValues):
print(cardFaceSeparator.join(" ┌───┐" * len(cardValues)))
print(cardFaceSeparator.join(f" | {c} |" for c in cardValues))
print(cardFaceSeparator.join(" | ♠ |" * len(cardValues)))
print(cardFaceSeparator.join(" └───┘" * len(cardValues)))
deck = ['dA','d2','d3','d4','d5','d6','d7','d8','d9','dT','dJ','dQ','dK',
'hA','h2','h3','h4','h5','h6','h7','h8','h9','hT','hJ','hQ','hK',
'sA','s2','s3','s4','s5','s6','s7','s8','s9','sT','sJ','sQ','sK',
'cA','c2','c3','c4','c5','c6','c7','c8','c9','cT','cJ','cQ','cK']
hand = []
separator = ''
def numberGenerator():
for x in range(6):
cardDealt = random.choice(deck)
hand.append(cardDealt)
deck.remove(cardDealt)
def cardArt(cardInput):
if cardInput[0] == 'd':
generateDiamondFace([cardInput[1]])
if cardInput[0] == 'h':
generateHeartFace([cardInput[1]])
if cardInput[0] == 's':
generateSpadeFace([cardInput[1]])
if cardInput[0] == 'c':
generateClubFace([cardInput[1]])
#actually prints the code cards, this is where I need help (line 61)
numberGenerator()
for x in range(len(hand)):
cardArt(hand[x-1])
print(hand)
Solution 1:[1]
The answer by @eric-lozano is a great one (I upvoted it :-).
I would tackle the problem in a slightly different manner though. I would definitely move the print() stuff out of method calls if I could.
This strategy is based on reshaping a your list of cards with face rows into a list of face rows with cards.
import random
## ---------------------------
## Generate the display for a single card.
## You have this now but in multiple methods
## Note that we will only print() from our main
## ---------------------------
def get_card_face(card_key):
card_suit, card_value = list(card_key)
suit_display = {"h": "?", "c": "?", "d": "?", "s": "?"}.get(card_suit, "*")
return [
f"?????",
f"| {card_value} |",
f"| {suit_display} |",
f"?????"
]
## ---------------------------
## ---------------------------
## Generatet the display for a hand of cards.
## Note that we will only print() from our main.
## ---------------------------
def get_hand_faces(hand):
## ---------------
## Get a list of the displays of each card.
## This will be a list of lists as each card display is a list of display rows
## ---------------
hand_faces = [get_card_face(c) for c in hand]
## ---------------
## ---------------
## Reshape hand faces so that our list of lists changes from a list of
## cards into a list of rows. Note hand_faces now an enumerator.
## ---------------
hand_faces = zip(*hand_faces)
## ---------------
## ---------------
## return a string with proper spacing and line breaks
## ---------------
return "\n".join(" ".join(row) for row in hand_faces)
## ---------------
## ---------------------------
deck = [
f"{s}{v}"
for s in list("hdcs")
for v in list("A23456789TJQK")
]
random.shuffle(deck)
user_hand = []
computer_hand = []
for _ in range(6):
user_hand.append(deck.pop())
computer_hand.append(deck.pop())
print("User Hand:")
print(get_hand_faces(user_hand))
print("Computer Hand:")
print(get_hand_faces(computer_hand))
This should give you something like:
User Hand:
????? ????? ????? ????? ????? ?????
| K | | 9 | | 4 | | 6 | | A | | 8 |
| ? | | ? | | ? | | ? | | ? | | ? |
????? ????? ????? ????? ????? ?????
Computer Hand:
????? ????? ????? ????? ????? ?????
| K | | 4 | | 9 | | 3 | | 4 | | A |
| ? | | ? | | ? | | ? | | ? | | ? |
????? ????? ????? ????? ????? ?????
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JonSG |
