Hello, everyone. I'm your good friend, Miss Wang~
As we all know, poker is a necessary prop for home travel and desktop friends. Today we use it Python To achieve a similar fried Golden Flower poker game, let's take a look at the basic rules of the game.
Fried golden flower, also known as three cards, is a folk multiplayer card game widely spread all over the country. The game uses a pair of playing cards excluding the king and the king, with a total of 4 colors and 52 cards. Each player draws 3 cards from them to compare the size. The size order of various card types is as follows (the smaller the probability of occurrence in the full arrangement and combination, the greater the score reward of card types): 1. Flush: three cards of the same color and continuous points, such as red heart 2, red heart 3 and red heart 4; 2. Leopard: three cards with the same points, such as AAA and 222; 3. Shunzi: three consecutive cards, such as hearts 2, spades 3 and diamonds 4; 4. Golden Flower: three cards of the same color, such as red heart 2, red heart 5 and red heart 8; 5. Pair: two cards with the same points, such as heart 2 and spade 2; 6. Leaflet: 2 ~ 10 < J < Q < K < a. The following probability is taken from Baidu Encyclopedia:Note: the game rules described in this paper are different from the actual situation, and are mainly designed based on the comparison of different card types
1, Game flow implementation
1. Prepare playing cards
Before starting the game, you need to form A pair of playing cards that meet the requirements. As card friends know, playing cards have the following four colors, each of which has 13 cards such as A, 2 ~ 10, J, Q and K.
suit = ["spade", "Hearts", "block", "Plum blossom"] num = [str(i) for i in range(2, 11)] + ["J", "Q", "K", "A"]
In order to facilitate the subsequent scoring, each single sheet shall be given corresponding points.
score_map = {} # Single point mapping table for s in suit: count = 2 for n in num: score_map[f"{s}{n}"] = count count += 1
The preview of playing card points is as follows:
score_map = {spades 2 ': 2,' spades 3 ': 3,' spades 4 ': 4,' spades 5 ': 5,' spades 6 ': 6,' spades 7 ': 7,' spades 8 ': 8,' spades 9 ': 9,' spades 10 ': 10,' spades J': 11,' spades Q': 12,' spades K': 13,' spades A': 14,' hearts 2 ': 2,...}2. Player admission
To distinguish players by p1, p2 and other names, we first invite 5 players to enter.
players = [f"p{i}" for i in range(1, 6)]
3. Licensing
Pass the player and poker list as parameters to the dealer. The dealer draws three cards at random for each player without putting them back, and records the player's name and its corresponding card group.
def get_pk_lst(pls, pks): result = [] for p in pls: pk = sample(pks, 3) for _pk in pk: pks.remove(_pk) result.append({"name": p, "poker": pk}) return result pokers = list(score_map.keys()) # Remove a poker of the king and the king poker_grp = get_pk_lst(players, pokers) # Licensing
The licensing preview is as follows:
Result = [{name ':' P1 ',' Poker ': [' box 5 ',' plum blossom 3 ',' box A ']}, {name': 'P2', 'Poker': ['spade 4', 'box 8', 'spade J']}, {name ':' P3 ',' Poker ': [' heart 10 ',' Heart K ',' box 7 ']}, {name': 'box 4', 'plum blossom 6', 'box J']}, {name ':' P5 ',' Poker ': [' heart 5 ',' plum blossom 10 ',' spade A ']}]4. Judge card type and score
Press the previous mapping dictionary before calculating the score pk_lst The three playing cards in the card are converted into corresponding points.
n_lst = list(map(lambda x: score_map[x], pk_lst)) # Point mapping
Next, the text of the decor part is intercepted, and the set is used to determine whether it is three same flowers after de duplication.
same_suit = len(set([pk[:2] for pk in pk_lst])) == 1 # Same design and color
Then sort the number of points and compare it with the order list generated by the maximum value of points to judge whether it is continuous points. It should be noted that A23 is regarded as shunzi like QKA.
continuity = sorted(n_lst) == [i for i in range(min(n_lst), max(n_lst) + 1)] or set(n_lst) == {14, 2, 3} # Continuous
Don't forget to consider the inspection methods for leopards and leopards.
check = len(set(n_lst)) # Repetition
Then officially start judging the card type and counting points! The first is the leaflet, which is not the same flower, non shunzi and different points. The score is added by 3 single points.
if not same_suit and not continuity and check == 3: return sum(n_lst), "Leaflet"
The second is the pair, not the same flower, with and only two points consistent. In the score, the part constituting the pair will be rewarded twice.
if not same_suit and check == 2: w = [i for i in n_lst if n_lst.count(i) == 2][0] single = [i for i in n_lst if i != w][0] return w*2*2 + single, "Pair"
Golden flower, that is, the same flower instead of shunzi, will be rewarded 9 times.
if same_suit and not continuity: return sum(n_lst)*9, "Golden Flower"
Shunzi, i.e. continuous points rather than the same flower, will be rewarded 81 times.
if continuity and not same_suit: return sum(n_lst)*81, "Shunzi"
The leopard, that is, the three points are the same, you can't brush 666.
if check == 1: return sum(n_lst)*666, "leopard"
Flush with flowers, same color and continuous points. It's amazing. Gambler's skill will cause 999 damage.
if continuity and same_suit: return sum(n_lst)*999, "Flush"
5. Decide the outcome
A group of players, draw cards, score calculation and card type records are as follows:
pk_ GRP = [{name ':'p1', 'Poker': ['box 5', 'plum blossom 3', 'box A'], 'score': 22, 'type': 'single'}, {name ':'p2', 'Poker': ['spade 4', 'box 8', 'spade J'], 'score': 23, 'type': 'single'}, {name ':'p3', 'Poker': ['red heart 10', 'Red Heart K', 'box 7'], 'score': 30, 'type': 'single'}, {name ':'p4', 'Poker': ['box 4', 'plum blossom 6', 'J'], 'score': 21, 'type': 'single'}, {name ':' P5 ',' Poker ': [' heart 5 ',' plum blossom 10 ',' spade a '],' score ': 29,' type ':' single '}]Use the max function to find out who is the best and publish the name!
The winner is ------ p3best = max(pk_grp, key=lambda x: x["score"])["name"]
Well, you can start the next happy game again~
2, Statistics and source code
1. Card type statistics
100000 games were played and the frequency statistics of various card types were carried out. It can be seen that the probability calculated by the above arrangement and combination is basically consistent.
Counter({'Leaflet': 371856, 'Pair': 84773, 'Golden Flower': 24833, 'Shunzi': 16239, 'leopard': 1179, 'Flush': 1120}) Single frequency: 74.37% Pair frequency: 16.95% Jinhua frequency: 4.97% CIS frequency: 3.25% Leopard frequency: 0.24% Flush frequency: 0.22%
2. Card game case
The situation and results of various card types are as follows:
Opening result------ {'name': 'p1', 'poker': ['Block 5', 'Plum blossom 3', 'block A'], 'score': 22, 'type': 'Leaflet'} {'name': 'p2', 'poker': ['Spades 4', 'Block 8', 'spade J'], 'score': 23, 'type': 'Leaflet'} {'name': 'p3', 'poker': ['Red heart 10', 'Hearts K', 'Block 7'], 'score': 30, 'type': 'Leaflet'} {'name': 'p4', 'poker': ['Block 4', 'Plum blossom 6', 'block J'], 'score': 21, 'type': 'Leaflet'} {'name': 'p5', 'poker': ['Red heart 5', 'Plum blossom 10', 'spade A'], 'score': 29, 'type': 'Leaflet'} The winner is------ p3 Opening result------ {'name': 'p1', 'poker': ['block Q', 'Spade 5', 'spade K'], 'score': 30, 'type': 'Leaflet'} {'name': 'p2', 'poker': ['Spade 2', 'Block 2', 'Red heart 10'], 'score': 18, 'type': 'Pair'} {'name': 'p3', 'poker': ['Plum blossom 2', 'Spades 4', 'Plum blossom J'], 'score': 17, 'type': 'Leaflet'} {'name': 'p4', 'poker': ['Hearts K', 'Plum blossom 7', 'Red heart 6'], 'score': 26, 'type': 'Leaflet'} {'name': 'p5', 'poker': ['block A', 'Block 6', 'Red heart 4'], 'score': 24, 'type': 'Leaflet'} The winner is------ p1 Opening result------ {'name': 'p1', 'poker': ['spade J', 'Spade 5', 'Spades 4'], 'score': 180, 'type': 'Golden Flower'} {'name': 'p2', 'poker': ['Plum blossom 7', 'Red heart 4', 'Plum blossom 5'], 'score': 16, 'type': 'Leaflet'} {'name': 'p3', 'poker': ['Block 5', 'Spade 9', 'Plum blossom 10'], 'score': 24, 'type': 'Leaflet'} {'name': 'p4', 'poker': ['spade Q', 'Plum blossom 9', 'Spades 10'], 'score': 31, 'type': 'Leaflet'} {'name': 'p5', 'poker': ['Red heart 9', 'Block 9', 'Hearts A'], 'score': 50, 'type': 'Pair'} The winner is------ p1 Opening result------ {'name': 'p1', 'poker': ['Block 8', 'Spades 10', 'Block 9'], 'score': 2187, 'type': 'Shunzi'} {'name': 'p2', 'poker': ['Plum blossom 9', 'Hearts Q', 'Spades 3'], 'score': 24, 'type': 'Leaflet'} {'name': 'p3', 'poker': ['block A', 'Plum blossom K', 'Spades 4'], 'score': 31, 'type': 'Leaflet'} {'name': 'p4', 'poker': ['block J', 'Hearts J', 'Red heart 6'], 'score': 50, 'type': 'Pair'} {'name': 'p5', 'poker': ['Plum blossom 5', 'spade K', 'Block 3'], 'score': 21, 'type': 'Leaflet'} The winner is------ p1 Opening result------ {'name': 'p1', 'poker': ['spade Q', 'Spade 8', 'Plum blossom 6'], 'score': 26, 'type': 'Leaflet'} {'name': 'p2', 'poker': ['Red heart 3', 'Plum blossom 3', 'Spades 3'], 'score': 5994, 'type': 'leopard'} {'name': 'p3', 'poker': ['Hearts A', 'Red heart 6', 'Block 5'], 'score': 25, 'type': 'Leaflet'} {'name': 'p4', 'poker': ['Spades 4', 'Plum blossom A', 'Block 2'], 'score': 20, 'type': 'Leaflet'} {'name': 'p5', 'poker': ['Plum blossom 7', 'Spade 6', 'Plum blossom 8'], 'score': 1701, 'type': 'Shunzi'} The winner is------ p2 Opening result------ {'name': 'p1', 'poker': ['Spade 5', 'Plum blossom 9', 'Block 9'], 'score': 41, 'type': 'Pair'} {'name': 'p2', 'poker': ['spade Q', 'Spade 2', 'Hearts Q'], 'score': 50, 'type': 'Pair'} {'name': 'p3', 'poker': ['Red heart 2', 'Spade 7', 'Red heart 5'], 'score': 14, 'type': 'Leaflet'} {'name': 'p4', 'poker': ['Plum blossom 3', 'Block 10', 'spade A'], 'score': 27, 'type': 'Leaflet'} {'name': 'p5', 'poker': ['Spade 9', 'spade J', 'Spades 10'], 'score': 29970, 'type': 'Flush'} The winner is------ p5
3. Complete code
# @Seon # Fried Golden Flower from random import sample from collections import Counter def get_pk_lst(pls, pks): # Licensing result = [] for p in pls: pk = sample(pks, 3) for _pk in pk: pks.remove(_pk) result.append({"name": p, "poker": pk}) return result def calculate(_score_map, pk_lst): # Return score and card type n_lst = list(map(lambda x: _score_map[x], pk_lst)) # Point mapping same_suit = len(set([pk[:2] for pk in pk_lst])) == 1 # Same design and color continuity = sorted(n_lst) == [i for i in range(min(n_lst), max(n_lst) + 1)] or set(n_lst) == {14, 2, 3} # Continuous check = len(set(n_lst)) # Repetition if not same_suit and not continuity and check == 3: return sum(n_lst), "Leaflet" if not same_suit and check == 2: w = [i for i in n_lst if n_lst.count(i) == 2][0] single = [i for i in n_lst if i != w][0] return w*2*2 + single, "Pair" if same_suit and not continuity: return sum(n_lst)*9, "Golden Flower" if continuity and not same_suit: return sum(n_lst)*81, "Shunzi" if check == 1: return sum(n_lst)*666, "leopard" if continuity and same_suit: return sum(n_lst)*999, "Flush" def compare(_score_map, pk_grp): # Specific size for p in pk_grp: p["score"], p["type"] = calculate(_score_map, p["poker"]) print("Opening result------") for p in pk_grp: print(p) print("The winner is------") best = max(pk_grp, key=lambda x: x["score"])["name"] print(best) return pk_grp def show(_score_map, _players): # opening pokers = list(_score_map.keys()) poker_grp = get_pk_lst(_players, pokers) return compare(_score_map, poker_grp) def start_game(_score_map, _players, freq=1): # Games and statistics type_lst = [] for i in range(freq): grp = show(_score_map, _players) type_lst = type_lst + [t["type"] for t in grp] c = Counter(type_lst) print(c) total = sum(c.values()) for item in c.items(): print(f"{item[0]}Frequency:{item[1]/total:.2%}") if __name__ == '__main__': # Prepare playing cards suit = ["spade", "Hearts", "block", "Plum blossom"] num = [str(i) for i in range(2, 11)] + ["J", "Q", "K", "A"] score_map = {} # Single point mapping table for s in suit: count = 2 for n in num: score_map[f"{s}{n}"] = count count += 1 # 5 players enter players = [f"p{i}" for i in range(1, 6)] # Start the game start_game(score_map, players, freq=100000)
It's over here. The above is the python poker game written by brother long for you for three nights. I hope you can give a key three times! You can also say in the comment area what other games you want to write in Python?