In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor will share with you how to use Python to compile a simple version of Texas hold'em Mini Game. I hope you will get something after reading this article. Let's discuss it together.
Texas hold'em briefly introduces what is Texas hold'em.
Texas hold'em does not know if you have played it, it is a kind of game card game originated in Texas in the United States, which is called Texas Hold'em Poker in English. The game is divided into regular table (Cash, cash bureau), single table game (SNG) and multi-table tournament (MTT). Although there are many kinds of poker, the basic poker rules are usually the same. It is a game that tests mentality and strategy.
Brief introduction to the rules of the game
1. Use props
A standard deck of playing cards removes 52 cards from the queen and queen to play the game.
Second, the number of people playing games
Generally 2-10 players, in individual cases there are 12 players.
Third, the purpose of the game
Win the chips of other players
IV. Purpose of betting
Players need to bet the same amount of chips to continue to watch or compare cards at the same time, and players with insufficient chips can watch to the end and participate in the game after all-in.
Fifth, issue a licence to place a bet
Licensing is generally divided into five steps, which are
Perflop-- first places large and small blind bets, and then issues two cards to each player. After the big blind bets, the first player chooses to follow, raise or cover the cards to give up. According to the clockwise direction, the other players declare their positions in turn, and the big blind players finally declare their positions. If the players have raised the bets, the players who have followed the bets before need to declare their positions again or even many times.
Flop-- deals three public cards at the same time, starting with a small blind bet (if a small blind bet is covered, starting with the nearest player, and so on). Players can choose to place bets, raise bets, or give up in clockwise order.
Turn-- deals the fourth card, starting with a small blind bet and expressing his position in a clockwise order.
River-- deals the fifth card, starting with a small blind bet and expressing its position clockwise in turn. Players can choose to place bets, raise bets, or seal cards to give up.
Playing cards-after the first four rounds of dealing and betting, the remaining players begin to show the size of the card, and the biggest player wins the bottom of the pool.
VI. The method of comparing cards
Combine your two cards with five public cards to choose five cards, no matter how many cards you use (or even without the cards in your hand) to form the largest card and compare it to other players.
The big card type is larger than the small card type, and the card type is generally divided into 10 types, from big to small:
Texas hold'em Game python implementation process Game initialization # call the sample function in the random library Used to randomly select cards from the stack of cards from random import sample# uses the list to store the colors and numbers of cards color = ['spades', 'hearts', 'clubs', 'diamonds'] number = ['2', 3', 4, 5, 6, 7, 8, 9, 9, 10, 10, 4, 4, 5, 6, 6, 9, 9, 10, and so on.
Import the sample function in the random library, which is then used to randomly extract cards from the card stack. At the same time, the color and number of cards are stored in color and number respectively by using the list.
# PokerGenerator function is used to generate a set of card cards def PokerGenerator (): # store the cards in the list Poker = [] # use a dictionary to represent the cards, add 52 blank cards to the Poker list for i in range (0Magazine 52): t = {'number': zero 'colors':'} Poker.append (t) # assign values to each card, each with 13 colors A total of four colors, 52 cards. For i in range (0552): Poker [I] .update ({'number': number [I], 'fancy': color [iAccord 13]}) # returns return Poker with a list of 52 cards stored in Poker
The function PokerGenerator is used to generate a new deck of playing cards. Texas hold'em uses a standard deck of playing cards, including four colors ('spades', 'hearts', 'clubs', 'diamonds'), each with 13 cards such as'2' to'A', with a total of 52 cards, excluding big and small kings.
# distribute cards def Pokerinitial (playersum) according to the number of players: Playerpoker = [] Poker = PokerGenerator () # initialize a new deck of cards num = 52 for i in range (0quot playersum): Pripoker = sample (Poker,2) # randomly select two cards from the card as a player's hand Playerpoker.append (Pripoker) # the cards selected are falsely added to the Playerpoker list as tuples Use the subscript serial number of Playerpoker to represent the player's serial number # to remove the cards drawn by each player from the stack. That is, to achieve non-return sampling for n in [0Magne1]: for m in range (0Magne1): if Poker [m] = = Pripoker [n]: del Poker [m] num-= 1 break Publicpoker = sample (Poker) 3) # three more cards are drawn from the stack as public cards # remove the drawn public cards from the stack for n in [0Jing 1jue 2]: for m in range (0 Num): if Poker [m] = = Publicpoker [n]: del Poker [m] num-= 1 break # display each player's hand and public card output for i in range (0 Playersum): print ("player% d"% (iTun1) + ": card 1:" + str (Playerpoker [I] [0]) + "card 2:" + str (Playerpoker [I] [1]) print (") print (" public card: "+ str (Publicpoker) +") return [Playerpoker,Poker,Publicpoker] # For the rest of the card stack, the public card group returns.
Distribute two hands to each player, distribute three public cards at the beginning of the game, and output each player's hand and public situation, using a dictionary to represent playing cards.
Select the winner
The most critical step to achieve Texas hold'em is to calculate which player has the largest hand, determine the final winner, and split the prize pool equally if there are multiple winners. I use 9 numbers such as 0 to 8 to represent the four levels from high card to flush. I do not specifically rank the royal flush. If there are two flush on the court at the same time, the winner will be determined according to the largest card number in the flush. Next, we will introduce the method of judging various hand categories.
# determine whether a player's cards and public cards constitute a shun son def judgestraight (finalpoker): result = [0,0] # used to store the results, result [0] stores the categories of the player's best five cards, from high cards to royal flush numbered 0 to 9. Result [1] stores the card with the largest number in the current open group pokernum = [] # stores the number of the card for i in range (0, len (finalpoker)): pokernum.append (number.index (finalpoker [I] ['number'])) pokernum.sort () # sorts the card numbers from small to large # to determine whether the combination of player cards and public cards can form a shun son maxp = minp = 0 card (1 Len (finalpoker): if (pokernum [I-1] + 1) = = pokernum [I]: maxp = I else: minp = I if (maxp-minp) = = 4: result [0] = 4 result [1] = max (pokernumminp: maxp + 1]) return result
The judgestraight function is used to determine whether the player's hand and five common cards can form a straight son, and the parameter finalpoker is required. Finalpoker shall accept a list object that contains the player's hand and the final public card, for a total of 7 cards. Then judge whether there is a straight son in it, and return the judgment result. The result contains two pieces of information: if result [0] is 4, it contains cis, and if it is 0, it does not. The largest card number in Shunzi is stored in result [1] if it is included.
# determine whether there is a homogenous def judgeflush (finalpoker) in the deck: result = [0,0] pokernum = [] flush = [] # used to store cards of the same design and color while len (finalpoker) > = 5: # pull out the cards of the same color in the deck flush.append (finalpoker [0]) for i in range (1 Len (finalpoker): if finalpoker [I] ['fancy'] = = finalpoker [0] ['fancy']: flush.append (finalpoker [I]) # if there are not less than five cards of the same pattern Then the card holder has the same flower if len (flush) > = 5: result [0] = 5 for ele in flush: pokernum.append (number.index (ele ['digital'])) result [1] = max (pokernum) for ele in flush: finalpoker.remove (ele) flush.clear () return result
The judgeflush function is used to determine whether the player's hand contains a flush and returns the result. As above, the result returned by the judgestraight function contains two information: whether the combination of the player's hand and public beat contains the same flower, and if so, the maximum number of the same flower will be returned at the same time.
# determine whether there are four, three, two pairs or one pair of def judgesame (finalpoker) in the card deck: result = [0,0] four =-1 # whether there are four three =-1 # in the card deck whether there are three two = [- 1 -1] # record whether there are two pairs and a pair of count = 1 pokernum = [] bottom = 0 # to store all card plates of the card deck in pokernum And sort for i in range (0, len (finalpoker)): pokernum.append (number.index (finalpoker [I] ['digital']) pokernum.sort () # to determine whether there are four, three, two pairs or one equivalent in the card deck. And the corresponding largest card number is stored in for i in range (1 Len (finalpoker): if pokernum [I] = = pokernum [bottom]: count + = 1 else: if count = = 2: if pokernum [bottom] > min (two): numid = two.index (min (two)) two [numid] = pokernum [bottom] if count = = 3: If pokernum [bottom] > three: three = pokernum [bottom] if count = = 4: four = pokernum [bottom] bottom = I count = 1 # determine the largest combination card type if four > = 0: result [0] = 7 result [1] = four elif three > = 0 and max (two) > = 0: result [0] = 6 t = three * 10 + max (two) result [1] = t elif three > = 0: result [0] = 3 result [1] = three elif min (two) > = 0: result [0] = 2 result [1] = max (two) elif max (two) > = 0: result [0] = 1 Result [1] = max (two) return result
This function is much longer than the above two functions, because it implements three functions to determine whether the combination of the player's hand and public cards contains a pair, two pairs, three or four. Similar to the function mentioned above, judgesame will determine whether the accepted card deck contains the above four cases, if so, return the case with the highest level, and return the largest card number contained in that case.
# calculate the largest combination card type def computeresult (Playerpoker, Publicpoker) in the player's card group: finalresult = [0 0] finalpoker = [] finalpoker = Playerpoker + Publicpoker a = finalpoker.copy () b = finalpoker.copy () c = finalpoker.copy () # store homogenous, straight, four, The judgment results of three items (that is, the number and situation of the same number) result_1 = judgeflush (a) result_2 = judgestraight (b) result_3 = judgesame (c) # judge whether it is a homoflush if result_1 [0]! = 0 and result_2 [0]! = 0: finalresult [0] = 8 finalresult [1] = result_2 [1] # if it is not a homoflush Then judge the maximum type of card deck composed of the player's hand card and public card: else: t cards 0 = result_1 [0] t cards 1 = result_1 [1] if result_2 [0] > t cards 0: t cards 0 = result_2 [0] t cards 1 = result_2 [1] if result_3 [0] > t cards 0: Tweak 0 = result_3 [0] tsp 1 = result_3 [1] # determine the final result That is, the largest type of cards that the player has: finalresult [0] = twee0 finalresult [1] = twee1 return finalresult
I finally use the computeresult function to calculate the highest level card deck that each player will eventually have. In this function, I call the above three functions respectively, get the highest level card deck owned by the player by comparison, and return the result.
Game theme function
The functions we have written have been able to initialize the game and calculate the results of the game, and then we use the above functions to write the real subject of Texas hold'em 's game.
# Game function body def gamestart (playersum): finalresult = [] # used to store each player's largest hand [playerpoker,Poker,Publicpoker] = Pokerinitial (playersum) # initialize a deck of cards according to the number of players, and distribute two hands to each player And distribute the initial three public cards playerlist = list (range (1) playersummers 1) # record the remaining players after each round playerlist_t = [] Playerpoker = [] while len (Publicpoker)
< 5 and len(playerlist) >1Rank # when the number of public cards is 5 or there is only one player left, the game ends with a settlement tag = 0 # identifier, indicating whether the entered player continues the game in error If there is an error, re-enter playerkeep = input ("Please enter the player to continue the game:") playerlist_t = eval (playerkeep) if isinstance (playerlist_t,int): if playerlist_t in playerlist: winplayer = playerlist_t print ("game over. The winner is "+ str (winplayer)) # if only one player chooses to continue the game, return playerlist.clear () else: print (" typed incorrectly) will be settled at the end of the game. Please re-enter ") # if the input player does not exist in this turn, report an error and re-enter elif isinstance (playerlist_t, tuple): playerlist_t = list (playerlist_t) for i in playerlist_t: if i not in playerlist: tag = 1 if tag = = 1: print (" incorrect input Please re-enter ") # if the entered player has a player who does not exist in this round, report an error and re-enter else: playerlist = playerlist_t pokerkeep = sample (Poker 1) # draw another card from the remaining card as the public card Publicpoker + = pokerkeep pokerkeep = pokerkeep [0] print ("Public card:" + str (Publicpoker) + "") # display the public card of this round for n in range (0 Len (Poker): # remove the extracted public cards from the remaining cards if Poker [n] = = pokerkeep: del Poker [n] break for i in playerlist: Playerpoker.append (Playerpoker [I-1]) for ppoker in Playerpoker: finalresult.append (computeresult (ppoker) Publicpoker) # substitute each player's hand and public cards to calculate the final result finalscore = [] finalscore_t = [] finalplayer = [] winner = [] for t in range (0, len (finalresult)): finalscore.append (finalresult[ t] [0]) maxscore = max (finalscore) # judge the largest combination card type of all players at this time # if multiple players have the largest combination card at the same time Then compare their largest card number with for t in range (0 Len (finalresult): if finalscore [t] = = maxscore: finalplayer.append (t) for t in finalplayer: finalscore_t.append (finalresult [t] [1]) maxscore_t = max (finalscore_t) for t in finalplayer: if finalresult [t] [1] = = maxscore_t: winner.append (player list [t]) # # output final player Print ("game over.The winner is:") for t in winner: print ("player:" + str (t)) return
The only parameter that the gamestart function needs to output is the number of players. At the beginning of the game, we use the Pokerinitial function to get a new deck of cards, distribute two hands to each player, and distribute three initial public cards. When the number of players is only 1 in the middle of the game, we think the game is over and show the final winner. If the game goes on normally to the end (two or more players hold on to the final round), the highest level card combination owned by each player is calculated and compared, and the final winner is obtained. If the player who enters to continue the game is not in the current player team, the system will report an error and prompt to re-enter. All right, without saying much, let's start to experience the game.
Game experience and presentation from Texas_Hold_em_Poker import gamestart
We first import the Texas hold'em game module we wrote, and only need the gamstart function.
N = input ('Please enter the number of players to play the game:') gamestart (int (n))
Then we get the number of players playing the game from the console by writing the input function.
We set the number of players participating in the game at 5.
Then each player's hand and public cards appear on the screen.
Then we enter the players who continue the game.
Then there was the public card for the next round, and we went on to let players 1, 2, and 3 continue the game.
You can see the public card in the final round and calculate the final winner. At this point, we try to enter the player number that did not continue to participate in the game in the last round and see what happens.
We still set the number of gamers at 5, and we still let players 1 and 2 continue the game in the first round.
But we enter players 4 and 5 who have dropped out of the game in the next round.
You can see that the system reported an error and prompted to re-enter it. At this point, we only need to enter the correct player number to get the correct result.
After reading this article, I believe you have a certain understanding of "how to use Python to write a simple version of Texas hold'em Mini Game". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.