流畅的Python纸牌:rank_value * len(suit_value)为什么要乘以4

用点数(rank_value )和花色(suits_values)两个因素对每张牌排序,每张牌都有不同的数值

return rank_value * len(suits_values) + suits_values[card.suit]

其中:

ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits_values = dict(zip(suits, [3, 1, 0, 2]))
* len(suits_values) 其实就是 *4

-------------------------------------------------------

每张牌计算如下:

Card(rank='2', suit='spades') 0 * 4 + 3 = 3
Card(rank='3', suit='spades') 1 * 4 + 3 = 7
Card(rank='4', suit='spades') 2 * 4 + 3 = 11
Card(rank='5', suit='spades') 3 * 4 + 3 = 15
Card(rank='6', suit='spades') 4 * 4 + 3 = 19
Card(rank='7', suit='spades') 5 * 4 + 3 = 23
Card(rank='8', suit='spades') 6 * 4 + 3 = 27
Card(rank='9', suit='spades') 7 * 4 + 3 = 31
Card(rank='10', suit='spades') 8 * 4 + 3 = 35
Card(rank='J', suit='spades') 9 * 4 + 3 = 39
Card(rank='Q', suit='spades') 10 * 4 + 3 = 43
Card(rank='K', suit='spades') 11 * 4 + 3 = 47
Card(rank='A', suit='spades') 12 * 4 + 3 = 51
Card(rank='2', suit='diamonds') 0 * 4 + 1 = 1
Card(rank='3', suit='diamonds') 1 * 4 + 1 = 5
Card(rank='4', suit='diamonds') 2 * 4 + 1 = 9
Card(rank='5', suit='diamonds') 3 * 4 + 1 = 13
Card(rank='6', suit='diamonds') 4 * 4 + 1 = 17
Card(rank='7', suit='diamonds') 5 * 4 + 1 = 21
Card(rank='8', suit='diamonds') 6 * 4 + 1 = 25
Card(rank='9', suit='diamonds') 7 * 4 + 1 = 29
Card(rank='10', suit='diamonds') 8 * 4 + 1 = 33
Card(rank='J', suit='diamonds') 9 * 4 + 1 = 37
Card(rank='Q', suit='diamonds') 10 * 4 + 1 = 41
Card(rank='K', suit='diamonds') 11 * 4 + 1 = 45
Card(rank='A', suit='diamonds') 12 * 4 + 1 = 49
Card(rank='2', suit='clubs') 0 * 4 + 0 = 0
Card(rank='3', suit='clubs') 1 * 4 + 0 = 4
Card(rank='4', suit='clubs') 2 * 4 + 0 = 8
Card(rank='5', suit='clubs') 3 * 4 + 0 = 12
Card(rank='6', suit='clubs') 4 * 4 + 0 = 16
Card(rank='7', suit='clubs') 5 * 4 + 0 = 20
Card(rank='8', suit='clubs') 6 * 4 + 0 = 24
Card(rank='9', suit='clubs') 7 * 4 + 0 = 28
Card(rank='10', suit='clubs') 8 * 4 + 0 = 32
Card(rank='J', suit='clubs') 9 * 4 + 0 = 36
Card(rank='Q', suit='clubs') 10 * 4 + 0 = 40
Card(rank='K', suit='clubs') 11 * 4 + 0 = 44
Card(rank='A', suit='clubs') 12 * 4 + 0 = 48
Card(rank='2', suit='hearts') 0 * 4 + 2 = 2
Card(rank='3', suit='hearts') 1 * 4 + 2 = 6
Card(rank='4', suit='hearts') 2 * 4 + 2 = 10
Card(rank='5', suit='hearts') 3 * 4 + 2 = 14
Card(rank='6', suit='hearts') 4 * 4 + 2 = 18
Card(rank='7', suit='hearts') 5 * 4 + 2 = 22
Card(rank='8', suit='hearts') 6 * 4 + 2 = 26
Card(rank='9', suit='hearts') 7 * 4 + 2 = 30
Card(rank='10', suit='hearts') 8 * 4 + 2 = 34
Card(rank='J', suit='hearts') 9 * 4 + 2 = 38
Card(rank='Q', suit='hearts') 10 * 4 + 2 = 42
Card(rank='K', suit='hearts') 11 * 4 + 2 = 46
Card(rank='A', suit='hearts') 12 * 4 + 2 = 50

-----------------------------------------------------------------------

如果没有 * len(suits_values)  ,结果如下:(计算完毕后有4个3,4个4,4个5.......)

Card(rank='2', suit='spades') 0 + 3 = 3
Card(rank='3', suit='spades') 1 + 3 = 4
Card(rank='4', suit='spades') 2 + 3 = 5
Card(rank='5', suit='spades') 3 + 3 = 6
Card(rank='6', suit='spades') 4 + 3 = 7
Card(rank='7', suit='spades') 5 + 3 = 8
Card(rank='8', suit='spades') 6 + 3 = 9
Card(rank='9', suit='spades') 7 + 3 = 10
Card(rank='10', suit='spades') 8 + 3 = 11
Card(rank='J', suit='spades') 9 + 3 = 12
Card(rank='Q', suit='spades') 10 + 3 = 13
Card(rank='K', suit='spades') 11 + 3 = 14
Card(rank='A', suit='spades') 12 + 3 = 15
Card(rank='2', suit='diamonds') 0 + 1 = 1
Card(rank='3', suit='diamonds') 1 + 1 = 2
Card(rank='4', suit='diamonds') 2 + 1 = 3
Card(rank='5', suit='diamonds') 3 + 1 = 4
Card(rank='6', suit='diamonds') 4 + 1 = 5
Card(rank='7', suit='diamonds') 5 + 1 = 6
Card(rank='8', suit='diamonds') 6 + 1 = 7
Card(rank='9', suit='diamonds') 7 + 1 = 8
Card(rank='10', suit='diamonds') 8 + 1 = 9
Card(rank='J', suit='diamonds') 9 + 1 = 10
Card(rank='Q', suit='diamonds') 10 + 1 = 11
Card(rank='K', suit='diamonds') 11 + 1 = 12
Card(rank='A', suit='diamonds') 12 + 1 = 13
Card(rank='2', suit='clubs') 0 + 0 = 0
Card(rank='3', suit='clubs') 1 + 0 = 1
Card(rank='4', suit='clubs') 2 + 0 = 2
Card(rank='5', suit='clubs') 3 + 0 = 3
Card(rank='6', suit='clubs') 4 + 0 = 4
Card(rank='7', suit='clubs') 5 + 0 = 5
Card(rank='8', suit='clubs') 6 + 0 = 6
Card(rank='9', suit='clubs') 7 + 0 = 7
Card(rank='10', suit='clubs') 8 + 0 = 8
Card(rank='J', suit='clubs') 9 + 0 = 9
Card(rank='Q', suit='clubs') 10 + 0 = 10
Card(rank='K', suit='clubs') 11 + 0 = 11
Card(rank='A', suit='clubs') 12 + 0 = 12
Card(rank='2', suit='hearts') 0 + 2 = 2
Card(rank='3', suit='hearts') 1 + 2 = 3
Card(rank='4', suit='hearts') 2 + 2 = 4
Card(rank='5', suit='hearts') 3 + 2 = 5
Card(rank='6', suit='hearts') 4 + 2 = 6
Card(rank='7', suit='hearts') 5 + 2 = 7
Card(rank='8', suit='hearts') 6 + 2 = 8
Card(rank='9', suit='hearts') 7 + 2 = 9
Card(rank='10', suit='hearts') 8 + 2 = 10
Card(rank='J', suit='hearts') 9 + 2 = 11
Card(rank='Q', suit='hearts') 10 + 2 = 12
Card(rank='K', suit='hearts') 11 + 2 = 13
Card(rank='A', suit='hearts') 12 + 2 = 14

posted @ 2023-12-30 11:45  sangern  阅读(23)  评论(0编辑  收藏  举报