# The fried golden flower game developed by python is coming, and it's no longer boring~

Hello ~ Hello, I'm Cha Cha! Everyone should have started work today. Many students spend the new year on the card table. Ha ha, so today we will bring you a little game of golden flowers developed in Python, which can be learned and played. Interesting learning is right! Come and have a look! Let's first look at the basic rules of the game.

Fried golden flower, also known as three cards, is a folk multiplayer card game widely spread throughout the country. The game uses a pair of playing cards excluding the big and small kings, 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 cut 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 realization

## 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, and each color 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 follow-up calculation of points, each ， single ， shall be given corresponding points first.

```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:

To distinguish players by p1, p2 and other names, we first invite five players to enter.

```players = [f"p{i}" for i in range(1, 6)]
```

## 3. Licensing

The list of players and playing cards is passed into the dealer as a parameter. The dealer draws three cards randomly for each player without putting them back in the playing cards, 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. Judgment card type and score calculation

Before calculating the score, press the previous mapping dictionary to set PK_ The three playing cards in LST # 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 a continuous number of 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}  # Is it continuous`

Don't forget to consider the inspection methods of 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 has 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, which is not the same flower, with and only two consistent points. 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"
```

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

The records of a group of players, drawing cards, counting points and card types are as follows:

pk_ GRP = [{name ':' P1 ',' Poker ': [' box 5 ',' plum blossom 3 ',' box a '],' score ': 22,' type ':' sheet '}, {name': 'p2', 'poker': ['spade 4', 'box 8', 'spade J'], 'score': 23, 'type': 'sheet'}, {name ':' P3 ',' Poker ': [' heart 10 ',' Heart K ',' box 7 '],' score ': 30,' type ':' sheet '}, {'name': 'p4', 'poker': ['box 4', 'plum blossom 6', 'box J'], 'score': 21, 'type': 'sheet'}, {'name': 'p5', 'poker': ['heart 5', 'plum blossom 10', 'spade A'], 'score': 29, 'type': 'sheet'}]

Use the max function to find out who is the best and publish the name!

```best = max(pk_grp, key=lambda x: x["score"])["name"]
```

The winner is ------ p3

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%
Golden Flower 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': ['Spade 4', 'Block 8', 'spade J'], 'score': 23, 'type': 'Leaflet'}
{'name': 'p3', 'poker': ['Red heart 10', 'Hearts K', 'Box 7'], 'score': 30, 'type': 'Leaflet'}
{'name': 'p4', 'poker': ['Box 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', 'Spade 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': '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', 'Box 9', 'Hearts A'], 'score': 50, 'type': 'Pair'}
The winner is------
p1

Opening result------
{'name': 'p1', 'poker': ['Block 8', 'Spades 10', 'Box 9'], 'score': 2187, 'type': 'Shunzi'}
{'name': 'p2', 'poker': ['Plum blossom 9', 'Hearts Q', 'Spade 3'], 'score': 24, 'type': 'Leaflet'}
{'name': 'p3', 'poker': ['block A', 'Plum blossom K', 'Spade 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', 'Box 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': ['Spade 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', 'Box 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'}
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}  # Is it 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)```

All right! In the new year, I hope you can become better! Learn more and become a great God as soon as possible!

Keywords: Python network Network Protocol p2p

Added by Dilbert137 on Tue, 08 Feb 2022 17:28:17 +0200