Skip to contents

Simulates a full round of Blackjack with n_players playing against a dealer. Each player can choose to hit or stand in turn; after all players have finished, the dealer draws according to standard rules. Finally, each player’s total is compared to the dealer’s to determine the outcome.

Usage

play_blackjack_multi(n_players = 2, input_fn = readline, ...)

Arguments

n_players

Number of players (default is 2)

input_fn

holder to obtain player actions (defaults to readline) which can be overridden to inject fake input for unit tests or if the user want to add fake actions.

...

Additional arguments (currently unused; reserved for future extensions)

Value

A data.frame with one row per player and these columns:

player

Character, e.g.\ “Player 1”, “Player 2”, ….

hand

Character: the player’s final cards, space-separated (e.g.\ “A♠ 10♦ 3♣”).

score

Integer: total value of the player’s hand.

result

Character: one of “Player busts,” “Dealer busts,” “Player wins,” “Dealer wins,” or “Push.”

Details

This function use its own build-in creating_shuffle_deck to create a shuffled deck of cards, and then deals two cards to all the player and the dealer. The player can then choose to hit (draw another card) or stand (end their turn). After the every player's turn, the dealer will draw cards according to standard Blackjack rules until they reach a score of 17 or higher. The function then evaluates the scores and determines the winner. Moreover, it is not possible for the user to use their custom deck instead. The function only support deck of card from the vctrs class.

Examples

# Simulate a game with 2 players using automatic decisions
# play_blackjack_multi() # Defaults to 2 players
# Player 1's turn
# Hit (h) or stand (s)?
# Player 2's turn
# Hit (h) or stand (s)?
# Example of using a custom input function to simulate player actions
play_blackjack_multi(n_players = 2, input_fn = function(...) "h") # Both player hit until burst
#> [1] Dealer shows: 6♣ ?
#> [1] Player 1 turn:
#> [1] Your hand: 8♠ Q♥ (Total = 18 )
#> [1] You drew J♣
#> [1] Player 2 turn:
#> [1] Your hand: 7♠ 7♥ (Total = 14 )
#> [1] You drew 10♣
#> [1] Dealer's hand: 6♣ 5♥ A♣ 8♥ Total: 20
#>     player      hand score       result
#> 1 Player 1  8♠ Q♥ J♣    28 Player busts
#> 2 Player 2 7♠ 7♥ 10♣    24 Player busts