当前位置:   article > 正文

麻将算法-支持单耗子-双耗子-多耗子_ifmj

ifmj
  1. # coding=utf-8
  2. import sys
  3. import math
  4. import codecs
  5. isDebug = False
  6. isTiaoShi = True
  7. isFengZui = True
  8. MAJONG = [
  9. 101,102,103,104,105,106,107,108,109,
  10. 101,102,103,104,105,106,107,108,109,
  11. 101,102,103,104,105,106,107,108,109,
  12. 101,102,103,104,105,106,107,108,109,
  13. 201,202,203,204,205,206,207,208,209,
  14. 201,202,203,204,205,206,207,208,209,
  15. 201,202,203,204,205,206,207,208,209,
  16. 201,202,203,204,205,206,207,208,209,
  17. 301,302,303,304,305,306,307,308,309,
  18. 301,302,303,304,305,306,307,308,309,
  19. 301,302,303,304,305,306,307,308,309,
  20. 301,302,303,304,305,306,307,308,309,
  21. 401,402,403,404,405,406,407,
  22. 401,402,403,404,405,406,407,
  23. 401,402,403,404,405,406,407,
  24. 401,402,403,404,405,406,407,
  25. ]
  26. MAJIANG_TYPE = [
  27. 101,102,103,104,105,106,107,108,109,
  28. 201,202,203,204,205,206,207,208,209,
  29. 301,302,303,304,305,306,307,308,309,
  30. 401,402,403,404,405,406,407,
  31. ]
  32. #将手里的耗子全部拿出来,并且对剩余的手牌排序
  33. #param mahjongList 手牌列表 haoziPai 耗子列表
  34. #return 自己所有的耗子列表
  35. def check_haozi_sort(mahjongList, haoziPaiList):
  36. printLog('当前的手牌为:',mahjongList)
  37. mahjongList.sort()
  38. printLog('排序完成之后手牌为:',mahjongList)
  39. haoziList = []
  40. for index in range(len(mahjongList)-1, -1, -1):
  41. for x in range(len(haoziPaiList)):
  42. if haoziPaiList[x] == mahjongList[index]:
  43. haoziList.append(mahjongList[index])
  44. mahjongList.pop(index)
  45. break
  46. printLog('移除耗子之后的手牌为:')
  47. printLog(mahjongList)
  48. printLog('手里的耗子列表:', haoziList)
  49. return haoziList
  50. #将手牌分为万条筒、 字
  51. def split_list(mahjongList):
  52. wttList = []
  53. ziList = []
  54. for index in range(len(mahjongList)):
  55. mjType = mahjongList[index] // 100
  56. #万
  57. if mjType == 1 or mjType == 2 or mjType == 3:
  58. wttList.append(mahjongList[index])
  59. elif mjType == 4:
  60. ziList.append(mahjongList[index])
  61. else:
  62. printLog("没有这种麻将类型", mjType)
  63. sys.exit(0)
  64. return wttList, ziList
  65. #检查组合 先检查刻,再检查顺
  66. #return 返回组成的刻,顺,以及检查完之后剩余的手牌列表
  67. def check_zuhe_ke_shun( mjList, isFeng=False):
  68. mjListCopy = mjList[:]
  69. shunziList = []
  70. keList = []
  71. #自己的数量 先判断能自然形成的,不加耗子的
  72. if len(mjListCopy) >= 3:
  73. keList = check_ke(mjListCopy)
  74. if len(mjListCopy) >= 3:
  75. #如果是字牌,并且是封嘴的时候,去检测一下风顺子
  76. if isFeng:
  77. if isFengZui:
  78. shunziList = check_shunzi(mjListCopy, isFeng)
  79. else:
  80. shunziList = check_shunzi(mjListCopy, isFeng)
  81. # #组成的刻子现在是这样的
  82. # if len(keList) > 0:
  83. # printLog('组成刻子:', keList)
  84. # if len(shunziList) > 0:
  85. # printLog('组成顺子:', shunziList)
  86. # #检测完之后的手牌为
  87. # if len(mjListCopy) > 0:
  88. # printLog('检测完之后的手牌', mjListCopy)
  89. return keList, shunziList, mjListCopy
  90. #检查组合 先检查顺,再检查刻
  91. #return 返回组成的刻,顺,以及检查完之后剩余的手牌列表
  92. def check_zuhe_shun_ke( mjList, isFeng=False):
  93. mjListCopy = mjList[:]
  94. shunziList = []
  95. keList = []
  96. #自己的数量 先判断能自然形成的,不加耗子的
  97. if len(mjListCopy) >= 3:
  98. #如果是字牌,并且是封嘴的时候,去检测一下风顺子
  99. if isFeng:
  100. if isFengZui:
  101. shunziList = check_shunzi(mjListCopy, isFeng)
  102. else:
  103. shunziList = check_shunzi(mjListCopy, isFeng)
  104. if len(mjListCopy) >= 3:
  105. keList = check_ke(mjListCopy)
  106. # #组成的刻子现在是这样的
  107. # if len(keList) > 0:
  108. # printLog('组成刻子:', keList)
  109. # if len(shunziList) > 0:
  110. # printLog('组成顺子:', shunziList)
  111. # #检测完之后的手牌为
  112. # if len(mjListCopy) > 0:
  113. # printLog('检测完之后的手牌', mjListCopy)
  114. return keList, shunziList, mjListCopy
  115. #mjList 风 给所有的风进行排序
  116. #return [[403,2],[402,1],[401,1]]
  117. def feng_sort(mjList):
  118. mjlsit_sort = []
  119. if len(mjList) == 0:
  120. return mjlsit_sort
  121. mjList.sort()
  122. mjlsit_sort = []
  123. count = 1
  124. curValue = mjList[0]
  125. for x in range(1, len(mjList)):
  126. if mjList[x] == curValue:
  127. count = count + 1
  128. else:
  129. tmpList = []
  130. tmpList.append(curValue)
  131. tmpList.append(count)
  132. mjlsit_sort.append(tmpList)
  133. curValue = mjList[x]
  134. count = 1
  135. tmpList = []
  136. tmpList.append(curValue)
  137. tmpList.append(count)
  138. mjlsit_sort.append(tmpList)
  139. printLog('排完前的数组为:',mjlsit_sort)
  140. mjlsit_sort.sort(key=takeSecond)
  141. mjlsit_sort.reverse()
  142. printLog('排完序的数组为:',mjlsit_sort)
  143. return mjlsit_sort
  144. def takeSecond(elem):
  145. return elem[1]
  146. #将手中的字牌去分成 [东南西北] ,[中发白]
  147. def split_feng_zi(mjList):
  148. fenglist = []
  149. ziList = []
  150. if len(mjList) == 0:
  151. return fenglist,ziList
  152. mjListCopy = mjList[:]
  153. mjListCopy.sort()
  154. for x in range(len(mjListCopy)):
  155. if mjListCopy[x] >= 405:
  156. ziList.append(mjListCopy[x])
  157. else:
  158. fenglist.append(mjListCopy[x])
  159. return fenglist,ziList
  160. #检查风顺子 fengList 当前的[东南西北]或者[中发白]
  161. #mjList 这个参数的主要目的是再检查出自己牌型有风顺子之后, 将组合完成的从自己的手牌里面移除掉
  162. #return 返回的是组成的风顺子列表
  163. def chenck_feng_shun(fengList, mjList):
  164. fengListCopy = fengList[:]
  165. feng_shun_list = []
  166. if len(fengListCopy) >= 3:
  167. curIndex = 0
  168. for index in range(fengListCopy[0][1]):
  169. tmpList = []
  170. while curIndex < len(fengListCopy):
  171. curCount = fengListCopy[curIndex][1]
  172. if curCount > 0:
  173. tmpList.append(fengListCopy[curIndex][0])
  174. fengListCopy[curIndex][1] = curCount - 1
  175. if len(tmpList) == 3:
  176. curIndex = 0
  177. feng_shun_list.append(tmpList)
  178. break
  179. else:
  180. curIndex = curIndex + 1
  181. if curIndex >= len(fengListCopy):
  182. curIndex = 0
  183. break
  184. else:
  185. curIndex = curIndex + 1
  186. continue
  187. printLog('当前的风牌手牌', feng_shun_list)
  188. if len(feng_shun_list) > 0:
  189. for x in range(len(feng_shun_list)):
  190. mjList.pop(mjList.index(feng_shun_list[x][0]))
  191. mjList.pop(mjList.index(feng_shun_list[x][1]))
  192. mjList.pop(mjList.index(feng_shun_list[x][2]))
  193. return feng_shun_list
  194. #检查手牌里面的刻, 这个是不用耗子就能直接做刻子的
  195. def check_ke(mjList):
  196. mjListCopy = mjList[:]
  197. mjListCopy2 = mjList[:]
  198. keList = []
  199. for index in range(len(mjListCopy)):
  200. if len(mjList) < 3:
  201. break
  202. tmpValue = mjListCopy[index]
  203. try:
  204. #找到一个就移除一个,如果能找到最后面,说明是对的
  205. value0 = mjListCopy2.index(tmpValue)
  206. mjListCopy2.pop(mjListCopy2.index(tmpValue))
  207. value1 = mjListCopy2.index(tmpValue)
  208. mjListCopy2.pop(mjListCopy2.index(tmpValue))
  209. value2 = mjListCopy2.index(tmpValue)
  210. mjListCopy2.pop(mjListCopy2.index(tmpValue))
  211. tmpList = []
  212. tmpList.append(tmpValue)
  213. tmpList.append(tmpValue)
  214. tmpList.append(tmpValue)
  215. keList.append(tmpList)
  216. except ValueError, e:
  217. continue
  218. #所有的检测都检测完了,这个时候 要对真正的手牌进行移除了
  219. if len(keList) > 0:
  220. for index in range(len(keList)):
  221. mjList.pop(mjList.index(keList[index][0]))
  222. mjList.pop(mjList.index(keList[index][0]))
  223. mjList.pop(mjList.index(keList[index][0]))
  224. return keList
  225. else:
  226. return keList
  227. #检查手牌里面的顺子, 这个是不用耗子就能直接做顺子的
  228. def check_shunzi(mjList, isFeng=False):
  229. shunziList = []
  230. mjListCopy = mjList[:]
  231. tmpIndex = 0
  232. if not isFeng:
  233. for index in range(len(mjListCopy)):
  234. #如果数组的长度小于3了就不用做检测了
  235. if len(mjList) < 3:
  236. break
  237. tmpValue = mjListCopy[index]
  238. try:
  239. #先检测一下这个顺子的有没有
  240. value0 = mjList.index(tmpValue)
  241. value1 = mjList.index(tmpValue+1)
  242. value2 = mjList.index(tmpValue+2)
  243. #上面检测成功了,说明有,现在要移除掉
  244. mjList.pop(mjList.index(tmpValue))
  245. mjList.pop(mjList.index(tmpValue+1))
  246. mjList.pop(mjList.index(tmpValue+2))
  247. tmpList = []
  248. tmpList.append(tmpValue)
  249. tmpList.append(tmpValue+1)
  250. tmpList.append(tmpValue+2)
  251. shunziList.append(tmpList)
  252. except ValueError, e:
  253. continue
  254. return shunziList
  255. else:
  256. #检测风顺子
  257. fengList, ziList = split_feng_zi(mjListCopy)
  258. feng_sort_list = feng_sort(fengList)
  259. zi_sort_list = feng_sort(ziList)
  260. feng_shun_list = chenck_feng_shun(feng_sort_list, mjList)
  261. zi_shun_list = chenck_feng_shun(zi_sort_list, mjList)
  262. feng_shun_list = feng_shun_list + zi_shun_list
  263. return feng_shun_list
  264. #检测手里面有两张牌,加耗子之后组成刻子(只有耗子玩法才去调用这个方法)
  265. #return 返回刻子列表和需要的耗子数量
  266. def check_2n_ke(mjList, haoziId):
  267. mjListCopy = mjList[:]
  268. mjListCopy2 = mjList[:]
  269. keList = []
  270. needHaoziCount = 0
  271. for index in range(len(mjListCopy)):
  272. if len(mjList) < 2:
  273. break
  274. tmpValue = mjListCopy[index]
  275. try:
  276. #找到一个就移除一个,如果能找到最后面,说明是对的
  277. value0 = mjListCopy2.index(tmpValue)
  278. mjListCopy2.pop(mjListCopy2.index(tmpValue))
  279. value1 = mjListCopy2.index(tmpValue)
  280. mjListCopy2.pop(mjListCopy2.index(tmpValue))
  281. tmpList = []
  282. tmpList.append(haoziId)
  283. tmpList.append(tmpValue)
  284. tmpList.append(tmpValue)
  285. keList.append(tmpList)
  286. needHaoziCount = needHaoziCount + 1
  287. except ValueError, e:
  288. continue
  289. if len(keList) > 0:
  290. for index in range(len(keList)):
  291. mjList.pop(mjList.index(keList[index][1]))
  292. mjList.pop(mjList.index(keList[index][1]))
  293. return keList, needHaoziCount
  294. else:
  295. return keList,needHaoziCount
  296. #检测手里面有两张牌,加耗子之后组成顺子(只有耗子玩法才去调用这个方法)
  297. #return 返回顺子列表和需要的耗子数量
  298. def check_2n_shun(mjList, haoziId, isFeng=False):
  299. mjListCopy = mjList[:]
  300. shunziList = []
  301. needHaoziCount = 0
  302. if not isFeng:
  303. for index in range(len(mjListCopy)):
  304. if len(mjList) < 2:
  305. break
  306. tmpValue = mjListCopy[index]
  307. #这个检测的是检测靠着
  308. try:
  309. #先检测一下这个顺子的有没有
  310. value0 = mjList.index(tmpValue)
  311. value1 = mjList.index(tmpValue + 1)
  312. #上面检测成功了,说明有,现在要移除掉
  313. mjList.pop(mjList.index(tmpValue))
  314. mjList.pop(mjList.index(tmpValue+1))
  315. tmpList = []
  316. tmpList.append(haoziId)
  317. tmpList.append(tmpValue)
  318. tmpList.append(tmpValue+1)
  319. needHaoziCount = needHaoziCount + 1
  320. shunziList.append(tmpList)
  321. continue
  322. except ValueError, e:
  323. pass
  324. #这个检测是间隔一个的
  325. try:
  326. #先检测一下这个顺子的有没有
  327. value0 = mjList.index(tmpValue)
  328. value1 = mjList.index(tmpValue + 2)
  329. #上面检测成功了,说明有,现在要移除掉
  330. mjList.pop(mjList.index(tmpValue))
  331. mjList.pop(mjList.index(tmpValue+2))
  332. tmpList = []
  333. tmpList.append(tmpValue)
  334. tmpList.append(haoziId)
  335. tmpList.append(tmpValue+2)
  336. needHaoziCount = needHaoziCount + 1
  337. shunziList.append(tmpList)
  338. except ValueError, e:
  339. continue
  340. return shunziList, needHaoziCount
  341. else:
  342. fengList, ziList = split_feng_zi(mjListCopy)
  343. feng_sort_list = feng_sort(fengList)
  344. zi_sort_list = feng_sort(ziList)
  345. feng_shun_list, needHaoziCount1 = check_2n_feng_shun(feng_sort_list,haoziId, mjList)
  346. zi_shun_list, needHaoziCount2 = check_2n_feng_shun(zi_sort_list, haoziId,mjList)
  347. feng_shun_list = feng_shun_list + zi_shun_list
  348. needHaoziCount = needHaoziCount1 + needHaoziCount2
  349. return feng_shun_list, needHaoziCount
  350. #检测风顺子
  351. def check_2n_feng_shun(fengList, haoziId, mjList):
  352. fengListCopy = fengList[:]
  353. feng_shun_list = []
  354. needHaoziCount = 0
  355. if len(fengListCopy) >= 2:
  356. curIndex = 0
  357. for index in range(fengListCopy[0][1]):
  358. tmpList = []
  359. while curIndex < len(fengListCopy):
  360. curCount = fengListCopy[curIndex][1]
  361. if curCount > 0:
  362. tmpList.append(fengListCopy[curIndex][0])
  363. fengListCopy[curIndex][1] = curCount - 1
  364. if len(tmpList) == 2:
  365. needHaoziCount = needHaoziCount + 1
  366. tmpList.append(haoziId)
  367. curIndex = 0
  368. feng_shun_list.append(tmpList)
  369. break
  370. else:
  371. curIndex = curIndex + 1
  372. if curIndex >= len(fengListCopy):
  373. curIndex = 0
  374. break
  375. else:
  376. curIndex = curIndex + 1
  377. continue
  378. if len(feng_shun_list) > 0:
  379. for x in range(len(feng_shun_list)):
  380. mjList.pop(mjList.index(feng_shun_list[x][0]))
  381. mjList.pop(mjList.index(feng_shun_list[x][1]))
  382. printLog('当前的风牌手牌', feng_shun_list)
  383. return feng_shun_list, needHaoziCount
  384. def printLog(*args):
  385. if isDebug:
  386. for str in args:
  387. if isinstance(str, basestring):
  388. print(str.decode('UTF-8').encode('GBK'))
  389. else:
  390. print(str)
  391. def printNow(*args):
  392. if isTiaoShi:
  393. for str in args:
  394. if isinstance(str, basestring):
  395. print(str.decode('UTF-8').encode('GBK'))
  396. else:
  397. print(str)
  398. def has_object( objList, obj ):
  399. for x in range(len(objList)):
  400. if objList[x] == obj:
  401. return True
  402. return False
  403. #去检测将
  404. def check_jiang(mjList, jiangType, haoziId, haoziList):
  405. mjListCopy = mjList[:]
  406. jiangList = []
  407. needHaoziCount = 0
  408. canjiang = False
  409. if has_object(mjListCopy, jiangType):
  410. mjListCopy.pop(mjListCopy.index(jiangType))
  411. if has_object(mjListCopy, jiangType):
  412. mjList.pop(mjList.index(jiangType))
  413. mjList.pop(mjList.index(jiangType))
  414. jiangList.append(jiangType)
  415. jiangList.append(jiangType)
  416. mjList = mjListCopy
  417. canjiang = True
  418. return canjiang, jiangList, needHaoziCount
  419. else:
  420. if haoziId != 0 and len(haoziList) >= 1:
  421. mjList.pop(mjList.index(jiangType))
  422. jiangList.append(jiangType)
  423. jiangList.append(haoziId)
  424. haoziList.pop(0)
  425. needHaoziCount = 1
  426. canjiang = True
  427. mjList = mjListCopy
  428. return canjiang, jiangList, needHaoziCount
  429. else:
  430. return canjiang, jiangList, needHaoziCount
  431. def can_hu_ke_shun(mjList, haoziId, haoziList):
  432. #拆分万条筒风
  433. wttList, ziList = split_list(mjList)
  434. #检测 先不带耗子 万条筒风 刻 顺 剩余手牌 组合
  435. keList,shunziList,handCardList = check_zuhe_ke_shun(wttList)
  436. zikList, zisList,ziHandCardList = check_zuhe_ke_shun(ziList, True)
  437. #只有有耗子的时候才去检测添加耗子
  438. if haoziId != 0:
  439. printLog('带耗子检测:')
  440. allList = keList + shunziList + zikList + zisList
  441. #如果手里面的已经组成四副了 说明肯定能胡了
  442. if len(allList) >= 4:
  443. allHands = handCardList + ziHandCardList
  444. return True, allList, allHands
  445. else:
  446. haoziKeList, haoziCount1 = check_2n_ke(handCardList, haoziId)
  447. haoziShunList, haoziCount2 = check_2n_shun(handCardList, haoziId)
  448. zikeList,haoziCount3 = check_2n_ke(ziHandCardList, haoziId)
  449. zi_shun_list = []
  450. haoziCount4 = 0
  451. if isFengZui:
  452. zi_shun_list,haoziCount4 = check_2n_shun(ziHandCardList,haoziId, True)
  453. leftAllHand = ziHandCardList + handCardList
  454. needAllHaoziCount = haoziCount1 + haoziCount2 + haoziCount3 + haoziCount4 + len(leftAllHand) * 2
  455. leftAllHandList = []
  456. for x in range(len(leftAllHand)):
  457. tmpList=[]
  458. tmpList.append(leftAllHand[x])
  459. tmpList.append(haoziId)
  460. tmpList.append(haoziId)
  461. leftAllHandList.append(tmpList)
  462. allList = allList + haoziKeList + haoziShunList + zikeList + zi_shun_list
  463. handCards = handCardList + ziHandCardList
  464. if needAllHaoziCount <= len(haoziList):
  465. return True, allList + leftAllHandList, []
  466. else:
  467. return False,allList, handCards
  468. else:
  469. printLog('不带耗子检测:')
  470. #组合完成了,没耗子的 这个时候就要判断生成的铺了,如果生成的铺是4个,并且手里面的剩余牌正好是一对,则说明能胡
  471. allList = keList + shunziList + zikList + zisList
  472. allHands = handCardList + ziHandCardList
  473. if len(allList) == 4:
  474. return True, allList, allHands
  475. else:
  476. if len(allHands) == 0:
  477. return True, allList, allHands
  478. else:
  479. return False,allList, allHands
  480. def can_hu_shun_ke(mjList, haoziId, haoziList):
  481. #拆分万条筒风
  482. wttList, ziList = split_list(mjList)
  483. #检测 先不带耗子 万条筒风 刻 顺 剩余手牌 组合
  484. keList,shunziList,handCardList = check_zuhe_shun_ke(wttList)
  485. printLog('字')
  486. zikList, zisList,ziHandCardList = check_zuhe_shun_ke(ziList, True)
  487. #只有有耗子的时候才去检测添加耗子
  488. if haoziId != 0:
  489. printLog('带耗子检测:')
  490. allList = keList + shunziList + zikList + zisList
  491. #如果手里面的已经组成四副了, 说明肯定能胡了
  492. if len(allList) >= 4:
  493. allHands = handCardList + ziHandCardList
  494. return True, allList, allHands
  495. else:
  496. haoziKeList, haoziCount1 = check_2n_ke(handCardList, haoziId)
  497. haoziShunList, haoziCount2 = check_2n_shun(handCardList, haoziId)
  498. zikeList,haoziCount3 = check_2n_ke(ziHandCardList, haoziId)
  499. zi_shun_list = []
  500. haoziCount4 = 0
  501. if isFengZui:
  502. zi_shun_list,haoziCount4 = check_2n_shun(ziHandCardList,haoziId, True)
  503. leftAllHand = ziHandCardList + handCardList
  504. needAllHaoziCount = haoziCount1 + haoziCount2 + haoziCount3 + haoziCount4 + len(leftAllHand) * 2
  505. leftAllHandList = []
  506. for x in range(len(leftAllHand)):
  507. tmpList=[]
  508. tmpList.append(leftAllHand[x])
  509. tmpList.append(haoziId)
  510. tmpList.append(haoziId)
  511. leftAllHandList.append(tmpList)
  512. allList = allList + haoziKeList + haoziShunList + zikeList + zi_shun_list
  513. handCards = handCardList + ziHandCardList
  514. if needAllHaoziCount <= len(haoziList):
  515. return True, allList+leftAllHandList, []
  516. else:
  517. return False,allList, handCards
  518. else:
  519. printLog('不带耗子检测:')
  520. #组合完成了,没耗子的 这个时候就要判断生成的铺了,如果生成的铺是4个,并且手里面的剩余牌正好是一对,则说明能胡
  521. allList = keList + shunziList + zikList + zisList
  522. allHands = handCardList + ziHandCardList
  523. if len(allList) == 4:
  524. return True, allList, allHands
  525. else:
  526. return False,allList, allHands
  527. def check_ting(mjList, haoziId, haoziList):
  528. #检测听牌的话,就是将自己的手牌给移除一张,再加上一张任意牌,看看能不能胡
  529. #mjList 现在已经移除掉所有的耗子牌了,
  530. mjListCopy = mjList[:]
  531. ting_cards = []
  532. for index in range(len(mjListCopy)):
  533. mjListCopy2 = mjList[:]
  534. mjValue = mjListCopy2.pop(index)
  535. hu_cards = []
  536. for addMjIndex in range(len(MAJIANG_TYPE)):
  537. mjListCopy3 = mjListCopy2[:]
  538. mjListCopy3.append(MAJIANG_TYPE[addMjIndex])
  539. mjListCopy3.sort()
  540. for i in range(len(mjListCopy3)):
  541. mjListCopy4 = mjListCopy3[:]
  542. canjiang, jiangList, needHaoziCount = check_jiang(mjListCopy4, mjListCopy3[i], haoziId, haoziList)
  543. if canjiang:
  544. canhu,allList, allHands = can_hu_ke_shun(mjListCopy4, haoziId, haoziList)
  545. if canhu:
  546. if needHaoziCount == 1:
  547. haoziList.append(haoziId)
  548. hu_cards.append(MAJIANG_TYPE[addMjIndex])
  549. break
  550. else:
  551. canhu, allList, allHands = can_hu_shun_ke(mjListCopy4, haoziId, haoziList)
  552. if canhu:
  553. if needHaoziCount == 1:
  554. haoziList.append(haoziId)
  555. hu_cards.append(MAJIANG_TYPE[addMjIndex])
  556. break
  557. else:
  558. if needHaoziCount == 1:
  559. haoziList.append(haoziId)
  560. continue
  561. else:
  562. continue
  563. if len(hu_cards) > 0:
  564. try:
  565. value = ting_cards.index(mjValue)
  566. ting_cards[value+1] = hu_cards
  567. except ValueError, e:
  568. ting_cards.append(mjValue)
  569. ting_cards.append(hu_cards)
  570. if len(ting_cards) > 0:
  571. printNow('当前的牌型可以听')
  572. for index in range(0, len(ting_cards) - 1, 2):
  573. printNow('打:', ting_cards[index])
  574. printNow('听:', ting_cards[index+1])
  575. return ting_cards
  576. def check_hu(mjList, haoziId, haoziList):
  577. canhu = False
  578. allList = []
  579. allHands = []
  580. for i in range(len(mjList)):
  581. mjListCopy4 = mjList[:]
  582. canjiang, jiangList, needHaoziCount = check_jiang(mjListCopy4, mjList[i], haoziId, haoziList)
  583. if canjiang:
  584. canhu,allList, allHands = can_hu_ke_shun(mjListCopy4, haoziId, haoziList)
  585. if canhu:
  586. allList.append(jiangList)
  587. break
  588. else:
  589. canhu, allList, allHands = can_hu_shun_ke(mjListCopy4, haoziId, haoziList)
  590. if canhu:
  591. allList.append(jiangList)
  592. break
  593. else:
  594. if needHaoziCount == 1:
  595. haoziList.append(haoziId)
  596. continue
  597. else:
  598. continue
  599. return canhu, allList, allHands
  600. def get_max_hu():
  601. pass
  602. def init(mjList, modePai, haoziPaiList, isTestTing):
  603. canhu = False
  604. allList = []
  605. allHands = []
  606. mahjongList = mjList
  607. modePai = modePai
  608. mahjongList.append(modePai)
  609. haoziPaiList = haoziPaiList
  610. haoziPai = haoziPaiList[0]
  611. #将手牌里面的耗子牌移除掉,并且记录耗子的数量
  612. haoziList = check_haozi_sort(mahjongList,haoziPaiList)
  613. if isTestTing:
  614. tingCards = check_ting(mahjongList, haoziPai, haoziList)
  615. return canhu, allList, allHands
  616. else:
  617. canhu,allList,allHands = check_hu(mahjongList, haoziPai, haoziList)
  618. haoziSort = feng_sort(haoziList)
  619. if len(haoziPaiList) > 1:
  620. count = 0
  621. for index in range(len(haoziSort)):
  622. if haoziPai == haoziSort[index][0]:
  623. count = haoziSort[index][1]
  624. curCount = 0
  625. for x in range(len(allList)):
  626. for y in range(len(allList[x])):
  627. if allList[x][y] == haoziPai:
  628. curCount = curCount + 1
  629. if curCount > count:
  630. allList[x][y] = haoziPaiList[1]
  631. printNow(canhu)
  632. return canhu,allList,allHands
  633. def chenck_fengshun(mjList, haoziId):
  634. mjList.sort()
  635. allLenth = len(mjList)
  636. fengKeList = []
  637. fengShunList = []
  638. needHaoziCount = 0
  639. #数量能正好组成扑
  640. if allLenth % 3 == 0:
  641. mjListCopy = mjList[:]
  642. #拿出刻子
  643. fengKeList = check_ke(mjListCopy)
  644. fengSort = feng_sort(mjListCopy)
  645. #检测风顺子
  646. fengShunList = chenck_feng_shun(fengSort, mjListCopy)
  647. if len(mjListCopy) > 0: #说明刻子没拿完
  648. fengSort = feng_sort(mjListCopy)
  649. if len(fengSort) == 2:
  650. needHaoziCount = 3
  651. fengSort = feng_sort(mjList)
  652. fengShunList = chenck_feng_shun(fengSort, mjList)
  653. if len(mjList) == 0:
  654. needHaoziCount = 0
  655. else:
  656. needHaoziCount = 3
  657. else:
  658. needHaoziCount = 0
  659. else:
  660. fengKeList = check_ke(mjList)
  661. needHaoziCount = 3 - len(mjList) % 3
  662. printNow('需要的耗子数量为', needHaoziCount)
  663. mjList = [ 401,401,401,402,402,404]
  664. chenck_fengshun(mjList, 0)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/930824
推荐阅读
相关标签
  

闽ICP备14008679号