Blackjack in the terminal, written in C++17 with no external libraries.
Play against the dealer from a six-deck shoe, with ASCII-art cards, betting, doubling down and a built-in basic-strategy coach. Press ? during any hand to see the mathematically best move.
T W E N T Y - O N E chips 525 bet 25 shoe 296 cards
Dealer (23, bust)
+-----+ +-----+ +-----+ +-----+
|2 | |8 | |3 | |Q |
| ♦ | | ♦ | | ♠ | | ♣ |
| 2| | 8| | 3| | Q|
+-----+ +-----+ +-----+ +-----+
You (18)
+-----+ +-----+
|J | |8 |
| ♠ | | ♥ |
| J| | 8|
+-----+ +-----+
Dealer busts with 23! +25 chips.
- Get closer to 21 than the dealer without going over. Aces count as 1 or 11, face cards as 10.
- A blackjack (an ace and a ten-value card as your first two cards) pays 3:2.
- The dealer draws to 16 and stands on all 17s, including soft 17.
- Double down on your first two cards: double the bet, take exactly one more card, then stand.
- Six decks are shuffled together. The shoe is reshuffled once three quarters of it has been dealt.
- You start with 500 chips. The minimum bet is 10.
| Input | Action |
|---|---|
a number / Enter |
Place a bet (Enter repeats your last bet) |
h |
Hit: take another card |
s |
Stand: keep your hand |
d |
Double down |
? |
Ask the basic-strategy coach |
q |
Cash out and see your stats |
Linux, macOS or WSL
git clone https://github.com/BigB-904/blackjack-cpp.git
cd blackjack-cpp
make
./blackjack
./blackjack --ascii # use S/H/D/C if your terminal can't show ♠♥♦♣
./blackjack --seed 7 # replay the same shuffle
make test # unit tests plus a 200,000-hand simulationWindows (Visual Studio or any CMake setup)
cmake -S . -B build
cmake --build build --config Release
build\Release\blackjack.exeOr compile directly: g++ -std=c++17 -O2 src/*.cpp -o blackjack
src/
Cards.h / Cards.cpp Card, Shoe, Hand, settling, payouts, basic strategy
Terminal.h / Terminal.cpp ANSI colours and UTF-8 output on Windows and POSIX
main.cpp betting, the hand loop, card rendering, statistics
tests/
tests.cpp rules tests and a house-edge simulation
Counting aces. Hand first adds every ace as 1 (the hard total). Two aces counted as 11 would already make 22, so at most one ace can ever count as 11. That means the best value is simply the hard total plus 10, if that doesn't go over 21. When an ace is being counted as 11, the hand is soft.
Settling a hand. The order of the checks matters. A player who busts loses even if the dealer busts afterwards, which is the house's built-in advantage. A natural blackjack beats a three-card 21. Two naturals push.
The strategy coach. basicStrategy() encodes the standard decision chart for a multi-deck game where the dealer stands on soft 17. It treats hard and soft totals separately. For example, hit hard 16 against a dealer 10, stand on it against a 6, and double 11 against anything but an ace.
Proving it's fair. One of the unit tests plays 200,000 hands with the coach's advice and measures the result. The house edge comes out at about 1.15%, in line with real blackjack once splitting is left out. Rules, payouts and strategy all fit together correctly.
Rules separate from display. Everything in Cards.cpp is pure logic with no input or output, so the same code drives the game, the coach and the simulation.
- Splitting pairs, insurance and surrender
- A running card count (Hi-Lo) shown as an optional "card counter's view"
- Several players at the table, or computer players who follow the coach
- Save your chip balance between sessions