poker游戏编码规则
生活随笔
收集整理的這篇文章主要介紹了
poker游戏编码规则
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#!/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'對(duì)子': 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'對(duì)子']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# 同花順:比較第三張牌,同時(shí)考慮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])# 對(duì)子if my_Cards_Type == cards_type[u'對(duì)子']: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# 單個(gè)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')
?
?
測(cè)試:
# -*- 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()總結(jié)
以上是生活随笔為你收集整理的poker游戏编码规则的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 读书笔记:PHP和MySQL高性能应用开
- 下一篇: Css+Jquery实现点击图片放大缩小