Euler Project 54

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:

Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD
Pair of Fives
 2C 3S 8S 8D TD
Pair of Eights
 Player 2
2 5D 8C 9S JS AC
Highest card Ace
 2C 5C 7D 8S QH
Highest card Queen
 Player 1
3 2D 9C AS AH AC
Three Aces
 3D 6D 7D TD QD
Flush with Diamonds
 Player 2
4 4D 6S 9H QH QC
Pair of Queens
Highest card Nine
 3D 6D 7H QD QS
Pair of Queens
Highest card Seven
 Player 1
5 2H 2D 4C 4D 4S
Full House
With Three Fours
 3C 3D 3S 9S 9D
Full House
with Three Threes
 Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?

简单来说,这个扑克牌的游戏规则是:

  1. 两个人玩,每人5张牌,牌牛逼的就赢
  2. 单张牌的大小依次为 2 3 4 5 6 7 8 9 T J Q K A (T 代表 10)

就这么简单

牌的大小是啥呢?从大到小依次为:

  1. 封尖同花顺
  2. 同花顺
  3. 炸蛋
  4. 三带一对
  5. 同花
  6. 顺子
  7. 三个
  8. 两对
  9. 一对
  10. 烂牌

下面代码来了

def is_sz(nums):
    '''顺子'''
    idx = [poker.index(ele) for ele in nums]
    sorted_idx = sorted(idx)
    poker_in_order = ''
    for idx_item in sorted_idx:
        poker_in_order += poker[idx_item] 
    if poker_in_order in poker:
        return True
    else:
        return False

def is_th(suits):
    '''同花'''
    if len(set(suits)) == 1:
        return True
    else:
        return False

def is_zd(nums):
    '''炸蛋'''
    for poker_item in poker:
        if (''.join(nums)).count(poker_item) == 4:
            return True
    return False

def is_sg(nums):
    '''三个'''
    for poker_item in poker:
        if (''.join(nums)).count(poker_item) == 3:
            return True
    return False

def n_dz(nums):
    '''对子'''
    count_ = 0
    for poker_item in poker:
        if (''.join(nums)).count(poker_item) == 2:
            count_ += 1
    return count_

def poker_level(nums, suits):
    '''牌的等级'''
    if is_sz(nums) and is_th(suits) and 'A' in nums:
        return 1
    elif is_sz(nums) and is_th(suits):
        return 2
    elif is_zd(nums):
        return 3
    elif is_sg(nums) and n_dz(nums) == 1:
        return 4
    elif is_th(suits):
        return 5
    elif is_sz(nums):
        return 6
    elif is_sg(nums):
        return 7
    elif n_dz(nums) == 2:
        return 8
    elif n_dz(nums) == 1:
        return 9
    else:
        return 10

def same_level_winner(nums1, nums2):
    '''等级相同时怎么办'''
    same_level = poker_level(nums1, suits1)
    if same_level in [1,2,5,6,10]:
        idx_1 = [poker.index(ele) for ele in nums1]
        idx_2 = [poker.index(ele) for ele in nums2]
        order_1 = sorted(idx_1, reverse = True)
        order_2 = sorted(idx_2, reverse = True)
    else:
        dict_1 = poker_dict(nums1)
        dict_2 = poker_dict(nums2)
        sorted_dict_1 = sorted(dict_1.items(), key = lambda x:x[1], reverse = True)
        sorted_dict_2 = sorted(dict_2.items(), key = lambda x:x[1], reverse = True)
        
        idx_1 = [poker.index(key) for key, value in sorted_dict_1]
        idx_2 = [poker.index(key) for key, value in sorted_dict_2]
        
        if same_level in [3,4]:
            order_1 = idx_1
            order_2 = idx_2
        elif same_level == 7:
            order_1 = [idx_1[0]] + sorted(idx_1[-2:], reverse = True)
            order_2 = [idx_2[0]] + sorted(idx_2[-2:], reverse = True)
            
        elif same_level == 8:
            order_1 = [max(idx_1[:2]), min(idx_1[:2]), idx_1[-1]]
            order_2 = [max(idx_2[:2]), min(idx_2[:2]), idx_2[-1]]
            
        else:
            order_1 = [idx_1[0]] + sorted(idx_1[1:], reverse = True)
            order_2 = [idx_2[0]] + sorted(idx_2[1:], reverse = True)
        
    for i in range(len(order_1)):
        if order_1[i] == order_2[i]:
            continue
        else:
            if order_1[i] > order_2[i]:
                return 1
            else:
                return 2
                    
def poker_dict(nums):
    dict_ = {}
    for ele in nums:
        if ele in dict_:
            dict_[ele] += 1
        else:
            dict_[ele] = 1
    return dict_

def winner(nums1, suits1, nums2, suits2):
    level_1 = poker_level(nums1, suits1)
    level_2 = poker_level(nums2, suits2)
    if level_1 == level_2:
        return same_level_winner(nums1, nums2)
    else:
        if level_1 < level_2:
            return 1
        else:
            return 2

if __name__ == '__main__':
    poker = '23456789TJQKA'    
    f = open('poker.txt', 'r')
    line = f.readline()
    win_count_1 = 0
    
    count = 0
    while line != '':
        count += 1
        line = line.replace('\n', '')
        line = line.replace(' ', '')
        nums_suits = list(line)
        nums1 = [nums_suits[i] for i in range(0,10,2)]
        suits1 = [nums_suits[i] for i in range(1,10,2)]
        nums2 = [nums_suits[i] for i in range(10,20,2)]
        suits2 = [nums_suits[i] for i in range(11,20,2)]
        
        which_win = winner(nums1, suits1, nums2, suits2)
        if which_win == 1:
            win_count_1 += 1
        line = f.readline()
    f.close()
    print(win_count_1)
结果玩家1 胜的盘数有376盘,^_^
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值