设计一套扑克牌

约束和假设

  • 这是一幅用于类似扑克和二十一点等游戏的通用扑克牌吗?
  • 我们可以假设这副牌有52张(2-10,杰克,女王,国王,埃斯)和4种花色吗?
  • 我们可以假设输入是有效的,还是需要对其进行验证?

编码实现

python 复制代码
from abc import ABCMeta, abstractmethod
from enum import Enum
import sys

class Suit(Enum):
	HEART = 0
	DIAMOND = 1
	CLUBS = 2
	SPADE = 3

class Card(metaclass=ABCMeta):
	
	def __init__(self, value, suit):
		self.value = value
		self.suit = suit
		self.is_available = true

	@property
	@abstractmethod
	def value(self):
		pass

	@value.setter
	@abstractmethod
	def value(self, other):
		pass

class BlackJackCard(Card):
	
	def __init__(self, value, suit):
		super(BlackJackCard, self).__init__(value, suit)

	def is_ace(self):
		'''Jack =11, Queen = 12, King = 13'''
		return 10 < self._value <= 13

	@preoperty
	def value(self):
		if self.is_ace() == 1:
			return 1
		elif self.is_face_card():
			return 10
		else:
			return self._value

	@value.setter
	def value(self, new_value):
		if 1 <= new_value <= 13:
			self._value = new_value
		else:
			raise ValueError('Invalid card value: {}'.format(new_value))

class Hand(object):
	def __init__(self, cards):
		self.cards = cards
	
	def add_card(self, card):
		self.cards.append(card)

	def score(self):
		total_value = 0
		for card in self.cards:
			total_value += card.value
		return total_value

class BlackJackHand(Hand):
	BLACKJACK = 21
	
	def __init__(self, cards):
		min_over = sys.MAXSIZE
		max_under = -sys.MAXSIZE
		for score in self.possible_scores():
			if self.BLACKJACK < score < min_over:
				min_over = score
			elif max_under < score <= self.BLACKJACK:
				max_under = score
		return max_under if max_under != -sys.MAXSIZE else min_over

	def possible_scores(self):
		'''Return a list of possible scores, taking aces into account'''

class Deck(oject):
	
	def __init__(self,card):
		self.cards = cards
		self.deal_index = 0

	def remaining_cards(self):
		return len(self.cards) - deal_index

	def deal_card():
		try:
			card = self.card[self.deal_index]
			card.is_available = False
			self.deal_index += 1
		except IndexError:
			return None
		return card

	def shuffle(self): #...
相关推荐
莹莹学编程—成长记41 分钟前
string的模拟实现
服务器·c++·算法
海天一色y1 小时前
Pycharm(十六)面向对象进阶
ide·python·pycharm
??? Meggie1 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
XIE3922 小时前
Browser-use使用教程
python
酷爱码3 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
蹦蹦跳跳真可爱5893 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
ShiinaMashirol5 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
MinggeQingchun6 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
Python自动化办公社区7 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
wuqingshun3141597 小时前
蓝桥杯 5. 交换瓶子
数据结构·c++·算法·职场和发展·蓝桥杯