使用4*13的双下标数组deck来表示一副扑克牌,数组的一行对应一种花色,0行代表红桃(Heart),1行代表方块(Diamond),2行代表草花(Club),3行代表黑桃(Spade)。数组的一列对应一张牌的面值——第0到9列依次对应着Ace到Ten、第10到12列分别对应着Jack、Queen、King。要使用字符串数组suit和字符串数组face来分别存储表示花色名的四个字符串和表示牌面值的13个字符串。 任务1:实现洗牌,即随机将编号为1-52的每张牌随机放到deck数组中; 任务2:实现发牌,即将上述1-52张牌依次显示出来,比如Six of Club; 任务3:修改发牌程序,使其能够处理一手五张牌的扑克游戏,然后实现如下功能: (a) 判断这一手牌中是否含有对子; (b) 判断这一手牌中是否含有两个对子; (c) 判断这一手牌中是否含有三张同级的牌; (d) 判断这一手牌中是否含有四张同级的牌; (e) 判断这一手牌中是否有一个同花; (f) 判断这一手牌中是否有一个顺子; (g) 判断这一手牌中是否有一个同花顺; 任务4:利用上面开发的函数,编写一个能处理两手五张扑克牌的程序,该程序分别评价每一手牌,然后判断哪一手更好(从g->a级别依次降低)。
时间: 2023-06-12 16:06:06 浏览: 119
任务1:
```python
import random
# 初始化牌面和花色
face = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Ten', 'Jack', 'Queen', 'King']
suit = ['Heart', 'Diamond', 'Club', 'Spade']
# 初始化一副扑克牌
deck = [['' for j in range(13)] for i in range(4)]
for i in range(4):
for j in range(13):
deck[i][j] = face[j] + ' of ' + suit[i]
# 洗牌
random.shuffle(deck)
# 输出洗好的牌
for i in range(4):
for j in range(13):
print(deck[i][j], end='\t')
print()
```
任务2:
```python
# 初始化牌面和花色
face = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Ten', 'Jack', 'Queen', 'King']
suit = ['Heart', 'Diamond', 'Club', 'Spade']
# 初始化一副扑克牌
deck = [['' for j in range(13)] for i in range(4)]
for i in range(4):
for j in range(13):
deck[i][j] = face[j] + ' of ' + suit[i]
# 洗牌
random.shuffle(deck)
# 发牌
for i in range(5):
print(deck[0][i], deck[1][i], deck[2][i], deck[3][i])
```
任务3:
```python
# 初始化牌面和花色
face = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Ten', 'Jack', 'Queen', 'King']
suit = ['Heart', 'Diamond', 'Club', 'Spade']
# 初始化一副扑克牌
deck = [['' for j in range(13)] for i in range(4)]
for i in range(4):
for j in range(13):
deck[i][j] = face[j] + ' of ' + suit[i]
# 洗牌
random.shuffle(deck)
# 发牌
hand = [deck[0][0], deck[1][0], deck[2][0], deck[3][0], deck[0][1]]
# 判断是否含有对子
faces = [card.split()[0] for card in hand]
if len(set(faces)) == 4:
print('No pairs')
elif len(set(faces)) == 3:
print('One pair')
elif len(set(faces)) == 2:
if faces.count(faces[0]) == 2 or faces.count(faces[1]) == 2:
print('Two pairs')
else:
print('Three of a kind')
else:
print('Four of a kind')
# 判断是否有同花
suits = [card.split()[2] for card in hand]
if len(set(suits)) == 1:
print('Flush')
# 判断是否有顺子
face_values = [face.index(card.split()[0]) for card in hand]
face_values.sort()
if len(set(face_values)) == 5 and face_values[-1] - face_values[0] == 4:
print('Straight')
# 判断是否有同花顺
if len(set(suits)) == 1 and len(set(face_values)) == 5 and face_values[-1] - face_values[0] == 4:
print('Straight flush')
```
任务4:
```python
# 初始化牌面和花色
face = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Ten', 'Jack', 'Queen', 'King']
suit = ['Heart', 'Diamond', 'Club', 'Spade']
# 初始化一副扑克牌
deck = [['' for j in range(13)] for i in range(4)]
for i in range(4):
for j in range(13):
deck[i][j] = face[j] + ' of ' + suit[i]
# 洗牌
random.shuffle(deck)
# 发牌
hand1 = [deck[0][0], deck[1][0], deck[2][0], deck[3][0], deck[0][1]]
hand2 = [deck[0][1], deck[1][1], deck[2][1], deck[3][1], deck[0][2]]
# 定义函数来判断手牌的等级
def evaluate(hand):
faces = [card.split()[0] for card in hand]
suits = [card.split()[2] for card in hand]
face_values = [face.index(card.split()[0]) for card in hand]
face_values.sort()
# 判断是否含有对子
if len(set(faces)) == 4:
return 1
elif len(set(faces)) == 3:
return 2
elif len(set(faces)) == 2:
if faces.count(faces[0]) == 2 or faces.count(faces[1]) == 2:
return 3
else:
return 4
else:
# 判断是否有同花顺
if len(set(suits)) == 1 and len(set(face_values)) == 5 and face_values[-1] - face_values[0] == 4:
return 5
# 判断是否有同花
elif len(set(suits)) == 1:
return 6
# 判断是否有顺子
elif len(set(face_values)) == 5 and face_values[-1] - face_values[0] == 4:
return 7
else:
return 8
# 输出手牌等级
hand1_level = evaluate(hand1)
hand2_level = evaluate(hand2)
print('Hand 1:', hand1, '- Level', hand1_level)
print('Hand 2:', hand2, '- Level', hand2_level)
# 判断哪一手牌更好
if hand1_level < hand2_level:
print('Hand 1 wins')
elif hand1_level > hand2_level:
print('Hand 2 wins')
else:
print('Tie')
```
阅读全文