def __init__(self): self._cards = [ Card(rank, suit) for suit in self.suits for rank in self.ranks ]
def __init__(self): self._cards = [ Card(rank, suit) for suit in self.suits for rank in self.ranks ] def __len__(self): return len(self._cards) def __getitem__(self, position): return self._cards[position] if __name__ == '__main__': from frenchdeck import FrenchDeck, Card beer_card = Card('7', 'diamonds') print(beer_card) deck = FrenchDeck() print(len(deck)) print("この機能は__getitem__が提供している。") print(deck[0]) print("スライス指定もできる") print(deck[:3]) print(deck[12::13]) print("インテレーションは暗黙的。コレクションに__contains__がなければin演算子が順番にスキャンしていく。") print(Card('Q', 'hearts') in deck)
from frenchdeck import FrenchDeck, Card, card_02 beer_card = Card('7', 'diamonds') print(beer_card) print(len(beer_card)) card_two = card_02(rank=8, suit='clubs') print(card_two) card_two = card_02(10, 'spades') print(card_two) deck = FrenchDeck() print(len(deck)) print(deck._cards) print(len(deck._cards)) print(deck[0]) print(deck[-1]) print(deck[12]) print(deck[:3]) print(deck[12::13]) from random import choice print(choice(deck)) for card in deck: print(card) print(Card('K', 'hearts') in deck) print(Card('K', 'tearts') in deck)
from frenchdeck import FrenchDeck, Card beerCard = Card('7', 'diamonds') deck = FrenchDeck() print(len(deck)) for card in reversed(deck): print(card) # from testing import poker # y = poker() # for item in y: # print(item)
from frenchdeck import FrenchDeck, Card from vector import Vector from random import shuffle beer_card = Card('7', 'diamond') print(type(beer_card)) deck = FrenchDeck() print(len(deck)) print(deck[1]) print(deck[:5]) suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0) def spades_high(card): rank_value = FrenchDeck.ranks.index(card.rank) return rank_value * len(suit_values) + suit_values[card.suit] for card in sorted(deck, key=spades_high): print(card) Vector(2, 4) shuffle(deck) print(deck._cards) print(deck[12::13])