设计一套扑克牌

约束和假设

  • 这是一幅用于类似扑克和二十一点等游戏的通用扑克牌吗?
  • 我们可以假设这副牌有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): #...
相关推荐
boonya13 小时前
Java垃圾回收机制理论算法及使用
jvm·算法·gc·垃圾收集器·理论
愚润求学13 小时前
【贪心算法】day9
c++·算法·leetcode·贪心算法
Python图像识别13 小时前
63_基于深度学习的草莓病害检测识别系统(yolo11、yolov8、yolov5+UI界面+Python项目源码+模型+标注好的数据集)
python·深度学习·yolo
lingran__14 小时前
速通ACM省铜第二天 赋源码(Adjacent XOR和Arboris Contractio)
c++·算法
十六点五14 小时前
Java NIO的底层原理
java·开发语言·python
跟橙姐学代码14 小时前
不要再用 print() 了!Python logging 库才是调试的终极武器
前端·python
半桔15 小时前
【STL源码剖析】二叉世界的平衡:从BST 到 AVL-tree 和 RB-tree 的插入逻辑
java·数据结构·c++·算法·set·map
小叶lr15 小时前
python 从pycharm部署到新环境
开发语言·python·pycharm
2301_7634710315 小时前
Python单元测试(unittest)实战指南
python
xiaoxiongip66615 小时前
动态ip适合挂什么项目
网络·爬虫·python·网络协议·tcp/ip·ip