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)

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

if lover=='player2':
    while 'J' in player_1:

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 ')
        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 ')
        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)
        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)
        players.extend(land_cards)    # Take back all your cards 
        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)

while not is_end:    # Game cycle 

#github The address is :
 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.

Random recommended