Dealer's card :♣ 10 ♦ Q Player's card :♦ 4 ♥ 8 Do you want to continue to take cards (y/n, Default to y):y The card the player gets is :♦ 4 ♥ 8 ♥ K Blow it up Player loses !
<>21 An overview of point Poker

21 Dot is also known as black jack (Blackjack)
, It's a popular poker game . The game is played by two to six people , Use the 52 Card , The player's goal is to keep the sum of the cards in his hand within the limit 21 Be small and as large as possible .

The rules for calculating the number of points in a hand are as follows :

* 2 to 9 brand , According to the original number of points ;
* 10,J,Q,K All the cards count 10 spot ( Generally recorded as T, Namely Ten) ;
* A brand (Ace) It can be regarded as 1 Point can also be counted as 11 spot , It's up to the player
* ( When players stop , The number of points will be regarded as the largest and try not to explode , as A+K by 21,A+5+8 by 14 instead of 24).
<>21 Design ideas of point playing card game

Simulate according to the following rules 21 Order a card game :

* Computer artificial intelligence AI As a banker (House), Users as players (Player) .
* At the beginning of the game , The dealer deals from a shuffled deck : The first 1 Card to player , The first 2 Card to dealer , The first 3 Card to player , The first 4 Card to dealer .
* then , Ask the player if they need to continue “ win the medal ”, Pass one or more times “ win the medal ”, The player tries to make the number of points of playing card close to the number of points 21. If the total number of points in the player's hand exceeds 21, The player loses .
* When players decide “ Suspension of trading ”( Namely , No more “ win the medal ”), It's the banker's turn to use the following rules (“ Rules of the maker ”)“ win the medal ”
: If the sum of the best points in the hands of the dealer is less than 17, You have to “ win the medal ”:, If the sum of the points is greater than or equal to 17, be “ Suspension of trading ”. If the sum of the dealer's points exceeds 21, The player wins .
* last ,
Compare player and dealer points . If the player has a large number of points , You win . If the player's points are small , You lose . If the points are the same , It's a draw . But the player and the dealer's card value is the same 21 spot , Have at this time blackjack
( a sheet Ace And a card with the number of points 10 My card ) Side wins .
The procedure is as follows :

* Initialization one The deck is washed , Initialize the cards in the hands of the dealer and player to be empty
Call function get_shuffled_deck() def get_shuffled_deck():
""" Initialization includes 52 A list of cards , And mixed row back , A washed deck of playing cards """ # Design and color suits And serial number suits = {'♣', '♠', '♦', '♥'}
ranks= {'2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'} deck
= [] # Create a pair 52 Playing cards for suit in suits: for rank in ranks: deck.append(suit + ' '
+ rank) random.shuffle(deck) return deck
* Give the player and the dealer two cards in turn
Call function deal_card() def deal_card(deck, participant): """ Give a card to the participant participant"""
card= deck.pop() participant.append(card) return card
* Players take cards : Ask the player whether to continue to take the card , If it is , Continue to deal players ( Call function deal_ card()) , And calculate the player's card compute_total()
, If greater than 21 spot , output “ Player loses !” information , And back . def compute_total(hand): """ Calculates and returns the sum of points in a hand """ values = {
'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, \ '9':9, '10':10, 'J':10, 'Q':
10, 'K':10, 'A':11} result = 0 # The sum of initialization points is 0 numAces = 0 #A The number of # Calculate the sum of points A The number of for card
in hand: result += values[card[2:]] if card[2] == 'A': numAces += 1
# If the number of points and >21, Try to A treat as 1 To calculate # ( That is, subtracting 10), Multiple A Subtract cycle 10, Up to the count <=21 while result > 21 and numAces
> 0: result -= 10 numAces -= 1 return result
*
The dealer takes the card : Banker ( Computer artificial intelligence AI) Press “ Rules of the maker ” Make sure you take the card , If it is , Continue to deal with the dealer ( Call function deal_card()) , And calculate the dealer card
compute_total(), If greater than 21 spot , output “ Players win cards !” information , And back .

*
Calculate the points of the dealer and the player respectively , Compare the number of points , Output the winning and losing result information .

Semantization :

* shuffled deck, Shuffle table
* deal, Licensing
random Function usage of module

* random.shuffle(lst) Sort all elements of the sequence randomly .
parameter :lst list
Function use of list

* s.append(x) Put the object x Append to list s The tail of
* s.pop(i) Pop up subscript i Place element , When omitted i The last element of the list pops up when the
<>21 Implementation of point playing card game
import random def get_shuffled_deck(): """ Initialization includes 52 A list of cards , And mixed row back , A washed deck of playing cards """
# Design and color suits And serial number suits = {'♣', '♠', '♦', '♥'} ranks = {'2', '3', '4', '5', '6', '7',
'8', '9', '10', 'J', 'Q', 'K', 'A'} deck = [] # Create a pair 52 Playing cards for suit in suits:
for rank in ranks: deck.append(suit + ' ' + rank) random.shuffle(deck) return
deckdef deal_card(deck, participant): """ Give a card to the participant participant""" card = deck.pop(
) participant.append(card) return card def compute_total(hand):
""" Calculates and returns the sum of points in a hand """ values = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, \
'9':9, '10':10, 'J':10, 'Q':10, 'K':10, 'A':11} result = 0 # The sum of initialization points is 0 numAces = 0
#A The number of # Calculate the sum of points A The number of for card in hand: result += values[card[2:]] if card[2] == 'A'
: numAces += 1 # If the number of points and >21, Try to A treat as 1 To calculate # ( That is, subtracting 10), Multiple A Cyclic subtraction 10, Up to the count <=21 while result >
21 and numAces > 0: result -= 10 numAces -= 1 return result def print_hand(hand)
: for card in hand: print(card, end = ' ') print() def blackjack():
"""21 Order a card game , Computer artificial intelligence AI For the makers , The user is the player """ # Initialize a shuffled deck of playing cards , Initialize the cards in the hands of the dealer and player to be empty deck =
get_shuffled_deck() house = [] # Dealer's card player = [] # Player's card # Give the player and the dealer two cards in turn for i
in range(2): deal_card(deck, player) deal_card(deck, house) # Print a hand print(
' Dealer's card :', end = ''); print_hand(house) print(' Player's card :', end = ''); print_hand(
player) # Ask the player whether to continue to take the card , If it is , Continue to deal cards to players answer = input(' Do you want to continue to take cards (y/n, Default to y):') while
answerin ('', 'y', 'Y'): card = deal_card(deck, player) print(' The card the player gets is :', end =
''); print_hand(player) # Calculation mark if compute_total(player) > 21: print(' Blow it up Player loses !')
return answer = input(' Do you want to continue to take cards (y/n, Default to y):') # Banker ( Computer artificial intelligence ) Press “ Rules of the maker ” Make sure you take the card while
compute_total(house) < 17: card = deal_card(deck, house) print(' What's the card the dealer got :', end =
''); print_hand(house) # Calculation mark if compute_total(house) > 21: print(' Blow it up Players win cards !')
return # Calculate the points of the dealer and the player respectively , Compare the number of points , Output the winning and losing result information houseTotal, playerTotal = compute_total(
house), compute_total(player) if houseTotal > playerTotal: print(' Dealer wins !') elif
houseTotal< playerTotal: print(' Players win cards !') elif houseTotal == 21 and 2 == len(house
) < len(player) : # have blackjack The best dealer wins print(' Dealer wins !') elif playerTotal == 21 and 2
== len(player) < len(house) : # have blackjack The best player wins print(' Players win cards !') else: print(
' it ends in a draw !') if __name__ == '__main__': blackjack()
Running result example 1 :
Dealer's card :♣ 10 ♦ Q Player's card :♦ 4 ♥ 8 Do you want to continue to take cards (y/n, Default to y):y The card the player gets is :♦ 4 ♥ 8 ♥ K Blow it up Player loses !

Technology