current position:Home>Python programming simulation poker game
Python programming simulation poker game
2022-01-30 19:44:57 【Tchunren】
Never in my wildest dreams , May day is so full . Get married when you are young , Number one, let's try on the clothes , Decorate the wedding room . Pick up on the morning of the second , In the afternoon, he Chengzi rode on my beloved little motorcycle that will never be in a traffic jam to take a bath , After dinner with everyone in the evening , Finally went home to sleep . No. 3 goes fishing with Wei Shao and his friends , Before fishing , I want to catch a six pound fish . After fishing, I found , I caught six . The roast fish we ate on the evening of the third , It was on this night , I found it simple J There are still people who can lose without cards in the game ,it's amazing! So I want to program and simulate this game .
On the morning of the fourth , In order to fulfill the wish of catching fish , Went fishing again , It's getting cold . But I'm brave , Caught the biggest fish in NANDA River . I made an appointment in the afternoon LMZ, Eating kebabs , Then I went to the basketball court to play basketball , Receive ZYQ Then go to my sister's place to pick nectarines . On the way back , The horizon is a bright sunset , With a beat , It's still beautiful . At this time, we are on the bridge , Under the bridge is the place for fishing during the day , The slanting shadow is a stay cable .LMZ Too much ink , His ink is a catch-up , Never surpassed . After you come back , Get ready for dinner , First, it's hot , Then we went to the dumpling restaurant . Steam with sweat after dinner , Then it was late . In the evening and LMZ Played for a while and ate chicken , When the game is over, I'll go to sleep , Come home at noon on the 5th , Sleeping in bed again . So may day is over .
Back to the point , On the evening of the 3rd, I wanted to program and simulate this game , But I simplified it ,52 I wanted to play cards with one-hot To express in form , Cards are played in expandable lengths 0-1 Vector representation , Later, after abstract processing , There is no need to call any third-party libraries , This simulation can be done directly using the list , I'll do the code first !
import time
def cards_init(): # Initialize the cards in the player's hand
source_cards = [ ['3',4],['4',4],['5',4],['6',4],['7',4],['8',4],['9',4],['10',4],['J',4],['Q',4],['K',4],['A',4],['2',4]] # Remove the playing cards of big and small kings
player_1 = [] # Before you catch a card palyer_1 Hand
player_2 = [] # Before you catch a card palyer_2 Hand
indexs = [ i for i in range(52) ] # Yes 52 Index cards
index_player_1 = [] # The player palyer_1 Hand index
index_player_2 = [] # The player palyer_1 Hand index
for i in range(26):
index = random.choice(indexs) # For players player_1 Randomly assign playing cards
index_player_1.append( index ) # For players player_1 Randomly assign playing cards
indexs.remove(index) # The card has been taken , Then delete this card from the bottom card
index = random.choice(indexs)
index_player_2.append(index)
indexs.remove(index)
for i in index_player_1: # Put the assigned card in your hand
player_1.append( source_cards[ i//4 ][0] )
for i in index_player_2:
player_2.append( source_cards[ i//4 ][0] )
return player_1, player_2
player_1, player_2 = cards_init() # Game initialization
print(player_1)
print(player_2)
lover = input(' Who do you want to win ?player1 or player2 or impartial: ') # Added a play Lai setting
if lover=='player1':
while 'J' in player_2:
player_2.remove('J')
if lover=='player2':
while 'J' in player_1:
player_1.remove('J')
print(player_1)
print(player_2)
land_cards = [] # Playing cards
is_end = False # A sign of whether the game is over
'''
push_card(players) It's the logic of the game , Using recursion . If the player's number of cards is zero , Then the game is over ; If the card you play does not exist in the deck and is not J, Then the next player plays cards ; If the card you play exists in the bottom card ,
Then all the cards between the card and the same card belong to yourself , Play again ; If the card is J, Then all the cards belong to yourself .
'''
def push_card(players):
if len(players)<5: # Check whether the player still has cards
print('game over')
is_end=True # Set the end flag of the game to True
return -1
temp_card = players[0] # Get the player's card information
if temp_card not in land_cards and temp_card != 'J': # The cards you play don't exist in the cards you play , And not J
print(' The player's hand is :', players)
print(' The bottom card is :', land_cards)
print(' Play cards for :', temp_card)
print(' You can't accept cards ')
print()
time.sleep(1)
land_cards.append(temp_card) # Add the player's card to the end of the bottom card
del players[0] # Delete the cards that have been played in the player's hand
return -2
elif temp_card in land_cards: # The card played exists in the bottom card
print(' The player's hand is :', players)
print(' The bottom card is :', land_cards)
print(' Play cards for :', temp_card)
print(' Close the card ')
time.sleep(1)
cut = land_cards[land_cards.index(temp_card):] # Intercept all cards between the cards in the bottom card that are the same as those played by the player
for i in range( land_cards.index(temp_card), len(land_cards) ): # Delete the intercepted card from the bottom card
del land_cards[-1]
players.extend(cut) # Add the intercepted cards to the end of the player's hand
players.append(temp_card) # Put the cards played by the player back to the end
del players[0] # Delete the card you just played
print(' After receiving the card, it will be :',players)
print()
push_card(players) # Recursively call
elif temp_card=='J': # If the player's card is J
print(' The player's hand is :', players)
print('J Come home ')
print(' The bottom card is :', land_cards)
print(' Play cards for :', temp_card)
time.sleep(1)
players.extend(land_cards) # Take back all your cards
players.append(temp_card)
for i in range(len(land_cards)): # Delete all cards in the card list
del land_cards[-1]
del players[0]
print(' After receiving the card, it will be :', players)
print()
push_card(players)
while not is_end: # Game cycle
push_card(player_1)
push_card(player_2)
#github The address is :https://github.com/t20134297/simple-poker-game
Copy code
Comments are made in the code , No more interpretation . Here's a little mention , In the logical decision function of playing and receiving cards , Using recursion , Otherwise, you can't do it . I wanted to use the game process tkinter Or other plug-ins to do a visualization , Too lazy , I didn't do , Have a look , Maybe one day on a whim .
copyright notice
author[Tchunren],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301944540030.html
The sidebar is recommended
- Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
- Turn audio into shareable video with Python and ffmpeg
- Using rbind in python (equivalent to R)
- Pandas: how to create an empty data frame with column names
- Talk about quantifying investment using Python
- Python, image restoration in opencv - CV2 inpaint
- Python notes (14): advanced technologies such as object-oriented programming
- Python notes (13): operations such as object-oriented programming
- Python notes (12): inheritance such as object-oriented programming
- Chapter 2: Fundamentals of python-5 Boolean
guess what you like
-
Python notes (11): encapsulation such as object-oriented programming
-
Python notes (10): concepts such as object-oriented programming
-
Gradient lifting method and its implementation in Python
-
Van * Python | simple crawling of a site course
-
Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
-
Nanny tutorial! Build VIM into an IDE (Python)
-
Fourier transform of Python OpenCV image processing, lesson 52
-
Introduction to python (III) network request and analysis
-
China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
-
Introduction to python (IV) dynamic web page analysis and capture
Random recommended
- Python practice - capture 58 rental information and store it in MySQL database
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
- This is what you should do to quickly create a list in Python
- On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
- Python OpenCV image area contour mark, which can be used to frame various small notes
- How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
- Initial Python tuple
- Introduction to Python urllib module
- Advanced Python Basics: from functions to advanced magic methods
- Python Foundation: data structure summary
- Python Basics: from variables to exception handling
- Python notes (22): time module and calendar module
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators