Skip to content
Rahul Nanwani edited this page Jan 12, 2022 · 2 revisions

blackjack21

A complete package for creating a multiplayer blackjack table.

Features

  • Create Tables.
  • Max. 5 players per table.
  • Hit, Stand, Double Down, Split.

Wiki Guide

Installation

Install Basic

pip install blackjack21

Upgrade

pip install --upgrade blackjack21

Install a specific version

pip install blackjack21==1.1.1

Basic Example

from blackjack21 import Table


def print_cards(player):
    print(f"\n{player.name}")
    for card in player.hand:
        print(f"{card.rank} of {card.suit}")
    print(player.total)


def play_round(player):
    print_cards(player)
    while not (player.bust or player.stand):
        action = int(input("\nHit(1), Stand(2): "))
        if action == 1:
            player.play_hit()
            print_cards(player)
        elif action == 2:
            player.play_stand()


def show_result(table):
    print_cards(table.dealer)
    print(f"\nDealer has {table.dealer.total}")
    for player in table.players:
        result = player.result
        if result > 0:
            print(f"{player.name} wins ${player.bet} ({player.total})")
        elif result == 0:
            print(f"Hand tied ({player.total})")
        else:
            print(f"{player.name} loses ${player.bet} ({player.total})")


def main():
    a = ("Charlotte", 100)
    players = (a, )

    table = Table(players)
    table.dealer.deal_cards()

    for player in table.players:
        play_round(player)

    table.dealer.play_dealer()
    show_result(table)


if __name__ == "__main__":
    main()

Advanced Example

from blackjack21 import Table


def print_cards(player):
    print(f"\n{player.name}")
    for card in player.hand:
        print(f"{card.rank} of {card.suit}")
    print(player.total)


def play_hit_or_stand(player):
    while not (player.bust or player.stand):
        action = int(input("\nHit(1), Stand(2): "))
        if action == 1:
            player.play_hit()
            print_cards(player)
        elif action == 2:
            player.play_stand()


def play_round(player):
    print_cards(player)

    if player.can_split():
        action = int(input("\nHit(1), Stand(2), Double down(3), Split(4): "))
    else:
        action = int(input("\nHit(1), Stand(2), Double down(3): "))

    if action == 1:
        player.play_hit()
        print_cards(player)
    elif action == 2:
        player.play_stand()
    elif action == 3:
        player.play_double_down()
        print_cards(player)
    elif action == 4 and player.can_split():
        player.play_split()
        print_cards(player)

    play_hit_or_stand(player)

    if player.split:
        print_cards(player.split)
        play_hit_or_stand(player.split)


def print_player_result(player):
    result = player.result
    if result == 3:
        print(f"The dealer is bust, {player.name} wins ${player.bet} ({player.total})")
    elif result in [1, 2]:
        print(f"{player.name} wins ${player.bet} ({player.total})")
    elif result == 0:
        print(f"Hand tied ({player.total})")
    elif result == -1:
        print(f"{player.name} loses ${player.bet} ({player.total})")
    else:
        print(f"{player.name} is bust, loses ${player.bet} ({player.total})")


def show_result(table):
    print_cards(table.dealer)
    print(f"\nDealer has {table.dealer.total}")
    for player in table.players:
        print_player_result(player)
        if player.split:
            print_player_result(player.split)


def main():
    a = ("Charlotte", 100)
    b = ("Jennifer", 200)
    players = (a, b)

    table = Table(players)
    table.dealer.deal_cards()

    for player in table.players:
        play_round(player)

    table.dealer.play_dealer()
    show_result(table)


if __name__ == "__main__":
    main()

Classes

Table

Table(players, dealer, suits, ranks)

Arguments:

  • players: A tuple of player tuples, where a player tuple is as (name: str, bet: int) (Min. 1 player, Max. 5 players)
  • dealer: Name of the dealer as a string (Default: "Dealer")
  • suits: A tuple of 4 suits (Default: ("Hearts", "Diamonds", "Spades", "Clubs"))
  • ranks: A tuple of 13 ranks ace to king (Default: ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"))

Attributes:

  • players: A tuple of Player objects
  • dealer: Dealer object
  • deck: A list of Card objects in the deck

PlayerBase

This object will be created from the method split() from the Player object.

Attributes:

  • name: Player name string
  • hand: A list of Card objects in the player's hand
  • total: Sum of the card values in the hand
  • bet: Bet placed by the player
  • bust: A bool if the player's hand total is more than 21
  • stand: A bool if the player has called for no further dealing of cards for the current round
  • result: Check the key for this below
-2: Player busts
-1: Player has less than the dealer
0: Player has the same total as the dealer and both are not bust
1: Player has 21 (aka. blackjack)
2: Player has greater total than the dealer
3: The dealer is bust
None: player or dealer has not finished playing their hand yet

Methods:

  • play_hit(): This will deal an extra card to the player, returns the Card object for the card dealt
  • play_stand(): This will stop further dealing of any cards to the player for the current round, returns a bool

Player

A tuple of these objects will be created on creating a Table object.

Attributes:

  • name: Player name string
  • hand: A list of Card objects in the player's hand
  • total: Sum of the card values in the hand
  • bet: Bet placed by the player
  • bust: A bool if the player's hand total is more than 21
  • stand: A bool if the player has called for no further dealing of cards for the current round
  • split: A PlayerBase object if method split() is used, else False
  • result: Check the key for this below
-2: Player busts
-1: Player has less than the dealer
0: Player has the same total as the dealer and both are not bust
1: Player has 21 (aka. blackjack)
2: Player has greater total than the dealer
3: The dealer is bust
None: player or dealer has not finished playing their hand yet

Methods:

  • play_hit(): This will deal an extra card to the player, returns the Card object for the card dealt
  • play_stand(): This will stop further dealing of any cards to the player for the current round, returns a bool
  • can_double_down(): Returns a bool if the player can play double down
  • can_split(): Returns a bool if the player can play split
  • play_double_down(): Double the bet in exchange for just one hit, returns the Card object for the card dealt
  • play_split(): The pair will split into two hands, keeping the bet the same on both hands. If the player bets 100 initially, so after playing split the player will have placed 100 on the first hand, and 100 more on the second hand. Returns the PlayerBase object

Dealer

This object will be created by creating a Table object.

Attributes:

  • name: Dealer name string
  • hand: A list of Card objects in the dealer's hand
  • total: Sum of the card values in the hand
  • bust: A bool if the dealer has a total of more than 21
  • stand: A bool if the dealer has a total of more than 17

Methods:

  • deal_cards(): This method will deal two cards to each hand and self(the dealer's total will be considered only for the first card), returns a bool for successful dealing of cards to each hand
  • play_dealer(): Use this method after all players have finished playing their hand, returns None

Card

A list of these objects will be created under the list deck attribute of the Table object.

Attributes:

  • suit: Suit of the card as a string
  • rank: Rank of the card as a string