suits = ['H', 'S', 'C', 'D'] card_val = (list(range(1, 11)) + [10] * 3) * 4 base_names = ['A'] + list(range(2, 11)) + ['J', 'K', 'Q'] cards = [] for suit in ['H', 'S', 'C', 'D']: cards.extend(str(num) + suit for num in base_names) deck = pd.Series(card_val, index=cards) deck def draw(deck, n=5): return deck.sample(n) draw(deck)
时间: 2023-08-12 15:03:24 浏览: 980
这段代码是用 Python 编写的一个简单的扑克牌游戏,其中变量 suits 表示扑克牌的花色,变量 card_val 表示扑克牌牌面对应的点数,变量 base_names 表示扑克牌的牌面名称,变量 cards 表示一副扑克牌中所有的牌,变量 deck 则用 Pandas 库中的 Series 类型表示了一副扑克牌,其中每张牌都有对应的点数。
函数 draw 用于从一副扑克牌中随机抽取 n 张牌,并返回这些牌的点数。在这段代码中,函数 draw 抽取了 5 张牌。
相关问题
import random random.seed(10) class Deck: def init(self): ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] suits = ['C', 'D', 'H', 'S'] self.deck = [s + r for s in suits for r in ranks] random.shuffle(self.deck) def hand(self, n=1): #拿一手牌,n张 hand = [self.deck[i] for i in range(n)] del self.deck[:n] return hand def deal(self, cards_per_hand, no_of_players): #发牌,cards_per_hand:每个人发牌数, no_of_players:人数 return [self.hand(cards_per_hand) for i in range(no_of_players)] def str(self): return str(self.deck) def same_rank(hand, n_of_a_kind): #分析牌有对或者3-4个 #hand:一手牌,n_of_a_kind:2~4 寻找相同牌面有这么多的牌 ranks = [card[1:] for card in hand] counter = 0 already_counted = [] for rank in ranks: if rank not in already_counted and \ ranks.count(rank) == n_of_a_kind: counter += 1 already_counted.append(rank) return counter def same_suit(hand): #统计同花牌数量 suits = [card[0] for card in hand] counter = {} # counter[suit]存放suit花色的数量 for suit in suits: count = suits.count(suit) if count > 1: #只记录花色数大于1的 counter[suit] = count return counter N=100000 #实验次数 #计算五张牌中正好两对的概率 def prob_two_pairs(): #----------begin----------- #----------end----------- #五张牌中有四张或五张同一花色的牌的概率 def prob_same_suit(): #----------begin----------- #----------end----------- #计算五张牌中有四张牌面数字相同的牌(四条)的概率 def prob_fourofakind(): #----------begin----------- #----------end----------- print('五张牌中正好两对的概率:%.5f' %prob_two_pairs()) print('五张牌中有四张或五张同一花色的牌的概率:%.5f' %prob_same_suit()) print('五张牌中有四张牌面数字相同的牌(四条)的概率:%.5f' %prob_fourofakind())请补全代码
import random
random.seed(10)
class Deck:
def __init__(self):
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
suits = ['C', 'D', 'H', 'S']
self.deck = [s + r for s in suits for r in ranks]
random.shuffle(self.deck)
def hand(self, n=1): #拿一手牌,n张
hand = [self.deck[i] for i in range(n)]
del self.deck[:n]
return hand
def deal(self, cards_per_hand, no_of_players): #发牌,cards_per_hand:每个人发牌数, no_of_players:人数
return [self.hand(cards_per_hand) for i in range(no_of_players)]
def __str__(self):
return str(self.deck)
def same_rank(hand, n_of_a_kind): #分析牌有对或者3-4个 #hand:一手牌,n_of_a_kind:2~4 寻找相同牌面有这么多的牌
ranks = [card[1:] for card in hand]
counter = 0
already_counted = []
for rank in ranks:
if rank not in already_counted and \
ranks.count(rank) == n_of_a_kind:
counter += 1
already_counted.append(rank)
return counter
def same_suit(hand): #统计同花牌数量
suits = [card[0] for card in hand]
counter = {} # counter[suit]存放suit花色的数量
for suit in suits:
count = suits.count(suit)
if count > 1: #只记录花色数大于1的
counter[suit] = count
return counter
N=100000 #实验次数
#计算五张牌中正好两对的概率
def prob_two_pairs():
count = 0
for i in range(N):
d = Deck()
hand = d.hand(5)
if same_rank(hand, 2) == 2:
count += 1
return count / N
#五张牌中有四张或五张同一花色的牌的概率
def prob_same_suit():
count = 0
for i in range(N):
d = Deck()
hand = d.hand(5)
suits = same_suit(hand)
if 4 in suits.values() or 5 in suits.values():
count += 1
return count / N
#计算五张牌中有四张牌面数字相同的牌(四条)的概率
def prob_fourofakind():
count = 0
for i in range(N):
d = Deck()
hand = d.hand(5)
if same_rank(hand, 4) == 1:
count += 1
return count / N
print('五张牌中正好两对的概率:%.5f' %prob_two_pairs())
print('五张牌中有四张或五张同一花色的牌的概率:%.5f' %prob_same_suit())
print('五张牌中有四张牌面数字相同的牌(四条)的概率:%.5f' %prob_fourofakind())
编写程序,生成一副扑克牌,有4个花色,每个花色有13张牌 【参考代码】: ranks=["2","3","4","5","6","7","8","9","10","J","Q","K","A"] suits=["黑桃","红桃","方块","梅花",] ____________: ____________: print(suit+rank,end=" ") print()
下面是生成一副扑克牌的Python代码:
```python
ranks = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
suits = ["黑桃","红桃","方块","梅花"]
for suit in suits:
for rank in ranks:
print(suit+rank,end=" ")
print()
```
输出结果如下:
```
黑桃2 黑桃3 黑桃4 黑桃5 黑桃6 黑桃7 黑桃8 黑桃9 黑桃10 黑桃J 黑桃Q 黑桃K 黑桃A
红桃2 红桃3 红桃4 红桃5 红桃6 红桃7 红桃8 红桃9 红桃10 红桃J 红桃Q 红桃K 红桃A
方块2 方块3 方块4 方块5 方块6 方块7 方块8 方块9 方块10 方块J 方块Q 方块K 方块A
梅花2 梅花3 梅花4 梅花5 梅花6 梅花7 梅花8 梅花9 梅花10 梅花J 梅花Q 梅花K 梅花A
```
阅读全文