尝试在Python中实现卡片组排序算法 [英] Trying to implement a card deck sorting algorithm in Python

查看:185
本文介绍了尝试在Python中实现卡片组排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试在Python中创建一副纸牌.我正在尝试创建一种方法来对卡片组重新排序.

So I'm trying to create a Deck of cards in Python. I am at the point where I'm trying to create the method to sort the deck of cards back into order.

2个俱乐部,3个俱乐部,... A级俱乐部

2 of clubs, 3 of clubs, ...Ace of clubs

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

2 of Diamonds, 3 of Diamonds, ...Ace of Diamonds

2个心,3个心,...心之王牌

2 of Hearts, 3 of Hearts, ...Ace of Hearts

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

2 of Spades, 3 of Spades, ...Ace of Spades

我已经实现了其中的一部分,但排序部分仅能正常工作.它成功地将它们分类到西装中,但是等级部分有点混乱. 10在2之前,图片卡不正确.我的猜测是我的 __ lt __ __ eq __ 函数无法正确处理等级(特别是图片卡)

I have part of it already implemented but the sorting part is only KIND OF working. It successfully sorts them in the suit, but the rank part is kind of messed up. 10 comes before 2 and the Picture Cards are incorrect. My guess is that my __lt__ and __eq__ functions aren't processing the ranks correctly (esp the Picture Cards)

代码:

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

推荐答案

您是正确的,是比较字符串的排名比较,即"10"<"2""Ace"<"Queen"等.

You're correct, it's the comparision of the rank that compares the string, that is "10"<"2" and "Ace"<"Queen" etc.

例如,您可以做一个适当的字符串写入数字排名函数.

What you could do is to for example write a suitable string to numeric rank function.

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

这篇关于尝试在Python中实现卡片组排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆