自己写一个程序,实现发牌、比大小判断输赢。
游戏规则:
一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。
有以下几种牌:
豹子:三张一样的牌,如3张6.
顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7
顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子
对子:2张牌一样
单张:单张最大的是A
这几种牌的大小顺序为, 豹子>顺金>顺子>对子>单张
需程序实现的点:
先生成一付完整的扑克牌
给5个玩家随机发牌
统一开牌,比大小,输出赢家是谁
import random
#分别定义全局变量:牌面值,花色,牌型,玩家姓名
FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A' ]
COLORS = ['黑桃♠', '红心♥', '方块♦', '梅花♣']
TYPE = ['豹子', '顺金', '顺子', '对子', '单张']
names = ['Player1', 'Player2', 'Player3', 'Player4', 'Player5']
dic = {}
all_card = []
PLAYERS = list()
#定义一个Player类,由玩家姓名初始化
for i in COLORS: # 生成卡牌
for j in FACES:
print(i+j)
all_card.append(i+j)
print(all_card)
list1 = [[], [], []]
for s in range(3): # 分配卡牌
for i in range(len(names)):
number = random.choice(all_card)
all_card.remove(number)
print(number)
list1[s].append(number)
print(list1)
for i in names: # 存入字典
t = []
for j in range(len(list1)):
t.append(list1[j][names.index(i)])
dic[i] = t
print(dic)
big = []
for i in names:
card = dic[i]
print('card',card)
if (card[0])[3:] == (card[1])[3:] == (card[2])[3:]:
print('豹子')
big.append('豹子')
elif abs(int(FACES.index(((card[0])[3:])))-int(FACES.index((card[1])[3:]))) == abs(int(FACES.index((card[1])[3:]))-int(FACES.index((card[2])[3:]))) == 1:
print('顺子')
big.append('顺子')
elif (card[0])[0:2] == (card[1])[0:2] == (card[2])[0:2] and abs(int(FACES.index((card[0])[3:]))-FACES.index((card[1])[3:])) == 1 == abs(int(FACES.index((card[1])[3:]))-int(FACES.index((card[2])[3:]))):
print('同花顺')
big.append('同花顺')
elif (card[0])[3:] == (card[1])[3:] or (card[1])[3:] == (card[2])[3:]:
print('对子')
big.append('对子')
else:
if FACES.index((card[0])[3:]) > FACES.index((card[1])[3:]) and FACES.index((card[0])[3:]) > FACES.index((card[2])[3:]):
print((card[0])[3:])
big.append((card[0])[3:])
elif FACES.index((card[1])[3:]) > FACES.index((card[2])[3:]):
print((card[1])[3:])
big.append((card[1])[3:])
else:
print((card[2])[3:])
big.append((card[2])[3:])
print(big)
sort_number = FACES+['对子','顺子','同花顺','豹子']
sn =[]
for i in big:
sn.append(sort_number.index(i))
print(sn)
n = sorted(sn,reverse=True)
print(n)
for i in range(len(n)):
print('第%d名选手排名第%s' % ((i+1),(n.index(sn[i])+1)))