-
Notifications
You must be signed in to change notification settings - Fork 14
Game
The chess game implementation manages the complete state of a chess game, including move history, variations, game outcomes, and PGN/FEN support. The implementation allows for complex game trees with variations and provides mechanisms for game state traversal.
The Game struct is the central component that maintains the game state:
type Game struct {
pos *Position // Current position
outcome Outcome // Game result
tagPairs TagPairs // PGN tag pairs
rootMove *Move // Root of move tree
currentMove *Move // Current position in tree
comments [][]string // Game comments
method Method // How the game ended
ignoreAutomaticDraws bool // Flag for automatic draw handling
}Games can end with the following outcomes:
-
NoOutcome: Game in progress -
WhiteWon: White victory -
BlackWon: Black victory -
Draw: Draw agreed
The game can end through various methods:
-
Checkmate: Victory by checkmate -
Resignation: Player resigns -
DrawOffer: Draw by agreement -
Stalemate: Draw by stalemate -
ThreefoldRepetition: Draw by position repetition -
FivefoldRepetition: Automatic draw -
FiftyMoveRule: Draw by fifty move rule -
SeventyFiveMoveRule: Automatic draw -
InsufficientMaterial: Draw by insufficient material
Create new games using the NewGame function:
// Default starting position
game := NewGame()
// From FEN string
game := NewGame(FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"))
// From PGN reader
game := NewGame(PGN(reader))The game supports a tree structure of moves with variations:
// Add a move
game.PushMove("e4", &PushMoveOptions{ForceMainline: true})
// Navigate moves
game.GoForward() // Move forward in current line
game.GoBack() // Move back one move
game.NavigateToMainLine() // Return to main line// Get current position
pos := game.Position()
// Get valid moves
moves := game.ValidMoves()
// Check game status
if game.Outcome() == NoOutcome {
// Game still in progress
}The game maintains a tree structure for moves and variations:
// Get main line moves
moves := game.Moves()
// Get variations at current position
variations := game.Variations(currentMove)
// Add variation
game.AddVariation(parentMove, newMove)The game tracks positions for:
- Current game state
- Move history
- Position repetition
- Draw detection
Automatic draws are detected for:
- Fivefold repetition
- Seventy-five move rule
- Insufficient material
Manual draws can be claimed for:
- Threefold repetition
- Fifty move rule
- Draw offer
// Check eligible draws
draws := game.EligibleDraws()
// Claim draw
game.Draw(ThreefoldRepetition)// Resignation
game.Resign(White)
// Check outcome
if game.Outcome() != NoOutcome {
method := game.Method()
// Handle game end
}PGN tag pairs can be managed:
// Add/update tag
game.AddTagPair("Event", "World Championship")
// Get tag value
event := game.GetTagPair("Event")
// Remove tag
game.RemoveTagPair("Event")The game supports standard chess formats:
// Get FEN string
fen := game.FEN()
// Get PGN
pgn := game.String()-
Move Management
- Use
PushMovefor adding moves - Handle variations explicitly
- Check move validity before adding
- Use
-
Game State
- Check game outcome before moves
- Handle automatic draws appropriately
- Verify position validity
-
Navigation
- Use proper navigation methods
- Check position before moves
- Maintain main line integrity
-
Draw Claims
- Verify draw eligibility
- Handle automatic draws
- Check position repetition
-
Move Handling
- No move timing information
- Limited annotation support
-
Position Analysis
- Basic draw detection
- No evaluation support
-
Game Format
- Limited PGN import/export
- Basic FEN support
Create deep copies of games:
// Clone game
newGame := game.Clone()Access full position history:
// Get all positions
positions := game.Positions()Complex move tree handling:
// Check position in tree
isStart := game.IsAtStart()
isEnd := game.IsAtEnd()
// Navigate tree
game.NavigateToMainLine()-
Move Management
- Enhanced variation support
- Improved move annotation
- Move timing support
-
Game Analysis
- Position evaluation
- Deep draw detection
- Tactical analysis
-
Format Support
- Enhanced PGN support
- Extended FEN capabilities
- Additional export formats