【Dison夏令营 Day 03】使用 Python 创建我们自己的 21 点游戏

news2024/11/25 18:29:46

21 点(英文:Blackjack)是一种在赌场玩的纸牌游戏。这种游戏的参与者不是互相竞争,而是与赌场指定的庄家竞争。在本文中,我们将从头开始创建可在终端上玩的玩家与庄家之间的二十一点游戏。
在这里插入图片描述

二十一点规则

我们将为从未玩过二十一点的读者提供一套简短的规则。21 点的神奇数字是 21。玩家拿到的所有牌的价值相加,如果总和超过 21,玩家就会立即爆牌并输掉。

如果玩家拿到的牌正好是 21,那么玩家就赢了庄家。否则,要想获胜,玩家的牌数总和必须大于庄家的牌数总和。

每张扑克牌的面值都是 10,而 A 可以被算作 1 或 11,这对玩家的获胜机会非常有利。其余牌的价值由其编号决定。

二十一点游戏的发牌过程如下:
  • 一张牌发给朝上的玩家(所有人都能看到)。
  • 庄家给自己发一张所有人都能看到的牌。
  • 另一张牌发给朝上的玩家。
  • 庄家给自己发一张朝下的牌。
  • 玩家必须决定是用现有的牌站着还是再拿一张牌。
  • 如果玩家决定打牌,庄家会再发一张牌。
  • 如果玩家决定不出牌,庄家就会亮出隐藏的牌。
  • 庄家无权决定玩家是跟还是不跟。一般规则是,如果庄家牌的总和小于 17,庄家需要继续打出更多的牌。
  • 一旦庄家牌的总和达到或超过 17 点,庄家就有义务不出牌。
  • 根据最后的牌数决定胜负。

了解规则后,21 点游戏的编程就变得简单了。从零开始创建基于终端的游戏需要三个主要部分:游戏设计、游戏逻辑和玩家互动管理。

用 Python 设计二十一点

首先,我们将进行游戏设计。我们的任务是在终端上有效地显示一系列牌,如下图所示。

在这里插入图片描述
我们需要一个能打印卡片序列且与卡片数量无关的函数。此外,它还必须提供在需要时打印隐藏卡片的功能。

下面的代码解决了我们的问题。

# 打印卡片的功能
def print_cards(cards, hidden):
         
    s = ""
    for card in cards:
        s = s + "\t ________________"
    if hidden:
        s += "\t ________________"
    print(s)
 
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"    
    print(s)
 
    s = ""
    for card in cards:
        if card.value == '10':
            s = s + "\t|  {}            |".format(card.value)
        else:
            s = s + "\t|  {}             |".format(card.value)  
    if hidden:
        s += "\t|                |"    
    print(s)
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|      * *       |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|    *     *     |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|   *       *    |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|   *       *    |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|       {}        |".format(card.suit)
    if hidden:
        s += "\t|          *     |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|         *      |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|        *       |"
    print(s)
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)    
 
    s = ""
    for card in cards:
        if card.value == '10':
            s = s + "\t|            {}  |".format(card.value)
        else:
            s = s + "\t|            {}   |".format(card.value)
    if hidden:
        s += "\t|        *       |"        
    print(s)    
         
    s = ""
    for card in cards:
        s = s + "\t|________________|"
    if hidden:
        s += "\t|________________|"
    print(s)        
 
    print()

每张卡片的详细信息都存储为一个卡片对象。print_cards() 函数的第二个参数是一个布尔值,表示是否显示隐藏的卡片。

创建卡片

在类和对象的帮助下,我们可以创建一个花色和数值的组合来表示一张 “扑克牌”。在二十一点游戏中,一张牌有三个属性:花色、代表值和作为分数的值。

所有上述属性都在下面的 "卡 "类中维护。

# 卡片类别的定义
class Card:
    def __init__(self, suit, value, card_value):
         
        # 纸牌的花色,如黑桃和梅花
        self.suit = suit
 
        # 表示牌的价值,如 A 表示 Ace,K 表示 King
        self.value = value
 
        # 卡片的分值,如 10 分代表国王
        self.card_value = card_value

使用上述类,我们可以创建一副包含 52 个 Card 对象的纸牌。

一些基本价值观

每种纸牌游戏都需要一些基本价值,如花色类型、纸牌类型以及每张纸牌的价值。

为了更方便理解,我代码和备注都用全英文了

# The type of suit
suits = ["Spades", "Hearts", "Clubs", "Diamonds"]
 
# The suit value 
suits_values = {"Spades":"\u2664", "Hearts":"\u2661", "Clubs": "\u2667", "Diamonds": "\u2662"}
 
# The type of card
cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
 
# The card value
cards_values = {"A": 11, "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 点。这一策略背后的理念是,每当玩家/发牌者的分数似乎超过 21 分时,我们就可以将 A 的分数(如果发牌)减为 1 分。

我们稍后将在本文中看到减分的实施。

生成一副扑克牌

一副普通的扑克牌由 52 张牌组成,每张牌都有不同的花色和价值组合。利用上述基本值和卡片类,我们可以生成一副扑克牌。

# The deck of cards
deck = []
 
# Loop for every type of suit
for suit in suits:
 
    # Loop for every type of card in a suit
    for card in cards:
 
        # Adding card to the deck
        deck.append(Card(suits_values[suit], card, cards_values[card]))

实际上,21 点游戏涉及多副扑克牌,因此可以重复使用上述循环来填充多副扑克牌。

新创建的牌面将传递给执行游戏的函数。

blackjack_game(deck)      

让我们来了解一下玩家和电脑发牌员之间 21 点游戏单次迭代背后的游戏逻辑。

声明重要的游戏变量

在任何时刻,我们都需要以下游戏变量:

  • 玩家和庄家的发牌列表。
  • 双方牌值的总和。
# Function for a single game of blackjack
def blackjack_game(deck):
 
    global cards_values
 
    # Cards for both dealer and player
    player_cards = []
    dealer_cards = []
 
    # Scores for both dealer and player
    player_score = 0
    dealer_score = 0

当我们设计游戏逻辑时,这些游戏变量就会发挥作用。

Python 二十一点游戏逻辑

整个游戏逻辑都围绕着发牌和玩家对击中或站立的选择。只要我们处理好上述两件事,今天的工作就完成了。

发牌的第一阶段:必发牌

初始发牌包括给玩家和庄家两张牌。不过,庄家的第二张牌必须保持未知。

# Initial dealing for player and dealer
while len(player_cards) < 2:
 
    # Randomly dealing a card
    player_card = random.choice(deck)
    player_cards.append(player_card)
    deck.remove(player_card)
 
    # Updating the player score
    player_score += player_card.card_value
 
    # In case both the cards are Ace, make the first ace value as 1 
    if len(player_cards) == 2:
        if player_cards[0].card_value == 11 and player_cards[1].card_value == 11:
            player_cards[0].card_value = 1
            player_score -= 10
 
    # Print player cards and score      
    print("PLAYER CARDS: ")
    print_cards(player_cards, False)
    print("PLAYER SCORE = ", player_score)
 
    input()
 
    # Randomly dealing a card
    dealer_card = random.choice(deck)
    dealer_cards.append(dealer_card)
    deck.remove(dealer_card)
 
    # Updating the dealer score
    dealer_score += dealer_card.card_value
 
    # Print dealer cards and score, keeping in mind to hide the second card and score
    print("DEALER CARDS: ")
    if len(dealer_cards) == 1:
        print_cards(dealer_cards, False)
        print("DEALER SCORE = ", dealer_score)
    else:
        print_cards(dealer_cards[:-1], True)    
        print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)
 
 
    # In case both the cards are Ace, make the second ace value as 1 
    if len(dealer_cards) == 2:
        if dealer_cards[0].card_value == 11 and dealer_cards[1].card_value == 11:
            dealer_cards[1].card_value = 1
            dealer_score -= 10
 
    input()
 
# Player gets a blackjack   
if player_score == 21:
    print("PLAYER HAS A BLACKJACK!!!!")
    print("PLAYER WINS!!!!")
    quit()

对于一个看似简单的交易来说,这可能是一个很大的挑战。让我们来了解一下上述代码所涉及的过程:

  • 主循环一直运行到玩家和庄家各拿到两张牌为止。
  • 庄家从牌组中随机抽取一张牌,然后在下一步中将这张牌从牌组中移除。
  • 这张牌的价值加到玩家的分数中。
  • 同样,庄家也会随机抽取一张牌,并将其价值计入庄家得分。
  • 玩家的牌正常显示在屏幕上。
  • 庄家的牌会小心显示,不会显示第二张牌及其牌值。
  • 如果任何一方拿到双 A,则会调整其分数,以确保双方都不爆牌。
  • 上述所有步骤顺利完成后,我们进入第二阶段发牌。

注:玩家和庄家的分数调整有细微差别。前者调整的是第一张牌的价值,而后者调整的是第二张牌的价值。

之所以要调整第二张牌的价值,是因为如果我们调整了第一张牌的价值,就会暴露隐藏牌是 Ace 的身份。

最后要做的一件事是检查玩家是否已经拥有 21 点。如果有,玩家获胜,游戏结束。

注:input() 函数会暂停程序,直到玩家按下 "ENTER "键。这样可以防止所有游戏事件的快速回放。

clear()函数负责清除终端,为游戏提供一个干净的界面。

第二阶段发牌:玩家的选择

发牌的第二阶段取决于玩家的决定,是要另一张牌来提高分数,还是维持现有的牌。

# Print dealer and player cards
print("DEALER CARDS: ")
print_cards(dealer_cards[:-1], True)
print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)
 
print() 
 
print("PLAYER CARDS: ")
print_cards(player_cards, False)
print("PLAYER SCORE = ", player_score)
 
# Managing the player moves
while player_score < 21:
    choice = input("Enter H to Hit or S to Stand : ")
 
    # Sanity checks for player's choice
    if len(choice) != 1 or (choice.upper() != 'H' and choice.upper() != 'S'):
        clear()
        print("Wrong choice!! Try Again")
 
    # If player decides to HIT
    if choice.upper() == 'H':
 
        # Dealing a new card
        player_card = random.choice(deck)
        player_cards.append(player_card)
        deck.remove(player_card)
 
        # Updating player score
        player_score += player_card.card_value
 
        # Updating player score in case player's card have ace in them
        c = 0
        while player_score > 21 and c < len(player_cards):
            if player_cards[c].card_value == 11:
                player_cards[c].card_value = 1
                player_score -= 10
                c += 1
            else:
                c += 1 
 
        clear()     
 
        # Print player and dealer cards
        print("DEALER CARDS: ")
        print_cards(dealer_cards[:-1], True)
        print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)
 
        print()
 
        print("PLAYER CARDS: ")
        print_cards(player_cards, False)
        print("PLAYER SCORE = ", player_score)
         
    # If player decides to Stand
    if choice.upper() == 'S':
        break
 
# Check if player has a Blackjack
if player_score == 21:
    print("PLAYER HAS A BLACKJACK")
    quit()
 
# Check if player busts
if player_score > 21:
    print("PLAYER BUSTED!!! GAME OVER!!!")
    quit()

玩家决定是打还是不打,直到分数超过 21 分或玩家决定不打为止。玩家发牌的数量没有限制,只有分数限制。

玩家每决定击中一次,就会从牌组中发出一张新牌,分数也会随之更新。如前所述,A 可以算作 1 或 11。如果分数超过 21,一段特殊的代码会将 A 的值从 11 转换为 1。

当玩家对当前分数满意时,他就会起立。当他这样做时,我们会在进行一些强制性检查(如二十一点或爆牌情况)后进入发牌的最后阶段。

发牌的最后阶段:庄家的牌

在发牌的最后阶段,庄家的暗牌会被揭开,庄家的分数也会被揭开。根据二十一点的标准规则,庄家必须给自己发更多的牌,直到分数大于或等于 17。

# Managing the dealer moves
while dealer_score < 17:
    clear() 
 
    print("DEALER DECIDES TO HIT.....")
 
    # Dealing card for dealer
    dealer_card = random.choice(deck)
    dealer_cards.append(dealer_card)
    deck.remove(dealer_card)
 
    # Updating the dealer's score
    dealer_score += dealer_card.card_value
 
    # Updating player score in case player's card have ace in them
    c = 0
    while dealer_score > 21 and c < len(dealer_cards):
        if dealer_cards[c].card_value == 11:
            dealer_cards[c].card_value = 1
            dealer_score -= 10
            c += 1
        else:
            c += 1
 
    # print player and dealer cards
    print("PLAYER CARDS: ")
    print_cards(player_cards, False)
    print("PLAYER SCORE = ", player_score)
 
    print()
 
    print("DEALER CARDS: ")
    print_cards(dealer_cards, False)
    print("DEALER SCORE = ", dealer_score)
 
    input()

庄家继续击球,直到分数越过 17 分为止。如果需要,我们也有类似的方法将 A 的牌值从 11 转换为 1。

游戏终结

当庄家的分数是 17 或更多时,我们就进入 “终局”,即比较牌局的胜负。可能会出现以下几种情况:

  • 庄家爆牌 - 庄家的分数超过 21。
  • 庄家有 21 点 - 庄家的准确分数是 21。
  • 平局 - 玩家和庄家的分数相同。
  • 闲家赢 - 闲家的分数超过庄家。
  • 庄家赢 - 庄家的分数超过闲家。

我们会检查上述每种可能性,并宣布获胜者。

# Dealer busts
if dealer_score > 21:        
    print("DEALER BUSTED!!! YOU WIN!!!") 
    quit()  
 
# Dealer gets a blackjack
if dealer_score == 21:
    print("DEALER HAS A BLACKJACK!!! PLAYER LOSES")
    quit()
 
# TIE Game
if dealer_score == player_score:
    print("TIE GAME!!!!")
 
# Player Wins
elif player_score > dealer_score:
    print("PLAYER WINS!!!")                 
 
# Dealer Wins
else:
    print("DEALER WINS!!!")  

至此,玩家与庄家之间的 21 点游戏的单次迭代结束。

二十一点游戏的完整 Python 代码

import random
import os
import time
 
# The Card class definition
class Card:
    def __init__(self, suit, value, card_value):
         
        # Suit of the Card like Spades and Clubs
        self.suit = suit
 
        # Representing Value of the Card like A for Ace, K for King
        self.value = value
 
        # Score Value for the Card like 10 for King
        self.card_value = card_value
 
# Clear the terminal
def clear():
    os.system("clear")
 
# Function to print the cards
def print_cards(cards, hidden):
         
    s = ""
    for card in cards:
        s = s + "\t ________________"
    if hidden:
        s += "\t ________________"
    print(s)
 
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"    
    print(s)
 
    s = ""
    for card in cards:
        if card.value == '10':
            s = s + "\t|  {}            |".format(card.value)
        else:
            s = s + "\t|  {}             |".format(card.value)  
    if hidden:
        s += "\t|                |"    
    print(s)
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|      * *       |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|    *     *     |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|   *       *    |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|   *       *    |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|       {}        |".format(card.suit)
    if hidden:
        s += "\t|          *     |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|         *      |"
    print(s)    
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|        *       |"
    print(s)
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)
 
    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)    
 
    s = ""
    for card in cards:
        if card.value == '10':
            s = s + "\t|            {}  |".format(card.value)
        else:
            s = s + "\t|            {}   |".format(card.value)
    if hidden:
        s += "\t|        *       |"        
    print(s)    
         
    s = ""
    for card in cards:
        s = s + "\t|________________|"
    if hidden:
        s += "\t|________________|"
    print(s)        
 
    print()
 
 
# Function for a single game of blackjack
def blackjack_game(deck):
 
    # Cards for both dealer and player
    player_cards = []
    dealer_cards = []
 
    # Scores for both dealer and player
    player_score = 0
    dealer_score = 0
 
    clear()
 
    # Initial dealing for player and dealer
    while len(player_cards) < 2:
 
        # Randomly dealing a card
        player_card = random.choice(deck)
        player_cards.append(player_card)
        deck.remove(player_card)
 
        # Updating the player score
        player_score += player_card.card_value
 
        # In case both the cards are Ace, make the first ace value as 1 
        if len(player_cards) == 2:
            if player_cards[0].card_value == 11 and player_cards[1].card_value == 11:
                player_cards[0].card_value = 1
                player_score -= 10
 
        # Print player cards and score      
        print("PLAYER CARDS: ")
        print_cards(player_cards, False)
        print("PLAYER SCORE = ", player_score)
 
        input()
 
        # Randomly dealing a card
        dealer_card = random.choice(deck)
        dealer_cards.append(dealer_card)
        deck.remove(dealer_card)
 
        # Updating the dealer score
        dealer_score += dealer_card.card_value
 
        # Print dealer cards and score, keeping in mind to hide the second card and score
        print("DEALER CARDS: ")
        if len(dealer_cards) == 1:
            print_cards(dealer_cards, False)
            print("DEALER SCORE = ", dealer_score)
        else:
            print_cards(dealer_cards[:-1], True)    
            print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)
 
 
        # In case both the cards are Ace, make the second ace value as 1 
        if len(dealer_cards) == 2:
            if dealer_cards[0].card_value == 11 and dealer_cards[1].card_value == 11:
                dealer_cards[1].card_value = 1
                dealer_score -= 10
 
        input()
 
    # Player gets a blackjack   
    if player_score == 21:
        print("PLAYER HAS A BLACKJACK!!!!")
        print("PLAYER WINS!!!!")
        quit()
 
    clear()
 
    # Print dealer and player cards
    print("DEALER CARDS: ")
    print_cards(dealer_cards[:-1], True)
    print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)
 
    print() 
 
    print("PLAYER CARDS: ")
    print_cards(player_cards, False)
    print("PLAYER SCORE = ", player_score)
 
    # Managing the player moves
    while player_score < 21:
        choice = input("Enter H to Hit or S to Stand : ")
 
        # Sanity checks for player's choice
        if len(choice) != 1 or (choice.upper() != 'H' and choice.upper() != 'S'):
            clear()
            print("Wrong choice!! Try Again")
 
        # If player decides to HIT
        if choice.upper() == 'H':
 
            # Dealing a new card
            player_card = random.choice(deck)
            player_cards.append(player_card)
            deck.remove(player_card)
 
            # Updating player score
            player_score += player_card.card_value
 
            # Updating player score in case player's card have ace in them
            c = 0
            while player_score > 21 and c < len(player_cards):
                if player_cards[c].card_value == 11:
                    player_cards[c].card_value = 1
                    player_score -= 10
                    c += 1
                else:
                    c += 1 
 
            clear()     
 
            # Print player and dealer cards
            print("DEALER CARDS: ")
            print_cards(dealer_cards[:-1], True)
            print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)
 
            print()
 
            print("PLAYER CARDS: ")
            print_cards(player_cards, False)
            print("PLAYER SCORE = ", player_score)
             
        # If player decides to Stand
        if choice.upper() == 'S':
            break
 
 
    clear() 
 
    # Print player and dealer cards
    print("PLAYER CARDS: ")
    print_cards(player_cards, False)
    print("PLAYER SCORE = ", player_score)
 
    print()
    print("DEALER IS REVEALING THE CARDS....")
 
    print("DEALER CARDS: ")
    print_cards(dealer_cards, False)
    print("DEALER SCORE = ", dealer_score)
 
    # Check if player has a Blackjack
    if player_score == 21:
        print("PLAYER HAS A BLACKJACK")
        quit()
 
    # Check if player busts
    if player_score > 21:
        print("PLAYER BUSTED!!! GAME OVER!!!")
        quit()
 
    input() 
 
    # Managing the dealer moves
    while dealer_score < 17:
        clear() 
 
        print("DEALER DECIDES TO HIT.....")
 
        # Dealing card for dealer
        dealer_card = random.choice(deck)
        dealer_cards.append(dealer_card)
        deck.remove(dealer_card)
 
        # Updating the dealer's score
        dealer_score += dealer_card.card_value
 
        # Updating player score in case player's card have ace in them
        c = 0
        while dealer_score > 21 and c < len(dealer_cards):
            if dealer_cards[c].card_value == 11:
                dealer_cards[c].card_value = 1
                dealer_score -= 10
                c += 1
            else:
                c += 1
 
        # print player and dealer cards
        print("PLAYER CARDS: ")
        print_cards(player_cards, False)
        print("PLAYER SCORE = ", player_score)
 
        print()
 
        print("DEALER CARDS: ")
        print_cards(dealer_cards, False)
        print("DEALER SCORE = ", dealer_score)      
 
        input()
 
    # Dealer busts
    if dealer_score > 21:        
        print("DEALER BUSTED!!! YOU WIN!!!") 
        quit()  
 
    # Dealer gets a blackjack
    if dealer_score == 21:
        print("DEALER HAS A BLACKJACK!!! PLAYER LOSES")
        quit()
 
    # TIE Game
    if dealer_score == player_score:
        print("TIE GAME!!!!")
 
    # Player Wins
    elif player_score > dealer_score:
        print("PLAYER WINS!!!")                 
 
    # Dealer Wins
    else:
        print("DEALER WINS!!!")                 
 
if __name__ == '__main__':
 
    # The type of suit
    suits = ["Spades", "Hearts", "Clubs", "Diamonds"]
 
    # The suit value 
    suits_values = {"Spades":"\u2664", "Hearts":"\u2661", "Clubs": "\u2667", "Diamonds": "\u2662"}
 
    # The type of card
    cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
 
    # The card value
    cards_values = {"A": 11, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}
 
    # The deck of cards
    deck = []
 
    # Loop for every type of suit
    for suit in suits:
 
        # Loop for every type of card in a suit
        for card in cards:
 
            # Adding card to the deck
            deck.append(Card(suits_values[suit], card, cards_values[card]))
     
    blackjack_game(deck) 

读者不必遵循整个编码顺序。通过增加多人对庄家的功能,可以对上述代码进行各种修改。

结论

二十一点游戏起初看似简单随意,但只有当玩家遵循某些策略(如算牌)时,游戏才会变得复杂。

世界上流传着许多不同版本的 21 点游戏,如瑞典酒吧 21 点游戏和家庭游戏 21 点游戏。好奇的读者可以了解这些变体,并尝试利用本文中获得的知识来实施它们。

感谢大家花时间阅读我的文章,你们的支持是我不断前进的动力。期望未来能为大家带来更多有价值的内容,请多多关注我的动态!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1876040.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Python实战因果推断】7_元学习器2

目录 X-Learner X-Learner X-learner 在解释上要比前一个学习器复杂得多&#xff0c;但其实现却非常简单&#xff0c;所以如果你一开始不理解&#xff0c;也不用担心。X 学习器有两个阶段和一个倾向得分模型。第一个阶段与 T 学习器相同。首先&#xff0c;将样本分为治疗组和…

3.js - 反射率(reflectivity) 、折射率(ior)

没啥太大的感觉 反射率 reflectivity 概念 反射率&#xff1a;指的是&#xff0c;材质表面反射光线的能力反射率&#xff0c;用于控制材质对环境光&#xff0c;或光源的反射程度反射率越高&#xff0c;材质表面反射的光线越多&#xff0c;看起来就越光亮使用 适用于&#xff0…

react+customize-cra使用less+less-loader时,可能遇到的问题及解决办法

目录 1、先附上各依赖版本和config-overrides.js配置代码&#xff0c;按这个版本和配置就没问题 2、问题&#xff08;注意&#xff1a;问题顺序没有先后之分哦&#xff09; 2.1、TypeError: Cannot read property tap of undefined 2.2、No module factory available for d…

算法金 | 决策树、随机森林、bagging、boosting、Adaboost、GBDT、XGBoost 算法大全

大侠幸会&#xff0c;在下全网同名「算法金」 0 基础转 AI 上岸&#xff0c;多个算法赛 Top 「日更万日&#xff0c;让更多人享受智能乐趣」 决策树是一种简单直观的机器学习算法&#xff0c;它广泛应用于分类和回归问题中。它的核心思想是将复杂的决策过程分解成一系列简单的决…

【windows|011】TCP/IP5层模型常见协议及应用总结大全

&#x1f341;博主简介&#xff1a; &#x1f3c5;云计算领域优质创作者 &#x1f3c5;2022年CSDN新星计划python赛道第一名 &#x1f3c5;2022年CSDN原力计划优质作者 ​ &#x1f3c5;阿里云ACE认证高级工程师 ​ &#x1f3c5;阿里云开发者社区专家博主 &#x1f48a;交流社…

【计算机毕业设计】077停车场微信小程序

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…

qmt量化交易策略小白学习笔记第52期【qmt编程之商品期货数据】

qmt编程之获取商品期货数据 qmt更加详细的教程方法&#xff0c;会持续慢慢梳理。 也可找寻博主的历史文章&#xff0c;搜索关键词查看解决方案 &#xff01; 主力合约生成规则 每个品种只有一个主连合约。主连合约于下一个交易日进行指向切换&#xff0c;切换前主连合约不变…

2024全新【大模型学习路径导航】从入门到精通的全面规划

引言 随着人工智能技术的迅猛发展&#xff0c;大模型成为了引领技术变革的关键力量。无论你是对大模型领域感兴趣的初学者&#xff0c;还是希望深化理解并应用大模型的开发者&#xff0c;这份精心策划的学习路径将为你提供从零基础到精通的全面指导。 第一阶段&#xff1a;基…

堆排序思想分享

人不走空 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌赋&#xff1a;斯是陋室&#xff0c;惟吾德馨 目录 &#x1f308;个人主页&#xff1a;人不走空 &#x1f496;系列专栏&#xff1a;算法专题 ⏰诗词歌…

鸿蒙NEXT

[中国&#xff0c;东莞&#xff0c;2024年6月24日] 华为开发者大会&#xff08;HDC&#xff09;正式开幕&#xff0c;带来全新的 HarmonyOS NEXT、盘古大模型5.0等最创新成果&#xff0c;持续为消费者和开发者带来创新体验。 HarmonyOS NEXT 鸿蒙生态 星河璀璨 鸿蒙生态设备数…

boost asio异步服务器(4)处理粘包

粘包的产生 当客户端发送多个数据包给服务器时&#xff0c;服务器底层的tcp接收缓冲区收到的数据为粘连在一起的。这种情况的产生通常是服务器端处理数据的速率不如客户端的发送速率的情况。比如&#xff1a;客户端1s内连续发送了两个hello world&#xff01;,服务器过了2s才接…

【scau大数据原理】期末复习——堂测题

一、集群安装知识 启动集群的命令start-all.sh位于 Hadoop安装目录的sbin文件夹 目录下。 bin文件夹下包含常见的Hadoop,yarn命令&#xff1b;sbin命令下包含集群的启动、停止命令。 启动集群的命令start-all.sh包含 同时启动start-dfs.sh和start-yarn.sh 功能。…

大模型ReAct:思考与工具协同完成复杂任务推理

ReAct: Synergizing Reasoning and Acting in Language Models Github&#xff1a;https://github.com/ysymyth/ReAct 一、动机 人类的认知通常具备一定的自我调节&#xff08;self-regulation&#xff09;和策略制定&#xff08;strategization&#xff09;的能力&#xff0…

ONLYOFFICE8.1版本桌面编辑器——功能测评

一、编辑DOCX 相信大家都有写word文档的经历&#xff0c;不知道大家是不是跟我一样&#xff0c;感觉做一个word不难&#xff0c;但想做好一个word却很麻烦&#xff0c;功能太多&#xff0c;看的人眼花缭乱&#xff0c;有时候一个功能要找很久&#xff0c;甚至有的功能用一辈子都…

mybatis核心配置介绍

mybatis核心配置 【mybatis全局配置介绍】 ​ mybatis-config.xml&#xff0c;是MyBatis的全局配置文件&#xff0c;包含全局配置信息&#xff0c;如数据库连接参数、插件等。整个框架中只需要一个即可。 1、mybatis全局配置文件是mybatis框架的核心配置&#xff0c;整个框架…

Unity | Shader基础知识(第十五集:透明效果)

目录 一、前言 二、素材准备 三、准备基础代码 四、准备基础场景 五、SurfaceOutput结构体 六、透明度 七、渲染顺序 八、选错的后果 九、Tags之渲染顺序 十、Cull&#xff08;正面和反面渲染&#xff09; 十一、代码汇总 十二、作者的碎碎念 一、前言 因为shader…

python-斐波那契数列

[题目描述] 斐波那契数列是指这样的数列&#xff1a;数列的第一个和第二个数都为 1&#xff0c;接下来每个数都等于前面 2个数之和。 给出一个正整数 a&#xff0c;要求斐波那契数列中第 a 个数是多少。输入&#xff1a; 第 1 行是测试数据的组数 n&#xff0c;后面跟着 n 行输…

UnityShader SDF有向距离场简单实现

UnityShader SDF有向距离场简单实现 前言项目场景布置连连看画一个圆复制一个圆计算修改shader参数 鸣谢 前言 突然看到B站的一个教程&#xff0c;还不错&#xff0c;记录一下 项目 场景布置 使用ASE连连看&#xff0c;所以先要导入Amplify Shader Editor 连连看 画一个…

有人问周鸿祎: 学历不重要,为什么360只要985和211?

关注、星标公众号&#xff0c;直达精彩内容 有人问周鸿祎:你说学历不重要&#xff0c;为什么360招聘的人才只要985和211&#xff1f;他说这个事情&#xff0c;我专门问了我们的人力资源&#xff0c;我们的干品分为校园招聘和社会招聘 校园招聘的话会看文凭 社会招聘的话&#x…

性能优化:Java垃圾回收机制深度解析 - 让你的应用飞起来!

文章目录 一、什么是垃圾回收二、Java 内存区域划分三、垃圾回收算法1. 标记-清除&#xff08;Mark-Sweep&#xff09;算法2. 复制&#xff08;Copying&#xff09;算法3. 标记-整理&#xff08;Mark-Compact&#xff09;算法4. 分代收集&#xff08;Generational Collecting&a…