尝试在Python中实现卡片组排序算法

时间:2015-11-30 19:32:29

标签: python sorting

所以我正在尝试用Python创建一个卡片组。我正在尝试创建一种方法来将卡片组重新排序。

2个俱乐部,3个俱乐部,......俱乐部的王牌

2颗钻石,3颗钻石,......钻石之王

心中有2个,心中有3个,......心中的王牌

2个黑桃,3个黑桃,......黑桃王牌

我已经实现了部分内容,但排序部分只是工作方式。它成功地在诉讼中对它们进行了排序,但排名部分有点搞砸了。 10出现在2之前且图片卡不正确。我的猜测是我的 __ lt __ __ eq __ 功能没有正确处理排名(尤其是图片卡)

CODE:

import random
# deck = ["2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D",
#        "7D", "8D", "9D", "10D", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH",
#        "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", "AS"]
from functools import total_ordering

graveyard = []


@total_ordering
class Card(object):
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return '%s of %s' % (self.rank,
                             self.suit)

    def __repr__(self): return str(self)

    def __lt__(self, other):
        t1 = self.suit, self.rank
        t2 = other.suit, other.rank
        return t1 < t2

    def __gt__(self, other):
        t1 = self.suit, self.rank
        t2 = other.suit, other.rank
        return t1 > t2

    def __eq__(self, other):
        t1 = self.suit, self.rank
        t2 = other.suit, other.rank
        return t1 == t2




class Deck(object):
    def __init__(self):
        """
        Idea for this found here.
        https://stackoverflow.com/questions/8511745/sorting-a-hand-of-cards-accoring-to-rank-and-suit-in-python

        I could use this to ALWAYS have a shuffled deck at the beginning or to just start with a 'clean' deck as above...
        :return:
        """
        self.rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
        self.suit = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
        self.deck = [Card(r, s) for r in self.rank for s in self.suit]
        random.shuffle(self.deck)

    def __getitem__(self, item):
        return self.deck[item]

    def deal(self):
        """
        Return a card from the deck.
        :return:
        """
        topCard = self.deck.pop(0)
        graveyard.append(topCard)
        print(topCard)

    def shuffle(self):
        """
        Shuffle the deck
        :return:
        """
        self.deck.extend(graveyard)
        random.shuffle(self.deck)
        self.fan()

    def fan(self):
        """
        Print out the deck
        :return:
        """
        for card in self.deck:
            print(card)

    def order(self):
        return self.deck.sort()

    def printGraveyard(self):
        for dead in graveyard:
            print(dead)


d = Deck()

d.order()

d.fan()

输出:

10 of Clubs
2 of Clubs
3 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
Ace of Clubs
Jack of Clubs
King of Clubs
Queen of Clubs
10 of Diamonds
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
Ace of Diamonds
Jack of Diamonds
King of Diamonds
Queen of Diamonds
10 of Hearts
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
Ace of Hearts
Jack of Hearts
King of Hearts
Queen of Hearts
10 of Spades
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
Ace of Spades
Jack of Spades
King of Spades
Queen of Spades

2 个答案:

答案 0 :(得分:2)

你是对的,它是比较字符串的等级的比较,即"10"<"2""Ace"<"Queen"等。

您可以做的是例如将合适的字符串写入数字排名函数。

def num_rank(rank):
    if rank[0] == "A":
         return 14
    if rank[0] == "J":
         return 11
    if rank[0] == "Q":
         return 12
    if rank[0] == "K":
         return 13
    return int(rank)

然后你用它来重写比较:

def __lt__(self, other):
    t1 = self.suit, num_rank(self.rank)
    t2 = other.suit, num_rank(other.rank)
    return t1 < t2

答案 1 :(得分:0)

我没有测试它,但我认为你可以创建一个等级的类,如:

class CardRank():
    def __init__(self, name, value):
        self.name = name # e.g. Ace
        self.value = value # e.g. 12
在甲板上

def __init__(self):
    self.rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
    self.suit = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    self.deck = [Card(CardRank(r, self.rank.index(r)), s) for r in self.rank for s in self.suit]

所以在class Card你可以做类似的事情:

 def __gt__(self, other):
     return self.rank.value > other.rank.value