首先
武汉加油,中国加油,中国必胜!
规则简介
在家里待的时间久了,也不太想去学习,就很无聊,就想瞎玩。
不知道大家有没有玩过一个叫“斗牛”的扑克牌游戏,腾讯游戏貌似就有,好像叫“欢乐斗牛”,反正我们这里叫做“牛牛”。
规则很简单,其实说白了就是一种拿扑克牌比大小的游戏,但它在比大小上又有一些小花样,又很容易上手,所以也可以说是很有意思:
- 开始时每人发5张牌;
- 各种花色的A~10算数字的1~10,JQK和大小王算数字10;
- 如果5张牌中有3张牌的点数之和是10的倍数,则称为有“牛”,否则称作没有“牛”;
- 若有“牛”,则将剩下来的两张牌点数之和除以10,求余数,整个牌型称作“牛N”(N为余数);
- 有“牛”的牌型比没有“牛”的牌型大,同有“牛”则比较余数大小,同没有“牛”或有“牛”但余数相同则比较牌面大小;
- “牛0”翻3倍,“牛8”和“牛9”翻两倍。
举例:
- 甲的牌:黑桃4 梅花2 方块4 红桃3 黑桃7 ;
- 乙的牌:梅花8 黑桃2 红桃Q 方块5 方块3 。
结果:
- 甲的牌为“牛(黑桃4 梅花2 方块4)0(红桃3 黑桃7 )”;
- 乙的牌为“牛(梅花8 黑桃2 红桃Q)8(方块5 方块3 )”;
- 甲比乙大,且翻3倍。
实际玩的时候还会有一些不同的规定,比如“庄家”、“下注”什么的,总之还蛮有意思,同时还能锻炼一下计算能力和反应速度。
Python编程
我真的太懒了,不想再写注释之类的了:
import numpy as np
class Poker(object):
"""
@Yaodo
用 类名(人数n) 生成实例
用 实例名.play() 执行游戏
手动比分或者用 实例名.toal 查看庄家总分
(默认1号玩家为庄家)
"""
def __init__(self, n):
super(Poker, self).__init__()
self.n = n
self.total = 0
#生成牌堆
def get_a_deck(self):
self.deck = {}
self.suits = {'S': '黑桃', 'H': '红桃', 'D': '方块', 'C': '梅花'}
self.rank = {str(i): i for i in range(2, 11)}
for i in 'JQK':
self.rank[i] = 10
self.rank['A'] = 1
for s in self.suits.keys():
for r in self.rank.keys():
self.deck[s+r] = self.rank[r]
self.deck['JOCKER'] = 10
self.deck['jocker'] = 10
#生成用来比较牌面大小的一副牌的字典
self.rank_deck = {}
self.rank_deck['JOCKER'] = 15
self.rank_deck['jocker'] = 14
self.rank2 = {str(i): i for i in range(2, 11)}
for i in range(len('JQK')):
self.rank2['JQK'[i]] = 11+i
self.rank2['A'] = 1
for s in self.suits.keys():
for r in self.rank2.keys():
self.rank_deck[s+r] = self.rank2[r]
#无放回地抽一张牌
def draw_a_card(self):
keys = [k for k in self.deck.keys()]
n = len(self.deck)
index = np.random.randint(n)
card = keys[index]
self.deck.pop(card)
return card
#读取牌面
def read_cards(self, cards):
for card in cards:
if card[0] == 'J':
print('大王', end= ' ')
elif card[0] == 'j':
print('小王', end= ' ')
else:
print(self.suits[card[0]]+ card[1:], end= ' ')
print()
#每人抽5张牌
def draw_cards(self):
self.cards = {}
for player in range(self.n):
self.cards[player] = []
for i in range(5):
self.cards[player].append(self.draw_a_card())
self.get_a_deck()
#读取牌型,生成代表牌型大小的JSON列表
def rank_cards(self, cards):
stop = 0
for a in range(4):
if stop == 1 : break
A = cards[a]
for b in range(a+1,5):
if stop == 1 : break
for c in range(b+1,5):
B = cards[b]
C = cards[c]
if (self.deck[A] + self.deck[B] + self.deck[C]) % 10 == 0:
NIU = [A, B, C]
cards.remove(A)
cards.remove(B)
cards.remove(C)
niu = cards
sum = 0
for card in cards:
sum += self.deck[card]
remain = sum % 10
stop = 1
print('牛{}'.format(remain))
print('牛:', end='')
self.read_cards(NIU)
print('剩余的两张牌:', end='')
self.read_cards(cards)
piece = {
'N': 1,
'r': remain,
'n': niu
}
break
else:
print('无牛')
piece = {
'N':0,
'r':'',
'n': cards
}
self.read_cards(cards)
return piece
#输出每位玩家的牌型
def rank_players(self):
self.comp = []
print('共{}名玩家,结果如下:\n'.format(self.n))
for i in range(self.n):
print('第{}位玩家:'.format(i+1))
self.comp.append(self.rank_cards(self.cards[i]))
print()
#将牌面转化为数值
def n_rank(self, dic):
n_rank = []
for i in range(len(dic['n'])):
n_rank.append(self.rank_deck[dic['n'][i]])
n_rank.sort(reverse=True)
return n_rank
#比较两人的牌面大小
def compare(self, dic1, dic2):
if dic1['N'] > dic2['N']:
if dic1['r'] == 0:
score = +3
elif (dic1['r'] == 9) or (dic1['r'] == 8):
score = +2
else:
score = +1
elif dic1['N'] < dic2['N']:
if dic2['r'] == 0:
score = -3
elif (dic2['r'] == 9) or (dic2['r'] == 8):
score = -2
else:
score = -1
else:
if dic1['r'] > dic2['r']:
if dic2['r'] == 0:
score = -3
elif (dic1['r'] == 9) or (dic1['r'] == 8):
score = +2
else:
score = +1
elif dic1['r'] < dic2['r']:
if dic1['r'] == 0:
score = +3
elif (dic2['r'] == 9) or (dic2['r'] == 8):
score = -2
else:
score = -1
else:
n_rank1 = self.n_rank(dic1)
n_rank2 = self.n_rank(dic2)
if n_rank1 > n_rank2:
score = +1
elif n_rank1 < n_rank2:
score = -1
else:
score = 0
if dic1['r'] == 0:
score = score * 3
elif (dic1['r'] == 9) or (dic1['r'] == 8):
score = score * 2
return score
#自动计算庄家得分
def get_score(self):
for i in range(1, len(self.comp)):
score = self.compare(self.comp[0], self.comp[i])
self.total += score
#执行一局游戏
def play(self):
self.get_a_deck()
self.draw_cards()
self.rank_players()
self.get_score()
A = Poker(5)
A.play()
print('庄家A的最终得分:{}\n'.format(A.total))
想要玩玩的话,下载这个文件也可以:SingleNiuNiu.py
最后的效果大概是这样:
Comments | NOTHING