Skip to contents

This function simulates a full round of Blackjack where a player plays against a dealer. The game is designed to be a simple interactive experience where the player can choose to hit or stand. The function handles the dealing of cards, player actions, and dealer actions, and determines the outcome of the game.

Usage

play_blackjack(..., input_fn = readline)

Arguments

...

Additional arguments (currently unused; reserved for future extensions)

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.

Value

A list with these elements:

player_hand

Character vector (or vctrs “card” record) of the player’s final cards.

dealer_hand

Character vector (or vctrs “card” record) of the dealer’s final cards.

player_score

Integer total of the player’s hand (including ace = 1 or 11 logic).

dealer_score

Integer total of the dealer’s hand.

result

Character scalar indicating the outcome: “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 both the player and the dealer. The player can then choose to hit (draw another card) or stand (end their turn). After the 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

# An example of how you play
# play_blackjack()
# The game will then ask you: "Hit (h) or stand (s)?"
# Press hit or h for hit a new card while press stand or s for stop hitting.
# Example: use a fake input to force hit until burst
play_blackjack(input_fn = function(...) "h")
#> [1] Dealer shows: 7♠ ?
#> [1] Your hand: Q♦ 6♠ (Total = 16 )
#> [1] You drew 9♣
#> [1] Dealer's hand: 7♠ Q♣ Total: 17
#> $player_hand
#> card[3]
#> Q♦ 6♠ 9♣ 
#> 
#> $dealer_hand
#> card[2]
#> 7♠ Q♣ 
#> 
#> $player_score
#> [1] 25
#> 
#> $dealer_score
#> [1] 17
#> 
#> $result
#> [1] "Player busts"
#>