Help Printing Emojis

Question:
I am making a CardGame hub where you can play all kinds of different card games, and I am starting with making War, and I want it to print some card emojis, but I canโ€™t seem to get it to work.

Repl link:
https://replit.com/@SalladShooter/CardGames

war.py

import json
import random

class War:
  def __init__(self, players):
    self.players = players
    self.hands = {}
    self.hand = []
    self.cards = {
      2: "2", 3: "3", 4: "4", 5: "5",
      6: "6", 7: "7", 8: "8", 9: "9",
      10: "10", 11: "J", 12: "Q", 13: "K", 14: "A",
      "๐Ÿƒ‚": 2, "๐Ÿƒƒ": 3, "๐Ÿƒ„": 4, "๐Ÿƒ…": 5,
      "๐Ÿƒ†": 6, "๐Ÿƒ‡": 7, "๐Ÿƒˆ": 8, "๐Ÿƒ‰": 9,
      "๐Ÿƒ": 10, "๐Ÿƒ‹": "J", "๐Ÿƒ": "Q", "๐ŸƒŽ": "K", "๐Ÿƒ‘": "A"
    }
    self.face_cards = {"J": 11, "Q": 12, "K": 13, "A": 14}
    self.cards_per_player = int(len(self.cards) / self.players - len(self.hand))

  def setup_game(self):
    self.hands = {}
    for player in range(1, self.players + 1):
      self.hand = []
      for i in range(self.cards_per_player):
        card_idx = random.randint(2, 14)
        card = self.cards[card_idx]
        self.hand.append(card)
      self.hands[f"Player{player}"] = self.hand

    with open("hands.json", "w") as outfile:
      json.dump(self.hands, outfile)

  def start_game(self):
    with open("hands.json") as json_file:
        data = json.load(json_file)
        winners = []
        max_card = 0

        for player in data:
            cards = data[player]
            if cards[0] in self.face_cards:
                drawn_card = self.face_cards[cards[0]]
            elif cards[0] in self.cards:
                drawn_card = self.cards[cards[0]]
            else:
                drawn_card = int(cards[0])

            print(f"{player} drew: {self.cards[drawn_card]}")
            if drawn_card > max_card:
                max_card = drawn_card
                winners = [player]
            elif drawn_card == max_card:
                winners.append(player)

        if len(winners) > 1:
            print(f"War initiated between players: {', '.join(winners)}")
            while len(set(data[player][0] for player in winners)) == 1:
                for player in winners:
                    drawn_card = data[player].pop(0)
                    if data[player]:
                        if drawn_card in self.face_cards:
                            new_card = self.face_cards[drawn_card]
                        elif drawn_card in self.cards:
                            new_card = self.cards[drawn_card]
                        else:
                            new_card = int(drawn_card)
                        print(f"{player} drew: {self.cards[new_card]}")
                        if new_card > max_card:
                            winners = [player]
                            max_card = new_card
                        elif new_card == max_card:
                            winners.append(player)
                        else:
                            break

        for player in winners:
            self.hands[player].extend(data[player])

        print(f"Winner: {', '.join(winners)}") 

main.py

import war

war = war.War(2)
war.setup_game()
war.start_game() 

Example hands.json

{"Player1": ["J", "7", "6", "10", "5", "2", "7", "J", "6", "K", "Q", "A", "2"], "Player2": ["3", "9", "J", "9", "K", "J", "7", "2", "Q", "Q", "9", "10", "K"]} 

On windows, you can press Windows Key + . to bring up emojis. You can then select them and insert them directly into the code.

Getting the emojis isnโ€™t my problem, I have some in the code already (Iโ€™m also not on Windows, but on iPad). I got the emojis from here โ†’ playing card decks โ™ฃ๏ธ.

What happens? What goes wrong?

@MilesWK it just doesnโ€™t print out the emojis like it should, example โ†’

Player1 drew: A 
Player2 drew: 7
Winner: Player1 

try using unicode and the chr() and ord() functions. Search them up on google to learn more

@AndrewDeng3 I looked up some Unicode things for the emojis, and they donโ€™t exist for the cards. The emojiโ€™s arenโ€™t the problem, there isnโ€™t any error. Just printing out the emojis is the problem, I think it has to do with the self.cards dictionary.

try using escape sequences:

# e.g. smiley
print("\U0001f600")

They work for me, check out my newest testing repl for a PoC

2 Likes

@python660 they still didnโ€™t print out, maybe I setup the dictionary wrong โ†’

import json
import random

class War:
  def __init__(self, players):
    self.players = players
    self.hands = {}
    self.hand = []
    self.cards = {
      2: "2", 3: "3", 4: "4", 5: "5",
      6: "6", 7: "7", 8: "8", 9: "9",
      10: "10", 11: "J", 12: "Q", 13: "K", 14: "A",
      "🂢": 2, "🂣": 3, "🂤": 4, "🂥": 5,
      "🂦": 6, "🂧": 7, "🂨": 8, "🂩": 9,
      "🂪": 10, "🂫": "J", "🂭": "Q", "🂮": "K", "🂡": "A"
    }
    self.face_cards = {"J": 11, "Q": 12, "K": 13, "A": 14}
    self.cards_per_player = int(len(self.cards) / self.players - len(self.hand))

  def setup_game(self):
    self.hands = {}
    for player in range(1, self.players + 1):
      self.hand = []
      for i in range(self.cards_per_player):
        card_idx = random.randint(2, 14)
        card = self.cards[card_idx]
        self.hand.append(card)
      self.hands[f"Player{player}"] = self.hand

    with open("hands.json", "w") as outfile:
      json.dump(self.hands, outfile)

  def start_game(self):
    with open("hands.json") as json_file:
        data = json.load(json_file)
        winners = []
        max_card = 0

        for player in data:
            cards = data[player]
            if cards[0] in self.face_cards:
                drawn_card = self.face_cards[cards[0]]
            elif cards[0] in self.cards:
                drawn_card = self.cards[cards[0]]
            else:
                drawn_card = int(cards[0])

            print(f"{player} drew: {self.cards[drawn_card]}")
            if drawn_card > max_card:
                max_card = drawn_card
                winners = [player]
            elif drawn_card == max_card:
                winners.append(player)

        if len(winners) > 1:
            print(f"War initiated between players: {', '.join(winners)}")
            while len(set(data[player][0] for player in winners)) == 1:
                for player in winners:
                    drawn_card = data[player].pop(0)
                    if data[player]:
                        if drawn_card in self.face_cards:
                            new_card = self.face_cards[drawn_card]
                        elif drawn_card in self.cards:
                            new_card = self.cards[drawn_card]
                        else:
                            new_card = int(drawn_card)
                        print(f"{player} drew: {self.cards[new_card]}")
                        if new_card > max_card:
                            winners = [player]
                            max_card = new_card
                        elif new_card == max_card:
                            winners.append(player)
                        else:
                            break

        for player in winners:
            self.hands[player].extend(data[player])

        print(f"Winner: {', '.join(winners)}") 
1 Like

Just a note, rather than using the @ symbol to mention people you can always reply using the reply buttonโ€ฆ but @ works too

1 Like

I know, itโ€™s just a habit though, because sometimes the Reply button doesnโ€™t work. But the @ mention, in my experience always worked.

1 Like

Ahh in that case keep doing it!

1 Like

I believe that this isnโ€™t an emoji printing problem, but a code problem. My head hurts too much from thinking, so I canโ€™t really debug right now, but one comment I would like to make is that since you define the emojis as keys, they wonโ€™t be printed.

1 Like

I fixed it, I did some messing around with setting amounts, and fixed some start_game() stuff as well โ†’

import json
import random

class War:
  def __init__(self, players):
    self.players = players
    self.hands = {}
    self.hand = []
    self.cards = {
      2: "๐Ÿ‚ข", 3: "๐Ÿ‚ฃ", 4: "๐Ÿ‚ค", 5: "๐Ÿ‚ฅ",
      6: "๐Ÿ‚ฆ", 7: "๐Ÿ‚ง", 8: "๐Ÿ‚จ", 9: "๐Ÿ‚ฉ",
      10: "๐Ÿ‚ช", 11: "๐Ÿ‚ซ", 12: "๐Ÿ‚ญ", 13: "๐Ÿ‚ฎ", 14: "๐Ÿ‚ฑ",
      "๐Ÿ‚ข": 2, "๐Ÿ‚ฃ": 3, "๐Ÿ‚ค": 4, "๐Ÿ‚ฅ": 5,
      "๐Ÿ‚ฆ": 6, "๐Ÿ‚ง": 7, "๐Ÿ‚จ": 8, "๐Ÿ‚ฉ": 9,
      "๐Ÿ‚ช": 10, "๐Ÿ‚ซ": 11, "๐Ÿ‚ญ": 12, "๐Ÿ‚ฎ": 13, "๐Ÿ‚ก": 14
    }
    self.face_cards = {"J": 11, "Q": 12, "K": 13, "A": 14}
    self.cards_per_player = int(len(self.cards) / self.players - len(self.hand))

  def setup_game(self):
    self.hands = {}
    for player in range(1, self.players + 1):
      self.hand = []
      for i in range(self.cards_per_player):
        card_idx = random.randint(2, 14)
        card = self.cards[card_idx]
        self.hand.append(card)
      self.hands[f"Player{player}"] = self.hand

    with open("hands.json", "w") as outfile:
      json.dump(self.hands, outfile)

  def start_game(self):
    with open("hands.json") as json_file:
        data = json.load(json_file)
        winners = []
        max_card = 0

        for player in data:
            cards = data[player]
            if cards[0] in self.face_cards:
                drawn_card = self.face_cards[cards[0]]
            elif cards[0] in self.cards:
                drawn_card = self.cards[cards[0]]
            else:
                drawn_card = int(cards[0])

            print(f"{player} drew: {self.cards[drawn_card]}")

            if drawn_card > max_card:
                max_card = drawn_card
                winners = [player]
            elif drawn_card == max_card:
                winners.append(player)

        if len(winners) > 1:
          print(f"War initiated between players: {', '.join(winners)}")
          winning_cards = [data[player][0] for player in winners]
          max_winning_card = max(winning_cards)
          winning_players = [player for player in winners if data[player][0] == max_winning_card]
          if len(winning_players) == 1:
              winners = winning_players
          else:
              while len(set(data[player][0] for player in winning_players)) == 1:
                  for player in winning_players:
                      drawn_card = int(data[player].pop(0))
                      new_card = self.face_cards.get(drawn_card) or self.cards.get(drawn_card, drawn_card)
                      print(f"{player} drew: {self.cards[new_card]}")
  
                      if new_card > max_winning_card:
                          winners = [player]
                          max_winning_card = new_card
                      elif new_card == max_winning_card:
                          winners.append(player)
                      else:
                          break

        for player in winners:
            self.hands[player].extend(data[player])

        print(f"Winner: {', '.join(winners)}") 
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.