BGFParser
A Go package for parsing BGBlitz backgammon data files, including SMILE-encoded binary formats.
Overview
BGFParser is a comprehensive library for reading and analyzing backgammon data from BGBlitz, a popular backgammon analysis software. The package supports two file formats:
- TXT files: Human-readable position files containing board states, evaluations, and analysis
- BGF files: Binary match files using compressed JSON (with optional SMILE encoding)
Features
TXT Parser
- β
Parse board positions with checker locations
- β
Extract player names and scores
- β
Read match information (length, Crawford state)
- β
Parse position identifiers (Position-ID, Match-ID, XGID)
- β
Extract dice rolls and cube information
- β
Parse move evaluations with equity and statistics
- β
Read cube decisions (Double/Take, Double/Pass, No Double)
- β
Support for multiple languages (English, French)
- β
Pip count extraction
BGF Parser
- β
Full SMILE encoding/decoding support
- Complete match data extraction
- Player names, ratings, and statistics
- Move-by-move analysis with equity calculations
- Cube decisions and match equity
- Performance ratings and error analysis
- All nested objects and arrays
Web-Ready API π
- β
Parse from files, HTTP uploads, memory buffers, or any
io.Reader
- β
JSON serializable structures for easy API responses
- β
No file system required - parse directly from uploaded data
- β
Database-ready structures with JSON tags
Installation
go get github.com/kevung/bgfparser
Quick Start
Parse from File
package main
import (
"fmt"
"log"
"github.com/kevung/bgfparser"
)
func main() {
// Parse a position file
position, err := bgfparser.ParseTXT("game_position.txt")
if err != nil {
log.Fatal(err)
}
// Access position data
fmt.Printf("Players: %s vs %s\n", position.PlayerX, position.PlayerO)
fmt.Printf("Score: %d-%d\n", position.ScoreX, position.ScoreO)
fmt.Printf("On Roll: %s with %d-%d\n",
position.OnRoll, position.Dice[0], position.Dice[1])
// Check evaluations
if len(position.Evaluations) > 0 {
best := position.Evaluations[0]
fmt.Printf("Best move: %s (Equity: %.3f)\n",
best.Move, best.Equity)
}
}
Parse from HTTP Upload π
import (
"net/http"
"encoding/json"
"github.com/kevung/bgfparser"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
// Get uploaded file
file, _, err := r.FormFile("bgffile")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
// Parse directly from upload (no temp file needed!)
match, err := bgfparser.ParseBGFFromReader(file)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Return JSON response
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(match)
}
Parse from Memory π
import (
"bytes"
"github.com/kevung/bgfparser"
)
func parseFromMemory(data []byte) (*bgfparser.Match, error) {
reader := bytes.NewReader(data)
return bgfparser.ParseBGFFromReader(reader)
}
Export to JSON π
// Parse any file
pos, _ := bgfparser.ParseTXT("position.txt")
// Get JSON output
jsonData, err := pos.ToJSON()
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
Web Server Example π
A complete web server is provided in examples/web_server/:
cd examples/web_server
go run main.go
Then visit http://localhost:8080 to upload and analyze BGF/TXT files through a web interface.
API Overview
File-Based Parsing
// Parse TXT position file
pos, err := bgfparser.ParseTXT("position.txt")
// Parse BGF match file
match, err := bgfparser.ParseBGF("match.bgf")
Web-Ready Parsing (io.Reader) π
// Parse from any io.Reader source
match, err := bgfparser.ParseBGFFromReader(reader)
pos, err := bgfparser.ParseTXTFromReader(reader)
// Works with:
// - HTTP file uploads (multipart.File)
// - Memory buffers (bytes.Reader)
// - Network streams (net.Conn)
// - Any io.Reader implementation
JSON Export π
### Export to JSON π
```go
// Export to JSON
jsonData, err := match.ToJSON()
jsonData, err := pos.ToJSON()
The package includes several CLI tools for parsing BGF and TXT files:
# Build all tools
go build -o bin/parse_txt ./examples/parse_txt/
go build -o bin/parse_bgf ./examples/parse_bgf/
go build -o bin/batch_parse ./examples/batch_parse/
go build -o bin/web_server ./examples/web_server/
# Or build individually
cd examples/parse_txt && go build
cd examples/parse_bgf && go build
cd examples/batch_parse && go build
cd examples/web_server && go build
parse_txt - Parse TXT Position Files
Parse and display information from a BGBlitz TXT position file.
# Usage
./bin/parse_txt <filename.txt>
# Example
./bin/parse_txt tmp/blunder21_EN.txt
# Output shows:
# - Player names and scores
# - Match length and Crawford status
# - Position on roll and dice
# - Cube value and owner
# - Move evaluations with equity
# - Cube decisions
# - XGID and Position IDs
parse_bgf - Parse BGF Match Files
Parse and display information from a BGBlitz BGF binary match file.
# Usage
./bin/parse_bgf <filename.bgf>
# Example
./bin/parse_bgf tmp/TachiAI_V_player_Nov_2__2025__16_55.bgf
# Output shows:
# - File format and version
# - Compression status
# - SMILE encoding detection
# - Match metadata (players, dates, scores)
# - Number of decoded fields
# - Game data (if available)
batch_parse - Process Multiple Files
Parse all BGF and TXT files in a directory.
# Usage
./bin/batch_parse <directory>
# Example
./bin/batch_parse tmp/
# Processes:
# - All .txt files (position data)
# - All .bgf files (match data)
# - Shows summary for each file
web_server - Web Interface
Run a web server with upload interface for analyzing BGF and TXT files.
# Start server
./bin/web_server
# Or run directly
cd examples/web_server && go run main.go
# Server starts on http://localhost:8080
# Upload BGF/TXT files through web interface
# Get JSON responses with parsed data
API Endpoints:
POST /upload/bgf - Upload and parse BGF file
POST /upload/txt - Upload and parse TXT file
GET /health - Health check
Running Without Building
You can also run tools directly with go run:
# Parse TXT file
go run examples/parse_txt/main.go tmp/blunder21_EN.txt
# Parse BGF file
go run examples/parse_bgf/main.go tmp/match.bgf
# Batch process directory
go run examples/batch_parse/main.go tmp/
# Start web server
go run examples/web_server/main.go
Use Cases
- Web Applications: Parse uploaded BGF/TXT files in HTTP handlers
- REST APIs: Provide backgammon position analysis endpoints
- Database Storage: Import positions and matches into SQL/NoSQL databases
- Batch Processing: Analyze collections of match/position files
- Match Servers: Real-time position analysis services
- Statistical Analysis: Extract move quality, equity, and win rates
Documentation
## Data Types
### Position
Represents a backgammon position with complete state:
```go
type Position struct {
Board [26]int // Checker positions (0=unused, 1-24=points, 25=bar)
PlayerX string // Player X name
PlayerO string // Player O name
ScoreX int // Player X score
ScoreO int // Player O score
MatchLength int // Match length in points
Crawford bool // Crawford rule active
PositionID string // BGBlitz Position-ID
MatchID string // BGBlitz Match-ID
XGID string // XG format ID
OnRoll string // "X" or "O"
Dice [2]int // Current dice roll
CubeValue int // Doubling cube value
CubeOwner string // "", "X", or "O"
OnBar map[string]int // Checkers on bar
PipCount map[string]int // Pip counts
Evaluations []Evaluation // Move evaluations
CubeDecision *CubeDecision // Cube decision analysis
}
Evaluation
Move evaluation with equity and statistics:
type Evaluation struct {
Rank int // Move rank (1 = best)
Move string // Move notation
Equity float64 // Equity value
Diff float64 // Difference from best
Win float64 // Win probability
WinG float64 // Gammon win probability
WinBG float64 // Backgammon win probability
LoseG float64 // Gammon loss probability
LoseBG float64 // Backgammon loss probability
IsBest bool // True if marked as best move
}
CubeDecision
Cube action analysis:
type CubeDecision struct {
Action string // "Double/Take", "Double/Pass", or "No Double"
MWC float64 // Match Winning Chances
MWCDiff float64 // Difference in MWC
EMG float64 // EMG (normalized equity)
EMGDiff float64 // Difference in EMG
IsBest bool // True if marked as best action
}
Match
BGF match file data:
type Match struct {
Format string // File format ("BGF")
Version string // Format version
Compressed bool // Gzip compression used
UseSmile bool // SMILE encoding used
Data map[string]interface{} // Parsed match data
}
Examples
The package includes example programs in the examples/ directory:
1. parse_txt
Detailed parser for TXT position files showing all available data.
go run examples/parse_txt/main.go tmp/blunder21_EN.txt
2. parse_bgf
Parser for BGF match files with format detection.
go run examples/parse_bgf/main.go tmp/match.bgf
3. batch_parse
Batch processor for parsing all files in a directory.
go run examples/batch_parse/main.go tmp/
4. web_server π
Complete web server with file upload interface.
cd examples/web_server
go run main.go
# Visit http://localhost:8080
Text files contain ASCII art representation of the board plus analysis:
+13-14-15-16-17-18------19-20-21-22-23-24-+ O: TachiAI_V 161
| X O | | O O X |
| X O | | O O X |
...
Position-ID: mGfwATDgc/ABMA Match-ID: cAllAAAAAAAE
XGID=-b----E-C---eE---b-d-b--B-:0:0:1:21:0:0:0:3:10
TachiAI_V - 0 player - 0 in a 3 point match.
player to move 2-1
Evaluation
==========
1) 13-11 24-23 0.473 / -0.289
0.443 0.113 0.002 - 0.557 0.179 0.003
The parser extracts:
- Board state (visual representation)
- Player information
- Match state and score
- Position identifiers
- Move evaluations with equity values
- Win/loss probabilities
- Cube decisions
Binary files with two parts:
- Header (JSON, uncompressed):
{"format":"BGF","version":"1.0","compress":true,"useSmile":true}
- Data (gzip + SMILE encoded JSON):
- Complete match data
- All positions
- Move history
- Analysis results
Note: Full SMILE decoding support is implemented. The parser successfully decodes all SMILE-encoded BGF files including complete match data, move analysis, equity calculations, and performance statistics.
Project Structure
bgfparser/
βββ LICENSE # MIT License
βββ README.md # This file
βββ go.mod # Go module definition
βββ types.go # Core data types (JSON-serializable)
βββ txt_parser.go # TXT format parser (file-based)
βββ bgf_parser.go # BGF format parser (file-based)
βββ web.go # Web-ready parsers (io.Reader) π
βββ txt_parser_helpers.go # TXT parsing utilities π
βββ doc/
β βββ API_REFERENCE.md # Complete API reference
β βββ WEB_INTERFACE.md # Web integration guide π
β βββ BGF_format.md # BGF format specification
βββ examples/
β βββ parse_txt/ # TXT parser example
β βββ parse_bgf/ # BGF parser example
β βββ batch_parse/ # Batch processing example
β βββ web_server/ # Web server example π
βββ tmp/ # Sample data files
βββ *.txt # Position files
βββ *.bgf # Match files
Testing
Run the batch parser on the sample data:
# Build examples
go build -o bin/parse_txt ./examples/parse_txt/
go build -o bin/parse_bgf ./examples/parse_bgf/
go build -o bin/batch_parse ./examples/batch_parse/
# Test on sample data
./bin/batch_parse tmp/
Limitations and Future Work
Current Limitations
Planned Improvements
- More comprehensive test suite
- Performance optimizations for batch processing
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for:
- Bug fixes
- Additional features
- Documentation improvements
- Test cases
- Language support
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Frank Berger for creating BGBlitz, the backgammon analysis software
- LeLuxNet for the SMILE decoder implementation
Support
For questions, issues, or suggestions:
- Open an issue on GitHub
- Check the
doc/ directory for format specifications
- Review the
examples/ directory for usage patterns