Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the 21:00 Poker Game by Python

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Python how to achieve poker blackjack game, the article is very detailed, has a certain reference value, interested friends must read!

Code directly.

import randomsys #card list card_code = ['A',' 2','3',' 4','5',' 6','7',' 8','9',' 10','J',' Q','K']#suit list card_symbol = ['♦','', ' â','']#game initialization def init(player_count): #Generate player counters based on the number of players player_group = [[] for _ in range(player_count)] #Generate whether players want cards based on the number of players player_isWant = [True for _ in range(player_count)] #Generate a list of elements 1 - 52 (remove the big and small cards [52]) poker = list(range(1, 53)) #Shuffle lists with random's shuffle function random.shuffle(poker) #Back to Player Group Do players want to shuffle 52 cards return player_group, player_isWant, poker#Print player points def print_player_point(player_group): #Store player points player_point = [] #Go through every player for index in range(len(player_group)): #Print cards and points for each player print("------Player"+str(index+1)+"-----") #Initialize player points If you have card A, because A can be considered 1 or 11, there are two points current_player = [0, 0] #Go through each player's hand for card in player_group[index]: """ core code Since the number of cards is from 1 to 52, the card must be subtracted by 1 and then the remainder is the real subscript of the card list If the player draws 15 cards, i.e., 15 - 13 = 2 and the suit order is, i.e., 2 Card 15 - 1 = 14 and 14% 13 = 1 This is the second element of the corresponding card list, which is 2 suit 15 - 1 = 14 and then 14 / 13 = 1 corresponds to the second element of the suit list """ #Get face and suit subscript code_index = int((card - 1) % 13) symbol_index = int((card - 1) / 13) #Print player card information print(card_code[code_index] + card_symbol[symbol_index], end="\t") #Add different points 1 and 11 if the card contains an ace if (code_index + 1) == 1: current_player[0] += 1 current_player[1] += 11 #Add the same points if the card does not contain an ace else: current_player[0] += code_index + 1 current_player[1] += code_index + 1 #Print one point if two points agree if current_player[0] == current_player[1]: print("points are"+str(current_player[0])+"points") #Otherwise print two points else: print("points are"+str(current_player[0])+"points or"+str(current_player[1])) #Add current player points player_point.append(current_player) #Return all player points return player_point#Play def play_game(): #Print game rules print("------Blackjack game----") print("---A can be seen as 1 or 11---") #Endless loop keeps playing while True: #Initialize the number of players to 0 player_count = 0 #Continue asking when the number of players is less than or equal to 1 or greater than 5 while player_count 5: #Ask the number of players print("How many players? (2~5 digits)", end="") #Get console input number No validation input If input non-digital program directly error player_count = int(input()) #Initialize the game Return to player group Do players want to shuffle 52 cards player_group, player_isWant, poker = init(player_count) #Start dealing two cards for each player Number of players in the cycle for index in range(player_count): for i in range(2): The # pop() function removes an element from a list (the default last element) and returns the value of that element. player_group[index].append(poker.pop()) #Print player points and get current player points player_point = print_player_point(player_group) #Always ask if player wants cards as long as player wants cards and there are cards left while True in player_isWant and len(poker) > 0: #Traverse players for index in range(player_count): #Decide if the player might need more cards if player_isWant[index] is True: Ask players if they want cards print("player"+str(index+1)+", you want another one? (y/n)") #Get Console Input isWant = str(input())[0] #Mark player as no longer needed if character entered is "n" if isWant == "n": player_isWant[index] = False #If it is not the character "n", the default is to continue to ask for cards and deal a card to this player else: player_group[index].append(poker.pop()) #Print player points at the end of each round and get current player points player_point = print_player_point(player_group) print("\n"*5+"=== End of round ===") #Define a scorer score = [] #The end of the card goes through all the players 'points to determine which player wins for point_list in player_point: #If two points are the same, there is no A if point_list[0] == point_list[1]: #If the score is greater than 21, take a negative number less than or equal to 21 and take any one as a score score.append(-point_list[0] if point_list[0] > 21 else point_list[0]) #If the two points do not agree, the explanation contains A, then continue to judge else: #If the larger of the two points is less than or equal to 21 if max(point_list) 21 else min(point_list)) #Highest score max_point = max(score) #If the number of people with the highest score is 1, directly consider the player with the highest score to win and print the game result if score.count(max_point) == 1: print("Player"+str(score.index(max_point) + 1)+"Win! ") #Otherwise, the highest scores are tied and multiple people are considered winners else: #List of winning players temp_list = [] #Traversing scores for index in range(len(score)): #Score equals highest score recorded player if score[index] == max_point: temp_list.append("Player"+str(index+1)) #Match winning player list Print game results print("Congratulations"+",".join(temp_list)+"Win! ") Ask if you want to continue playing print("Do you want to continue playing? (y/n)") #Exit if console input is not character "y" if str(input())[0] != 'y': sys.exit()#program main entry if __name__= '__main__': #Play games play_game()

The results are as follows

That's all for Python's How to Make Poker Blackjack Work. Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report