poker编码

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import operator

cards_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, value

def compare_color(self, cards):
if cards.card_color > self.card_color:
return -1
elif cards.card_color == self.card_color:
return 0
else:
return 1

def compare_value(self, cards):
if cards.card_value > self.card_value:
return -1
elif cards.card_value == self.card_value:
return 0
else:
return 1

def comp_cards(self, cards):
pass


def is_bomb(cards):
if cards[0].card_value == cards[2].card_value:
return True
return False


def is_flush(cards):
if cards[0].card_color == cards[2].card_color and \
cards[1].card_color == cards[2].card_color:
return True
return False


def is_straight(cards):
if cards[0].card_value == cards[1].card_value - 1 and \
cards[1].card_value == cards[2].card_value - 1:
return True
return False


def is_flush_staright(cards):
if isA23(cards):
return True
if is_flush(cards) and is_straight(cards):
return True
return False


def is_pair(cards):
if cards[0].card_value != cards[2].card_value:
if cards[0].card_value == cards[1].card_value:
return True
if cards[1].card_value == cards[2].card_value:
return True
return False


def isA23(cards):
if cards[0].card_value == 2 and cards[1].card_value == 3 \
and cards[2].card_value == 14:
return True
return False


def is235(cards):
if cards[0].card_value == 2 and cards[1].card_value == 3 \
and cards[2].card_value == 5:
return True
return False


def 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 0
if my_Cards[0].card_value - next_Cards[1].card_value > 0:
return 1
else:
return -1
# 同花顺:比较第三张牌,同时考虑A23特殊顺子情况
if my_Cards_Type == cards_type[u'同花顺']:
if isA23(my_Cards) and isA23(next_Cards):
return 0
if isA23(my_Cards) or isA23(next_Cards):
if isA23(my_Cards):
return -1
else:
return 1
if not isA23(my_Cards) and not isA23(next_Cards):
if my_Cards[2].card_value - next_Cards[2].card_value == 0:
return 0
elif my_Cards[2].card_value - next_Cards[2].card_value > 0:
return 1
else:
return -1
# 同花
if my_Cards_Type == cards_type[u'同花']:
if my_Cards[2].card_value - next_Cards[2].value > 0:
return 1
elif my_Cards[2].card_value - next_Cards[2].value < 0:
return -1
else:
if my_Cards[1].card_value - next_Cards[1].value > 0:
return 1
elif my_Cards[1].card_value - next_Cards[1].value < 0:
return -1
else:
if my_Cards[0].card_value - next_Cards[0].value > 0:
return 1
elif my_Cards[0].card_value - next_Cards[0].value < 0:
return -1
else:
return 0
# 顺子
if my_Cards_Type == cards_type[u'顺子']:
if isA23(my_Cards) and isA23(next_Cards):
return 0
if isA23(my_Cards) or isA23(next_Cards):
if isA23(my_Cards):
return -1
return 1
return 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 1
elif my_Cards[0].card_value + my_Cards[2].card_value == \
next_Cards[0].card_value + next_Cards[2].card_value:
return 0
else:
return -1
# 单个
if my_Cards_Type == cards_type[u'散牌']:
if my_Cards[2].card_value - next_Cards[2].value > 0:
return 1
elif my_Cards[2].card_value - next_Cards[2].value < 0:
return -1
else:
if my_Cards[1].card_value - next_Cards[1].value > 0:
return 1
elif my_Cards[1].card_value - next_Cards[1].value < 0:
return -1
else:
if my_Cards[0].card_value - next_Cards[0].value > 0:
return 1
elif my_Cards[0].card_value - next_Cards[0].value < 0:
return -1
else:
return 0


def cardsTypeDif(my_Cards, next_Cards, my_Cards_Type, next_CardsType):
if my_Cards_Type > next_CardsType:
return 1
else:
return -1


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_CardsType)
if my_CardsType != next_CardsType:
winOrLose = cardsTypeDif(my_Cards, next_Cards, my_CardsType, next_CardsType)
return winOrLose


if __name__ == '__main__':
get_cards_type(u'黑桃3,黑桃J,黑桃7')



# -*- coding: UTF-8 -*-
from poker_basic import *
import unittest


class 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()
posted @ 2020-08-04 02:00  summer_che  阅读(159)  评论(0编辑  收藏  举报