欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

poker游戏编码规则

发布时间:2024/1/8 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 poker游戏编码规则 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
#!/usr/bin/python # -*- coding: UTF-8 -*-import operatorcards_color = {u'黑桃': 4,u'红桃': 3,u'梅花': 2,u'方块': 1} cards_value = {'A': 14,'2': 2,'3': 3,'4': 4,'5': 5,'6': 6,'7': 7,'8': 8,'9': 9,'10': 10,'J': 11,'Q': 12,'K': 13, }cards_type = {u'散牌': 0,u'对子': 1,u'顺子': 2,u'同花': 3,u'同花顺': 4,u'炸弹': 5,}class Poker_Basic:def __init__(self, cards):self.card_color, self.card_value = self.get_color_value(cards)def get_color_value(self, cards):color = cards_color[cards[0:2]]value = cards_value[cards[2::]]return color, valuedef compare_color(self, cards):if cards.card_color > self.card_color:return -1elif cards.card_color == self.card_color:return 0else:return 1def compare_value(self, cards):if cards.card_value > self.card_value:return -1elif cards.card_value == self.card_value:return 0else:return 1def comp_cards(self, cards):passdef is_bomb(cards):if cards[0].card_value == cards[2].card_value:return Truereturn Falsedef is_flush(cards):if cards[0].card_color == cards[2].card_color and \cards[1].card_color == cards[2].card_color:return Truereturn Falsedef is_straight(cards):if cards[0].card_value == cards[1].card_value - 1 and \cards[1].card_value == cards[2].card_value - 1:return Truereturn Falsedef is_flush_staright(cards):if isA23(cards):return Trueif is_flush(cards) and is_straight(cards):return Truereturn Falsedef is_pair(cards):if cards[0].card_value != cards[2].card_value:if cards[0].card_value == cards[1].card_value:return Trueif cards[1].card_value == cards[2].card_value:return Truereturn Falsedef isA23(cards):if cards[0].card_value == 2 and cards[1].card_value == 3 \and cards[2].card_value == 14:return Truereturn Falsedef is235(cards):if cards[0].card_value == 2 and cards[1].card_value == 3 \and cards[2].card_value == 5:return Truereturn Falsedef get_cards_type(cards):card_list = cards.split(',')poker_list = []for data in card_list:poker_list.append(Poker_Basic(data))cmpfun = operator.attrgetter('card_value')poker_list.sort(key=cmpfun)cardtype = cards_type[u'散牌']if poker_list:if is_bomb(poker_list):return cards_type[u'炸弹']if is_flush_staright(poker_list):return cards_type[u'同花顺']if is_flush(poker_list):return cards_type[u'同花']if is_straight(poker_list):return cards_type[u'顺子']if is_pair(poker_list):return cards_type[u'对子']return cardtype"""def cmp_cards(my_Cards, next_Cards):my_CardsType = get_cards_type(my_Cards)next_CardsType = get_cards_type(next_Cards)if my_CardsType == next_CardsType:winOrLose = cardsTypeSame(my_Cards, next_Cards, my_Cards_Type)if my_CardsType != next_CardsType:winOrLose = cardsTypeDif(my_Cards, next_Cards, my_Cards_Type,next_CardsType)return winOrLose"""def cardsTypeSame(my_Cards, next_Cards, my_Cards_Type):# 豹子:比较单张牌牌值if my_Cards_Type == cards_type[u'炸弹']:if my_Cards[0].card_value - next_Cards[1].card_value == 0:return 0if my_Cards[0].card_value - next_Cards[1].card_value > 0:return 1else:return -1# 同花顺:比较第三张牌,同时考虑A23特殊顺子情况if my_Cards_Type == cards_type[u'同花顺']:if isA23(my_Cards) and isA23(next_Cards):return 0if isA23(my_Cards) or isA23(next_Cards):if isA23(my_Cards):return -1else:return 1if not isA23(my_Cards) and not isA23(next_Cards):if my_Cards[2].card_value - next_Cards[2].card_value == 0:return 0elif my_Cards[2].card_value - next_Cards[2].card_value > 0:return 1else:return -1# 同花if my_Cards_Type == cards_type[u'同花']:if my_Cards[2].card_value - next_Cards[2].value > 0:return 1elif my_Cards[2].card_value - next_Cards[2].value < 0:return -1else:if my_Cards[1].card_value - next_Cards[1].value > 0:return 1elif my_Cards[1].card_value - next_Cards[1].value < 0:return -1else:if my_Cards[0].card_value - next_Cards[0].value > 0:return 1elif my_Cards[0].card_value - next_Cards[0].value < 0:return -1else:return 0# 顺子if my_Cards_Type == cards_type[u'顺子']:if isA23(my_Cards) and isA23(next_Cards):return 0if isA23(my_Cards) or isA23(next_Cards):if isA23(my_Cards):return -1return 1return my_Cards[2].compare_value(next_Cards[2])# 对子if my_Cards_Type == cards_type[u'对子']:if my_Cards[1].compare_value(next_Cards[1]) != 0:return my_Cards[1].compare_value(next_Cards[1])else:if my_Cards[0].card_value + my_Cards[2].card_value > \next_Cards[0].card_value + next_Cards[2].card_value:return 1elif my_Cards[0].card_value + my_Cards[2].card_value == \next_Cards[0].card_value + next_Cards[2].card_value:return 0else:return -1# 单个if my_Cards_Type == cards_type[u'散牌']:if my_Cards[2].card_value - next_Cards[2].value > 0:return 1elif my_Cards[2].card_value - next_Cards[2].value < 0:return -1else:if my_Cards[1].card_value - next_Cards[1].value > 0:return 1elif my_Cards[1].card_value - next_Cards[1].value < 0:return -1else:if my_Cards[0].card_value - next_Cards[0].value > 0:return 1elif my_Cards[0].card_value - next_Cards[0].value < 0:return -1else:return 0def cardsTypeDif(my_Cards, next_Cards, my_Cards_Type, next_CardsType):if my_Cards_Type > next_CardsType:return 1else:return -1def cmp_cards(my_Cards, next_Cards):my_CardsType = get_cards_type(my_Cards)next_CardsType = get_cards_type(next_Cards)if my_CardsType == next_CardsType:winOrLose = cardsTypeSame(my_Cards, next_Cards, my_CardsType)if my_CardsType != next_CardsType:winOrLose = cardsTypeDif(my_Cards, next_Cards, my_CardsType, next_CardsType)return winOrLoseif __name__ == '__main__':get_cards_type(u'黑桃3,黑桃J,黑桃7')

 

 

测试:

# -*- coding: UTF-8 -*- from poker_basic import * import unittestclass Poker_BasicTest(unittest.TestCase):def test_value(self):cardsA = Poker_Basic(u'黑桃A')cardsB = Poker_Basic(u'黑桃5')self.assertEqual(1, cardsA.compare_value(cardsB))def test_color(self):cardsA = Poker_Basic(u'黑桃A')cardsB = Poker_Basic(u'黑桃5')self.assertEqual(0, cardsA.compare_color(cardsB))def test_three(self):#cardsA3=Poker_Basic(u'黑桃A,黑桃4,黑桃9')self.assertEqual(cards_type[u'同花'],get_cards_type(u'黑桃A,黑桃4,黑桃9') )if __name__ == '__main__':unittest.main()

总结

以上是生活随笔为你收集整理的poker游戏编码规则的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。