So I am creating a card game in python. For the part of the game I an currently working on, I need to check an array of playing cards to see if there are any pairs. A pair is considered two cards with the same value and color. For example, king of hearts and king of diamonds are a pair, but king of hearts and king of clubs aren't.
I need to return a new list with all of the pairs removed.
Suppose we have
list = ['9♠', '5♠', 'K♢', 'A♣', 'K♣', 'K♡', '2♠', 'Q♠', 'K♠', 'Q♢', 'J♠', 'A♡', '4♣', '5♣', '7♡', 'A♠', '10♣', 'Q♡', '8♡', '9♢', '10♢', 'J♡', '10♡', 'J♣', '3♡']
The result should be:
list without pairs = ['10♣', '2♠', '3♡', '4♣', '7♡', '8♡', '9♠', '9♢', 'A♣', 'A♡', 'A♠', 'J♠', 'J♡', 'J♣', 'K♢', 'K♣', 'K♡', 'K♠', 'Q♠']
I currently have this code:
import random
result=[]
for i in range(len(l)):
if '♣' in l[i]:
pass
elif '♠' in l[i]:
pass
elif '♡' in l[i]:
pass
elif '♢' in l[i]:
pass
random.shuffle(result)
return result
CodePudding user response:
You can use below code. It is based on sorting by numbers and then compare the colors of successive cards. Comparing color is based on the ordinal logic:
def get_card(card):
return (card[:-1], card[-1]) # save number and color
if __name__ == "__main__":
mylist = ['9♠', '5♠', 'K♢', 'A♣', 'K♣', 'K♡', '2♠', 'Q♠', 'K♠', 'Q♢', 'J♠', 'A♡', '4♣', '5♣', '7♡', 'A♠', '10♣', 'Q♡',
'8♡', '9♢', '10♢', 'J♡', '10♡', 'J♣', '3♡']
# Sort the cards based on numbers
mylist.sort(key=lambda x: x[0])
print(mylist)
output = []
index = 0
while index < len(mylist)-1:
# Compare successive two cards
first_card = get_card(mylist[index])
second_card = get_card(mylist[index 1])
print(f"Comparing {first_card} and {second_card} -- {ord(first_card[1])} {ord(second_card[1])} = {ord(first_card[1]) ord(second_card[1])}")
# check the number
if (first_card[0] == second_card[0]):
# check the color
ord_val = ord(first_card[1]) ord(second_card[1])
if (first_card[1] == second_card [1] or ord_val % 10 == 1 ):
# same card, skip this card
index = 1
else:
# add card to output, dont skip next card
output.append(mylist[index])
print("\tAdding card")
else:
output.append(mylist[index])
print("\tAdding card")
index = 1
print(output)
Logic:
Use ordinal values to compare colors. Sum of ordinals of same colors(according to color symbol in your problem statement) are equal and last digit is 1. Ord values:
"""
♠: 9824 - black - spade
♣: 9827 - black - club
♡: 9825 - red - heart
♢: 9826 - red - diamond
"""
Spade club = 9824 9827 = 19651
heart diamond = 9825 9826 = 19651
Output:
['10♣', '2♠', '3♡', '4♣', '7♡', '8♡', '9♠', '9♢', 'A♣', 'A♡', 'A♠', 'J♠', 'J♡', 'J♣', 'K♢', 'K♣', 'K♡', 'K♠', 'Q♠']
CodePudding user response:
cards_list = [
"9♠",
"5♠",
"K♢",
"A♣",
"K♣",
"K♡",
"2♠",
"Q♠",
"K♠",
"Q♢",
"J♠",
"A♡",
"4♣",
"5♣",
"7♡",
"A♠",
"10♣",
"Q♡",
"8♡",
"9♢",
"10♢",
"J♡",
"10♡",
"J♣",
"3♡",
]
SAME_COLORS = {"♢": "♡", "♡": "♢", "♠": "♣", "♣": "♠"}
list_without_pairs = []
for card in cards_list:
if card[:-1] SAME_COLORS[card[-1]] in cards_list:
continue
else:
list_without_pairs.append(card)
print(list_without_pairs)
Output:
['9♠', '2♠', 'Q♠', 'A♡', '4♣', '7♡', '10♣', '8♡', '9♢', 'J♡', '3♡']
This work exactly the same but could be a little bit more confuse:
list_without_pairs = [card for card in cards_list if card[:-1] SAME_COLORS[card[-1]] not in cards_list]