3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シンプルなトランプ用Class(Python)

Last updated at Posted at 2019-03-01

プログラミング入門者からの卒業試験は『ブラックジャック』を開発すべし

の記事を見かけ、自分も入門者から卒業目指してチャレンジしてみることに。
第一歩として、シンプルなトランプ用クラス作成。
(Deck=シャッフルされたトランプの山)

内部のデータは扱いやすいように整数、
表示は絵文字を使った形式に。

ソースコード: gist


import random


class Card:
	SUITS = '♤♡♢♧'
	RANKS = '0 A 2 3 4 5 6 7 8 9 10 J Q K'.split()

	def __init__(self, suit, rank): self.suit, self.rank = suit, rank

	def __repr__(self): return f'{Card.SUITS[self.suit]}{Card.RANKS[self.rank]}'


class Deck:

	def __init__(self):
		self.cards = [Card(suit, rank) for suit in range(4) for rank in range(1, 14)]
		random.shuffle(self.cards)

	@property
	def next_card(self): return self.cards.pop()

表示のテスト:

d=Deck()
print(d.next_card)
print(d.next_card)
print(d.next_card)
実行結果
♡Q
♢6
♢7
3
3
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?