Skip to content

samuelj1519/go-blackjack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

19 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿƒ Intelligent Blackjack Game

๐ŸŒ Language / ่ฏญ่จ€้€‰ๆ‹ฉ: English | ไธญๆ–‡

A fully-featured blackjack game written in Go, integrated with Monte Carlo probability analysis, Kelly Criterion bankroll management, and intelligent decision recommendation systems.

โœจ Core Features

๐ŸŽฏ Intelligent Probability Analysis

  • Real-time probability calculation: Based on Monte Carlo simulation (10,000 trials) to calculate winning probabilities
  • Action win rate comparison: Analyzes expected win rates for hit, stand, double down, and other actions
  • Optimal strategy recommendation: Automatically recommends the best decision based on basic strategy

๐Ÿ’ฐ Kelly Criterion Bankroll Management

  • Intelligent betting suggestions: Provides scientific betting amount recommendations based on bankroll status
  • Risk assessment: Real-time evaluation of current bankroll status and risk level
  • Double down decision analysis: Evaluates expected ROI and risk-reward ratio for doubling down

๐Ÿง  Decision Support System

  • Basic strategy integration: Built-in professional blackjack basic strategy
  • Real-time data analysis: Displays key indicators like bust probability, 21-point probability, etc.
  • Entertainment cost estimation: Helps players understand expected entertainment costs

๐Ÿš€ Quick Start

Requirements

  • Go 1.22+
  • UTF-8 compatible terminal

Install Dependencies

go mod tidy

Run the Game

# Method 1: Run directly
go run ./cmd

# Method 2: Build and run
go build -o blackjack ./cmd
./blackjack

๐ŸŽฎ Game Controls

Basic Actions

  • h / hit - Hit (take a card)
  • s / stand - Stand
  • d / double / doubledown - Double down
  • q / quit - Quit game
  • y / yes - Continue game
  • n / no - End game

Menu Options

  • 1 - Start game
  • 2 - View game rules
  • 3 - Exit program

๐Ÿƒ Game Rules

๐ŸŽฏ Game Objective

Get your hand as close to 21 as possible without going over, while beating the dealer's hand.

๐ŸŽด Card Values

  • Number cards (2-10): Face value
  • Face cards (J,Q,K): 10 points each
  • Ace: Intelligently calculated as 1 or 11 (whichever is more favorable)

๐Ÿ’ฐ Betting System

  • Starting chips: 1000
  • Betting options: 10, 25, 50, 100, 200 chips
  • Smart betting: Automatic all-in option when chips are insufficient
  • Payout rules:
    • ๐Ÿ† Regular win: 1:1
    • ๐ŸŒŸ Blackjack win: 3:2 (non-double situations)
    • ๐Ÿค Push: Return original amount

โšก Double Down Feature

  • Trigger condition: Available on first two cards
  • Chip requirement: Current chips โ‰ฅ current bet amount
  • Game rule: Can only take one more card after doubling
  • Payout adjustment: Blackjack pays 1:1 after doubling

๐ŸŽฒ Game Flow

  1. Betting phase โ†’ Choose bet amount + bankroll management advice
  2. Dealing phase โ†’ Player and dealer each get 2 cards (dealer has 1 hidden card)
  3. Player turn โ†’ Choose hit/stand/double + probability analysis
  4. Dealer turn โ†’ Dealer follows automatic rules
  5. Settlement phase โ†’ Compare points and settle chips

๐Ÿ“Š Intelligent Analysis System

๐ŸŽฏ Real-time Probability Analysis

The game displays detailed probability analysis each turn:

๐Ÿ“Š Current Win Probability Analysis
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐ŸŸข Player Win Probability: 45.6%
๐Ÿ”ด Dealer Win Probability: 16.7%
๐ŸŸก Push Probability:       37.6%

๐Ÿ“ˆ Detailed Analysis:
   ๐Ÿ’ฅ Player Bust Probability: 85.1%
   ๐Ÿ’ฅ Dealer Bust Probability: 22.7%
   ๐ŸŽฏ Player 21 Probability: 8.5%
   ๐ŸŽฏ Dealer 21 Probability: 6.3%

๐ŸŽฏ Action Win Rate Comparison:
   โœ‹ Stand: 45.8% โญ (Recommended)
   ๐Ÿ‘† Hit: 41.9%
   โšก Double: 36.3%

๐Ÿ’ฐ Kelly Criterion Bankroll Management

Betting Phase Advice

๐Ÿ’ฐ Bankroll Management Advice:
๐Ÿ“Š Recommended Bet: 15 chips (1.5% of bankroll)
๐Ÿ’ก Your bankroll status is good, moderate betting recommended
๐ŸŸข Risk Status: Sufficient funds, controllable risk
๐ŸŽฎ Expected Entertainment Cost: 0.06% per hand

Double Down Decision Analysis

๐Ÿ’ฐ Kelly Criterion Double Analysis:
โšก Recommend Double (Expected ROI: 18.0%)
๐Ÿ”ด Double Risk Level: High (Kelly fraction: 0.180)

๐Ÿงฎ Tiered Bankroll Management Strategy

Bankroll Level Recommended % Risk Level Strategy Description
โ‰ฅ 1000 chips 1.5% ๐ŸŸข Low Sufficient funds, moderate betting
โ‰ฅ 500 chips 1.0% ๐ŸŸข Low Conservative betting, risk control
โ‰ฅ 200 chips 0.5% ๐ŸŸก Medium More conservative, minimum betting
< 200 chips Minimum ๐Ÿ”ด High Recommend caution or leave game

๐Ÿง  Probability Calculation Principles

๐ŸŽฒ Monte Carlo Simulation Method

The game uses Monte Carlo simulation to calculate various probabilities:

  1. Simulation count: 10,000 simulations per analysis
  2. Strategy simulation: Player uses basic strategy, dealer follows fixed rules
  3. Remaining deck: Based on current known cards and remaining deck
  4. Statistical results: Statistics on frequency of various outcomes

๐Ÿ“ˆ Advantages Over Direct Calculation

Aspect Monte Carlo Simulation Direct Calculation
Complexity Management โœ… Handles complex strategies simply โŒ Combinatorial explosion
Strategy Integration โœ… Naturally integrates basic strategy โŒ Difficult to handle strategy changes
Extensibility โœ… Easy to add new rules โŒ Need to rewrite calculation logic
Multi-variable Handling โœ… Naturally handles multiple factors โŒ Complexity explodes with dimensions
Dynamic Adaptation โœ… Automatically adapts to deck changes โŒ Need to re-derive formulas

๐Ÿ’ก Kelly Criterion Application

Basic Formula

f* = (bp - q) / b
  • f*: Optimal betting fraction
  • b: Odds (1:1 or 3:2)
  • p: Win probability
  • q: Loss probability

Practical Application Strategy

  1. Conservative factor: Use 25% of Kelly fraction to reduce risk
  2. Tiered management: Different strategies based on bankroll status
  3. Entertainment-oriented: Focus on bankroll management rather than strict expected returns

๐Ÿ—๏ธ Architecture Design

๐Ÿ“ Project Structure

go-blackjack/
โ”œโ”€โ”€ cmd/                         # ๐Ÿš€ Program entry
โ”‚   โ””โ”€โ”€ main.go
โ”œโ”€โ”€ internal/                    # ๐Ÿ”’ Internal modules
โ”‚   โ”œโ”€โ”€ domain/                  # ๐ŸŽฏ Domain layer - Core business logic
โ”‚   โ”‚   โ””โ”€โ”€ entities/
โ”‚   โ”‚       โ”œโ”€โ”€ game.go          # Game aggregate root
โ”‚   โ”‚       โ”œโ”€โ”€ player.go        # Player entity
โ”‚   โ”‚       โ”œโ”€โ”€ dealer.go        # Dealer entity
โ”‚   โ”‚       โ”œโ”€โ”€ card.go          # Card entity
โ”‚   โ”‚       โ”œโ”€โ”€ deck.go          # Deck entity
โ”‚   โ”‚       โ”œโ”€โ”€ hand.go          # Hand entity
โ”‚   โ”‚       โ””โ”€โ”€ types.go         # Type definitions
โ”‚   โ”œโ”€โ”€ application/             # ๐Ÿ”„ Application layer - Use case orchestration
โ”‚   โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ game.go          # Game application service
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ probability.go   # Probability calculation service
โ”‚   โ”‚   โ””โ”€โ”€ dtos/
โ”‚   โ”‚       โ””โ”€โ”€ game.go          # Data transfer objects
โ”‚   โ””โ”€โ”€ interfaces/              # ๐Ÿ–ฅ๏ธ Interface layer - User interaction
โ”‚       โ””โ”€โ”€ cli/
โ”‚           โ”œโ”€โ”€ game.go          # CLI handler
โ”‚           โ””โ”€โ”€ display.go       # Display service
โ”œโ”€โ”€ go.mod                       # ๐Ÿ“ฆ Dependency management
โ”œโ”€โ”€ go.sum
โ””โ”€โ”€ README.md                    # ๐Ÿ“– Project documentation

๐ŸŽฏ Architecture Layers

Domain Layer

  • Responsibility: Core business logic and rules
  • Characteristics: No external dependencies, pure business code
  • Contains: Game entities, business rules, state management
// Game aggregate root - Unified game state management
type Game struct {
    ID          string
    Player      *Player
    Dealer      *Dealer
    Deck        *Deck
    State       GameState // State machine management
    RoundNumber int
}

Application Layer

  • Responsibility: Use case orchestration, domain object coordination
  • Characteristics: Handles business processes, data transformation
  • Contains: Application services, probability calculation, DTO objects
// Game application service - Orchestrates game use cases
type GameApplicationService struct {
    game            *entities.Game
    probabilityCalc *ProbabilityCalculator
}

// Probability calculator - Monte Carlo simulation
type ProbabilityCalculator struct {
    trials int // Number of simulations
    rng    *rand.Rand
}

Interface Layer

  • Responsibility: User interaction, input/output handling
  • Characteristics: Extensible for multiple interface implementations
  • Contains: CLI handler, display service
// CLI game handler
type GameHandler struct {
    gameService *services.GameApplicationService
    display     *DisplayService
}

๐Ÿ”„ Data Flow

User Input โ†’ CLI Handler โ†’ Application Service โ†’ Probability Calculator
                    โ†“                              โ†“
Domain Entities โ† Application Service โ† Monte Carlo Simulation
                    โ†“
CLI Display โ† Kelly Formula Analysis

๐Ÿงช Core Features

๐ŸŽฎ State Machine Management

type GameState int
const (
    StateWaitingToBet GameState = iota
    StatePlayerTurn
    StateDealerTurn
    StateGameOver
)

๐Ÿƒ Intelligent Card Value Calculation

Automatically calculates optimal Ace value (1 or 11) to ensure best possible hand value.

๐Ÿ“Š Probability Analysis Engine

type ProbabilityResult struct {
    PlayerWinProbability  float64
    DealerWinProbability  float64
    PlayerBustProbability float64
    ActionAnalysis        *ActionAnalysis
}

๐Ÿ’ฐ Kelly Criterion Decision Making

type KellyRecommendation struct {
    RecommendedBetAmount   int
    RecommendedBetFraction float64
    ShouldDouble           bool
    RiskLevel              string
}

๐Ÿ’ก Business Rule Validation

Strict business rule validation in the domain layer ensures game logic correctness.

๐Ÿ”ง Extensible Design

  • Multi-interface support: Easy to add web, mobile interfaces
  • Feature extension: Easy to add split, surrender features
  • Algorithm optimization: Supports different probability calculation methods

๐ŸŽฏ Game Screenshots Examples

Betting Phase - Bankroll Management Advice

๐Ÿ’ฐ Current Chips: 1000
Please select bet amount:
1. 10 chips
2. 25 chips
3. 50 chips
4. 100 chips
5. 200 chips
6. Exit game

๐Ÿ’ฐ Bankroll Management Advice:
๐Ÿ“Š Recommended Bet: 15 chips (1.5% of bankroll)
๐Ÿ’ก Your bankroll status is good, moderate betting recommended
๐ŸŸข Risk Status: Sufficient funds, controllable risk
๐ŸŽฎ Expected Entertainment Cost: 0.06% per hand

Game Round - Intelligent Analysis

๐Ÿ‘จ Dealer Hand (first card hidden):
๐Ÿ‚  ๐ŸƒA

๐Ÿ‘จ Player Hand (Value: 9):
๐Ÿƒ5 ๐Ÿƒ4

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐Ÿ“Š Current Win Probability Analysis
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐ŸŸข Player Win Probability: 59.1%
๐Ÿ”ด Dealer Win Probability: 34.0%
๐ŸŸก Push Probability:       6.8%

๐Ÿ“ˆ Detailed Analysis:
   ๐Ÿ’ฅ Player Bust Probability: 0.0%
   ๐Ÿ’ฅ Dealer Bust Probability: 44.4%
   ๐ŸŽฏ Player 21 Probability: 0.0%
   ๐ŸŽฏ Dealer 21 Probability: 8.7%

๐ŸŽฏ Action Win Rate Comparison:
   โœ‹ Stand: 44.2%
   ๐Ÿ‘† Hit: 56.4%
   โšก Double: 59.0% โญ (Recommended)

๐Ÿ† Optimal Strategy Expected Win Rate: 59.0%

๐Ÿ’ฐ Kelly Criterion Double Analysis:
   โšก Recommend Double (Expected ROI: 18.0%)
   ๐Ÿ”ด Double Risk Level: High (Kelly fraction: 0.180)
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

๐ŸŽฏ Future Plans

Short-term Goals

  • ๐Ÿงช Complete unit test coverage
  • ๐Ÿ“ˆ Add historical statistics feature
  • ๐ŸŽฎ Implement split functionality
  • ๐Ÿ”„ Add surrender option

Medium-term Goals

  • ๐ŸŒ Develop web interface version
  • ๐Ÿ’พ Implement game data persistence
  • ๐Ÿ“ฑ Mobile adaptation
  • ๐Ÿค– AI opponent mode

Long-term Goals

  • ๐Ÿ† Multiplayer functionality
  • ๐Ÿ“Š Detailed statistical reports
  • ๐ŸŽฏ Custom rule settings
  • ๐ŸŒ Internationalization support

๐Ÿค Contributing

Issues and Pull Requests are welcome!

Development Environment Setup

git clone https://github.com/yourusername/go-blackjack.git
cd go-blackjack
go mod tidy
go test ./...

Code Style

  • Follow Go official coding standards
  • Use meaningful variable and function names
  • Add appropriate comments and documentation

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details


โญ If this project helps you, please give us a star!

About

A fully-featured blackjack game written in Go, integrated with Monte Carlo probability analysis, Kelly Criterion bankroll management, and intelligent decision recommendation systems.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors