webdevpuneet.com

Blackjack vs Dealer with Chips and Hints — Free HTML CSS JS

Blackjack Card Game vs Dealer · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

About this UI Snippet

Blackjack Card Game — A Casino Table With Chips, Double Down, 3:2 Payouts and a Strategy Hint

Screenshot of the Blackjack Card Game vs Dealer snippet rendered live

Blackjack looks simple but rewards getting the details right: an Ace that changes value, a dealer who follows a fixed rule, a blackjack that pays more than an ordinary win, and a tie that returns your bet. This snippet is a complete single-player table in plain JavaScript and CSS, with every one of those rules implemented, real playing-card faces drawn without any images, chips and a bankroll, and the extras that make it feel like a game rather than a demo: a card that flies from the shoe, a hole card that flips in 3D, and a hint button that explains what basic strategy would do.

Soft and hard totals

score(hand) adds every card with each Ace counted as 11, remembers how many Aces there are, and then demotes them to 1 one at a time while the total is over 21. It returns both the best total and whether the hand is "soft" (an Ace is still counting as 11). A hand of Ace and 6 reads as a soft 17, and drawing a 10 turns it into a hard 17 without any special-case code, because the total is recalculated from scratch on every card.

A fixed dealer, and the real rules of the table

After you stand, the dealer's hole card flips and dealerTurn() draws until the total reaches 17, then stops, whether the 17 is hard or soft ("dealer stands on all 17s", printed on the felt). A natural is checked the way a casino checks it: when the dealer shows an Ace or a ten-value card, the hole card is peeked at straight away. Player blackjack against a dealer without one pays 3 to 2; both having blackjack is a push, which the earlier version of this snippet scored as a win; a bust ends the round at once as a loss.

Chips, payouts and double down

Bets are built from chips of 10, 50, 100 and 500, and the bet is shown as a stack drawn greedily from the highest chip down. finish() is the one place money moves: a win returns the bet twice, a blackjack returns it 2.5 times, a push returns it once and a loss returns nothing, so the ledger always balances. Double down is allowed on the first two cards when you can afford it: the bet doubles, you get exactly one more card, and the dealer then plays. Chips and the win, loss and push record are saved to localStorage in a try/catch, and a two-press reset button starts over.

A six-deck shoe

The shoe holds six shuffled decks (312 cards), built with the Fisher-Yates shuffle, which is the standard unbiased in-place shuffle. draw() pops from the end, a new shoe is shuffled when fewer than 78 cards remain, and the shoe counter beside the stack shows how deep you are.

Cards drawn with CSS

Each card is built from elements: a corner index at the top left and, rotated 180 degrees, at the bottom right; pips for the numbers 2 to 10 placed from a table of column and row positions (the lower half drawn upside down like a real card); a large suit for the Ace; and a framed letter for the court cards. The back is a repeating-gradient pattern. The dealer's hole card has a front and a back with backface-visibility: hidden, and removing the down class rotates it 180 degrees on the Y axis.

Animating the deal with FLIP

When a card is dealt, addCard() measures the cards already in the hand, adds the new one (which makes the hand re-centre), and animates each old card from its previous position back to the new one with the Web Animations API. The new card flies in from the shoe's position the same way. Only transforms are animated, so the motion stays smooth, and visitors who prefer reduced motion get an instant deal.

A hint that explains itself

The Hint button runs advice(), a compact basic-strategy table for six decks with a dealer who stands on 17. It looks at your total, whether it is soft, the dealer's up card and whether doubling is still possible, then names the move and gives a one-sentence reason. The recommended button pulses gold. It never plays for you.

Keyboard and accessibility

H hits, S stands, D doubles (or deals when betting), and ? shows the hint. The message line is a live region, every card has an aria-label such as "Queen of hearts" (the hole card says "Face-down card" until it is revealed), and the hands announce their cards and total.

What's included

Features

Real card faces built from HTML and CSS: corner indexes, pips for 2 to 10, big Ace, framed court cards
Hole card that flips in 3D, and cards that fly in from a six-deck shoe
FLIP animation re-spaces the cards already in a hand when a new one arrives
Chips with a 10, 50, 100 and 500 denomination, a stacked bet and an animated chip counter
Casino payouts: 3 to 2 for blackjack, 1 to 1 for a win, bet returned on a push
Double down on the first two cards, with the bankroll checked first
Soft and hard Ace scoring recalculated on every card
Dealer stands on all 17s, and peeks for blackjack under an Ace or ten-value card
Basic-strategy hint button with a one-line explanation and a pulsing recommended action
Six-deck shoe with Fisher-Yates shuffle and automatic reshuffle below 78 cards
Chips and the win, loss and push record saved in localStorage, with a two-press reset
Keyboard play (H, S, D, ?), live-region messages and labelled cards, and reduced-motion support

Step by step

How to Use

  1. 1
    Play the hand you are dealtA first-time visitor starts with a hand already dealt: a Queen and a 5 against a dealer 7. Press Hit or Stand. After that, every hand starts with your own bet.
  2. 2
    Place a betClick chips to build a bet of 10, 50, 100 or 500 each, up to your chips. Clear removes it, and Deal starts the round with a card flying from the shoe to each hand.
  3. 3
    Hit, stand or doubleHit takes a card, Stand ends your turn, and Double (on your first two cards) doubles the bet and gives you exactly one more card. Going over 21 is an instant loss.
  4. 4
    Ask for a hintPress Hint (or ?) and the table names the basic-strategy move with a short reason. The matching button pulses. Try it on a soft hand such as Ace and 6.
  5. 5
    Watch the dealerThe hole card flips in 3D and the dealer draws to 16 and stands on every 17. A blackjack pays 3 to 2, a push returns your bet, and the float text shows what you won or lost.
  6. 6
    Keep playingDeal again repeats your last bet, and Change bet lets you pick a new one. When the shoe runs low it is reshuffled. Your chips are remembered the next time you visit.
  7. 7
    Use the keyboardH, S and D play the hand, D deals when you are betting, and ? shows the hint.
  8. 8
    Make it your ownChange MIN_BET, START_CHIPS, DECKS or the CHIP_VALUES list in the JS panel. For hit-on-soft-17, change the dealer loop in dealerTurn() to also draw on a soft 17.

Real-world uses

Common Use Cases

Teaching state machines and recalculation
The Ace rule is a good example of a value that cannot be computed once and cached: it must be recalculated from the hand each time. The bet, dealing, player, dealer and done phases make a compact state machine.
Portfolio pieces that show a rules engine
A blackjack with correct Ace scoring, a house-rule dealer, 3 to 2 payouts and push handling shows edge-case thinking far better than a static card mockup.
A playable diversion on a site
A self-contained, dependency-free table is an easy drop-in for a break-room page, a waiting screen or a games section, with no server needed.
Card UI and 3D flip reference
The card faces, the backface-visibility flip and the FLIP re-spacing are reusable for memory games, flashcards or any hand-of-cards layout.
A base for splitting, insurance and surrender
The round is split into named functions (dealRound, doHit, doDouble, dealerTurn, finish), so side rules can be added without touching the scoring.
Probability and strategy practice
The hint button is a basic-strategy trainer. Play with and without it and compare your results over a few hundred hands.
Related: Battleship Ship-Finding Game
See the Battleship Ship-Finding Game for a related games pattern worth pairing with this one.
Related: Precision Aim Trainer Game
See the Precision Aim Trainer Game for a related games pattern worth pairing with this one.
Related: Mastermind Code Breaker Game
See the Mastermind Code Breaker Game for a related games pattern worth pairing with this one.
Related: Dots and Boxes Game
See the Dots and Boxes Game for a related games pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

score() adds every Ace as 11 and counts them, then demotes Aces to 1 one at a time while the total is over 21. It returns the best total that does not bust and a soft flag that says whether an Ace is still counting as 11, and it recalculates from the whole hand every time a card is added.

The dealer draws until the total is 17 or more and stands on every 17, including a soft 17. For the stricter "hit on soft 17" rule, change the loop in dealerTurn() so it also keeps drawing when score(dealer).total is 17 and the hand is soft.

finish() is the only place money moves. A win returns twice the bet, a blackjack returns 2.5 times the bet (3 to 2 profit), a push returns the bet, and a loss returns nothing. Because the bet is taken when you deal or double, those returns keep the bankroll exact. Chips are multiples of 10, so 3 to 2 never creates a fraction.

It is a push and your bet is returned. When the dealer shows an Ace or a ten-value card, the hole card is peeked at straight after the deal, so a dealer blackjack ends the round immediately.

The hole card has a front and a back with backface-visibility hidden, and the card-inner element rotates 180 degrees on the Y axis when the down class is removed. For dealing, addCard() uses FLIP: it measures the existing cards, adds the new one, and animates each card from its old position, and the new card from the shoe, with the Web Animations API.

buildShoe() creates six decks and applies the Fisher-Yates shuffle, swapping each card with a randomly chosen card at or before it. That is the standard way to get an unbiased permutation. Sorting with Math.random() is not uniform.

Yes. The round is split into named functions, so splitting would mean holding an array of hands instead of one, giving each its own bet, and running doHit and dealerTurn once per hand. Insurance is a side bet offered when the dealer shows an Ace, settled in afterDeal().

Copy & paste

Source Code

<div class="bj" id="bj" data-phase="bet">
  <header class="bj-top">
    <div class="bj-brand"><span class="bj-brand-chip"></span><b>Blackjack</b><small>vs the dealer</small></div>
    <div class="bj-stats">
      <div class="bj-bank"><span>Chips</span><b id="bj-bank">$1,000</b></div>
      <div class="bj-rec" title="Wins, losses and pushes"><span id="bj-w">0</span>W <span id="bj-l">0</span>L <span id="bj-p">0</span>P</div>
      <button type="button" class="bj-reset" id="bj-reset" aria-label="Reset chips and stats" title="Reset chips and stats">&#8634;</button>
    </div>
  </header>

  <div class="bj-table" id="bj-table">
    <svg class="bj-arcs" viewBox="0 0 600 160" aria-hidden="true" preserveAspectRatio="xMidYMid meet">
      <defs><path id="bjArcA" d="M80 62 Q300 12 520 62"/><path id="bjArcB" d="M120 112 Q300 150 480 112"/></defs>
      <text><textPath href="#bjArcA" startOffset="50%" text-anchor="middle">BLACKJACK PAYS 3 TO 2</textPath></text>
      <text class="sm"><textPath href="#bjArcB" startOffset="50%" text-anchor="middle">DEALER STANDS ON ALL 17s</textPath></text>
    </svg>

    <div class="bj-shoe" id="bj-shoe" aria-hidden="true"><div class="bj-shoe-stack"><i></i><i></i><i></i></div><span id="bj-shoe-n">312</span></div>

    <section class="bj-seat bj-dealer" aria-label="Dealer">
      <div class="bj-label">Dealer <span class="bj-badge" id="bj-dealer-total"></span></div>
      <div class="bj-hand" id="bj-dealer-hand" role="group" aria-label="Dealer hand"></div>
    </section>

    <div class="bj-center">
      <p class="bj-msg" id="bj-msg" role="status" aria-live="polite">Place your bet</p>
      <p class="bj-hint" id="bj-hint" hidden></p>
    </div>

    <section class="bj-seat bj-player" aria-label="Player">
      <div class="bj-hand" id="bj-player-hand" role="group" aria-label="Your hand"></div>
      <div class="bj-label">You <span class="bj-badge" id="bj-player-total"></span></div>
    </section>

    <div class="bj-bet" aria-label="Current bet"><div class="bj-bet-stack" id="bj-bet-stack"></div><span class="bj-bet-amt" id="bj-bet-amt">$0</span></div>
    <span class="bj-float" id="bj-float" aria-hidden="true"></span>
  </div>

  <div class="bj-controls">
    <div class="bj-bets" role="group" aria-label="Place your bet">
      <button type="button" class="bj-chip c10" data-chip="10" aria-label="Add $10"><span>10</span></button>
      <button type="button" class="bj-chip c50" data-chip="50" aria-label="Add $50"><span>50</span></button>
      <button type="button" class="bj-chip c100" data-chip="100" aria-label="Add $100"><span>100</span></button>
      <button type="button" class="bj-chip c500" data-chip="500" aria-label="Add $500"><span>500</span></button>
      <button type="button" class="bj-btn bj-ghost" id="bj-clear">Clear</button>
      <button type="button" class="bj-btn bj-gold" id="bj-deal" title="Deal (D)">Deal</button>
    </div>
    <div class="bj-play" role="group" aria-label="Your move">
      <button type="button" class="bj-btn" id="bj-hit" title="Hit (H)">Hit</button>
      <button type="button" class="bj-btn" id="bj-stand" title="Stand (S)">Stand</button>
      <button type="button" class="bj-btn" id="bj-double" title="Double down (D)">Double</button>
      <button type="button" class="bj-btn bj-ghost" id="bj-hintbtn" title="Basic strategy hint (?)">Hint</button>
    </div>
    <div class="bj-after" role="group" aria-label="Next hand">
      <button type="button" class="bj-btn bj-gold" id="bj-again" title="Deal again (D)">Deal again</button>
      <button type="button" class="bj-btn bj-ghost" id="bj-change">Change bet</button>
    </div>
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

Paste this snippet's JS into an AI assistant like Claude and ask it to trace score() with Ace, Ace and 9 to confirm it reads a soft 21 and not two Aces counted as 11, and to explain why finish() pays 2.5 times the bet for a blackjack. Then try the extensions the code does not have: splitting a pair into two hands with a bet each, an insurance side bet when the dealer shows an Ace, a hit-on-soft-17 table rule switch, a Hi-Lo card-counting trainer that shows the running and true count, sound effects with the Web Audio API, or a simulation mode that plays 10,000 hands with basic strategy and reports the house edge.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a casino-style single-player Blackjack game in plain HTML, CSS and JavaScript, with no libraries and no image files.

Requirements:
- Table: a felt table with a wooden rail, curved printed text ("BLACKJACK PAYS 3 TO 2", "DEALER STANDS ON ALL 17s"), a dealer hand at the top, a message line in the middle, your hand below it, a shoe in the corner with a card counter, and a bet circle.
- Cards drawn with HTML and CSS only: a corner index at the top left and, rotated 180 degrees, at the bottom right; pips for 2 to 10 placed from a table of column and row positions with the lower half upside down; a large suit for the Ace; a framed letter for J, Q and K; red and black suits; a patterned back. The dealer's hole card has a front and back (backface-visibility: hidden) and flips in 3D when revealed.
- Shoe: six decks shuffled with Fisher-Yates, reshuffled when fewer than 78 cards remain, with the count shown.
- Scoring: a score(hand) function that counts Aces as 11 and demotes them one at a time while the total is over 21, returning the best total and whether the hand is soft. Show the total in a badge ("Soft 17", "Bust", "Blackjack").
- Betting: a $1,000 bankroll, chips of 10, 50, 100 and 500 drawn as round casino chips, a stacked bet display and a counter that animates when it changes. Payouts in one function: win returns 2x the bet, blackjack returns 2.5x, push returns 1x, loss returns 0. Save chips and the win/loss/push record to localStorage in a try/catch, with a two-press reset.
- Round: deal alternating cards (dealer's second face down), peek for dealer blackjack under an Ace or ten, push if both have blackjack, hit, stand, and double down on the first two cards (double the bet, take exactly one card). A bust ends the round immediately. The dealer reveals the hole card and draws to 16, standing on all 17s.
- Animation: each dealt card flies in from the shoe with the Web Animations API, and the cards already in the hand slide to their new positions with a FLIP technique. Show a floating "+$150" or "-$50" after each round. Respect prefers-reduced-motion.
- Hint: a button that computes basic strategy for six decks (hard totals, soft totals, doubling when allowed) from the player's hand and the dealer's up card, shows the move with a one-sentence reason, and highlights the matching button. It must not play for the user.
- Keyboard and accessibility: H hit, S stand, D double or deal, ? hint; a live region for messages; aria-labels on cards ("Queen of hearts") and on the hands; the hole card labelled "Face-down card" until revealed.
- Start a first-time visitor with a hand already dealt (Queen and 5 against a 7) so there is something to play immediately. Make it responsive, with smaller cards under 600px.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.