๐ 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.
- 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
- 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
- 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
- Go 1.22+
- UTF-8 compatible terminal
go mod tidy# Method 1: Run directly go run ./cmd # Method 2: Build and run go build -o blackjack ./cmd ./blackjackh/hit- Hit (take a card)s/stand- Standd/double/doubledown- Double downq/quit- Quit gamey/yes- Continue gamen/no- End game
1- Start game2- View game rules3- Exit program
Get your hand as close to 21 as possible without going over, while beating the dealer's hand.
- 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)
- 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
- 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
- Betting phase โ Choose bet amount + bankroll management advice
- Dealing phase โ Player and dealer each get 2 cards (dealer has 1 hidden card)
- Player turn โ Choose hit/stand/double + probability analysis
- Dealer turn โ Dealer follows automatic rules
- Settlement phase โ Compare points and settle chips
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% ๐ฐ 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 ๐ฐ Kelly Criterion Double Analysis: โก Recommend Double (Expected ROI: 18.0%) ๐ด Double Risk Level: High (Kelly fraction: 0.180) | 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 |
The game uses Monte Carlo simulation to calculate various probabilities:
- Simulation count: 10,000 simulations per analysis
- Strategy simulation: Player uses basic strategy, dealer follows fixed rules
- Remaining deck: Based on current known cards and remaining deck
- Statistical results: Statistics on frequency of various outcomes
| 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 |
f* = (bp - q) / b f*: Optimal betting fractionb: Odds (1:1 or 3:2)p: Win probabilityq: Loss probability
- Conservative factor: Use 25% of Kelly fraction to reduce risk
- Tiered management: Different strategies based on bankroll status
- Entertainment-oriented: Focus on bankroll management rather than strict expected returns
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 - 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 }- 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 }- 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 }User Input โ CLI Handler โ Application Service โ Probability Calculator โ โ Domain Entities โ Application Service โ Monte Carlo Simulation โ CLI Display โ Kelly Formula Analysis type GameState int const ( StateWaitingToBet GameState = iota StatePlayerTurn StateDealerTurn StateGameOver )Automatically calculates optimal Ace value (1 or 11) to ensure best possible hand value.
type ProbabilityResult struct { PlayerWinProbability float64 DealerWinProbability float64 PlayerBustProbability float64 ActionAnalysis *ActionAnalysis }type KellyRecommendation struct { RecommendedBetAmount int RecommendedBetFraction float64 ShouldDouble bool RiskLevel string }Strict business rule validation in the domain layer ensures game logic correctness.
- Multi-interface support: Easy to add web, mobile interfaces
- Feature extension: Easy to add split, surrender features
- Algorithm optimization: Supports different probability calculation methods
๐ฐ 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 ๐จ 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) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ - ๐งช Complete unit test coverage
- ๐ Add historical statistics feature
- ๐ฎ Implement split functionality
- ๐ Add surrender option
- ๐ Develop web interface version
- ๐พ Implement game data persistence
- ๐ฑ Mobile adaptation
- ๐ค AI opponent mode
- ๐ Multiplayer functionality
- ๐ Detailed statistical reports
- ๐ฏ Custom rule settings
- ๐ Internationalization support
Issues and Pull Requests are welcome!
git clone https://github.com/yourusername/go-blackjack.git cd go-blackjack go mod tidy go test ./...- Follow Go official coding standards
- Use meaningful variable and function names
- Add appropriate comments and documentation
This project is licensed under the MIT License - see the LICENSE file for details
โญ If this project helps you, please give us a star!