Created
August 7, 2014 13:43
-
-
Save anonymous/9e4a9300be911a02ae2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
输入: list[14] item是长度为2的字符串,第一个字符为牌型,第二个为数字1-9 | |
输出: 所有听牌的方式 | |
测试输入:['@1','@2','@7','L4','L5','L6','@3','@8','@9','@1','@1','@2','@2','@9'] | |
输出: | |
@1@1@1 @2@2@2 @7@8@9 L4L5L6 然后随便打一张 | |
@1@1@1 @2@2@2 L4L5L6 @9@9 @7@8 | |
@1@1@1 @7@8@9 L4L5L6 @2@2 @2@3 | |
""" | |
# 返回和牌x构成顺牌的组合,比如输入@2, 返回[[@1,@2,@3],[@2,@3,@4]] | |
def sunpai(x): | |
x = list(x) | |
n = int(x[1]) | |
k = [] | |
l = 1 if n-2<1 else n-2 | |
r = 7 if n>7 else n | |
for i in range(l, r+1): | |
kp = ["%s%d"%(x[0], i), "%s%d"%(x[0], i+1), "%s%d"%(x[0], i+2)] | |
k.append(kp) | |
return k | |
# 将牌x从list(A)移动到B | |
def move(A, B, x): | |
for item in x: | |
A.remove(item) | |
B.append(item) | |
# shoupai = 手牌 | |
# done为从shoupai中抽取出来构成的牌 | |
# 整个函数不断的从手牌中取刻牌、将牌或者顺牌出来放到done中 | |
ans = [] | |
def isTing(shoupai, done): | |
#复制新的list实例 | |
shoupai = shoupai[:] | |
done = done[:] | |
# 能构成12或者13张则能听牌 | |
if len(done) == 12: | |
if not sorted(done) in ans: | |
ans.append(sorted(done)) | |
if len(done) == 13: | |
if not sorted(done[:-4])+done[-4:] in ans: | |
ans.append(sorted(done[:-4])+done[-4:]) | |
# 开始从手牌中抽取 | |
for x in set(shoupai): | |
# 顺牌 | |
if shoupai.count(x) >=3: | |
move(shoupai, done, [x]*3) | |
isTing(shoupai, done) | |
move(done, shoupai, [x]*3) | |
# 刻牌 | |
for y in sunpai(x): | |
if set(y).issubset(set(shoupai)): | |
move(shoupai, done, y) | |
isTing(shoupai, done) | |
move(done, shoupai, y) | |
# 将牌的情况 | |
if len(shoupai) == 5: | |
for y in set(shoupai): | |
if shoupai.count(y)>=2: | |
move(shoupai, done, [y]*2) | |
isTing(shoupai, done) | |
move(done, shoupai, [y]*2) | |
# 将牌之后还要两张连着的牌 | |
if len(shoupai) == 3: | |
for y in set(shoupai): | |
n = int(list(y)[1]) | |
p1 = "%s%d"%(list(y)[0], n+1) | |
if p1 in set(shoupai): | |
move(shoupai, done, [y, p1]) | |
isTing(shoupai, done) | |
move(done, shoupai, [y, p1]) | |
isTing(['@1', '@2', '@7', 'L4', 'L5', 'L6', '@3', '@8', '@9', '@1', '@1', '@2', '@2', '@9'], []) | |
for x in ans: | |
if len(x) == 12: | |
print "%s %s %s %s 然后随便打一张" % (''.join(x[0:3]), ''.join(x[3:6]), ''.join(x[6:9]), ''.join(x[9:12])) | |
if len(x) == 13: | |
print "%s %s %s %s %s" % (''.join(x[0:3]), ''.join(x[3:6]), ''.join(x[6:9]), ''.join(x[9:11]), ''.join(x[11:13])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment