I am making a card game in python, consisting of 3 bots and a player. The deck of 52 cards is randomly distributed and stored in 4 separate lists of 13 cards each.
bot1 = ['H5', 'CJ', 'C4', 'D7', 'H10', 'S4', 'HQ', 'C9', 'DA', 'S8', 'CQ', 'CA', 'CK'] bot2 = ['H4', 'S5', 'C6', 'H8', 'S3', 'D5', 'H2', 'S10', 'SJ', 'HA', 'H7', 'SA', 'HK'] bot3 = ['D8', 'SQ', 'D4', 'D2', 'DJ', 'DK', 'D9', 'DQ', 'HJ', 'SK', 'S2', 'S7', 'C3'] player = ['C5', 'H3', 'D10', 'C10', 'S6', 'D3', 'C8', 'H9', 'C7', 'C2', 'S9', 'H6', 'D6']
Here, H5 stands for 5 of hearts, DJ as Joker of Diamonds, SA as Ace of Spades and so on. But, I want to sort these lists of strings in order of the value of cards i.e. A > K > Q >J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2 For eg., after sorting the bot1 list will look like
bot1 = ['DA', 'CA', 'CK', 'CQ', 'HQ', 'CJ', 'H10', 'C9', 'S8', 'D7', 'H5', 'C4', 'S4']
Is is possible to sort in this manner? If yes, then how?
Advertisement
Answer
You may get the values ordered in a string for example, then use the index in that list to order the card set
bot1 = ['H5', 'CJ', 'C4', 'D7', 'H10', 'S4', 'HQ', 'C9', 'DA', 'S8', 'CQ', 'CA', 'CK'] order = 'AKQJ1098765432' bot1 = sorted(bot1, key=lambda x: order.index(x[1:]))