gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:10 26 4
gpt4 key购买 nike

所以我正在尝试用 Python 创建一副纸牌。我正在尝试创建方法来将一副纸牌重新排序。

梅花2,梅花3,...梅花A

方 block 2,方 block 3,...方 block A

红心 2,红心 3,...红心 A

黑桃 2,黑桃 3,...黑桃 A

我已经实现了其中的一部分,但排序部分只能正常工作。它成功地将它们按花色排序,但等级部分有点乱。 10 在 2 之前,图片卡不正确。我的猜测是我的 __lt____eq__ 函数没有正确处理排名(尤其是图片卡片)

代码:

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

最佳答案

你没看错,就是比较字符串的rank,也就是"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

关于python - 尝试在 Python 中实现卡片组排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com