-
Notifications
You must be signed in to change notification settings - Fork 14
Position
The Position type represents a complete chess position state, including piece placement, castling rights, en passant possibilities, move counts, and the side to move. It supports FEN notation and provides methods for position manipulation and validation.
type Position struct {
board *Board // Current board state
castleRights CastleRights // Available castling options
validMoves []Move // Cache of legal moves
halfMoveClock int // Half-move counter
moveCount int // Full move counter
turn Color // Side to move
enPassantSquare Square // En passant target square
inCheck bool // Whether current side is in check
}The CastleRights type manages castling availability:
type CastleRights string // FEN format (e.g., "KQkq")
type Side int
const (
KingSide Side = iota + 1 // Kingside castle
QueenSide // Queenside castle
)Create positions using predefined functions:
// Standard starting position
pos := StartingPosition()
// From FEN string
pos, err := decodeFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")The position can be updated through various methods:
// Apply a move
newPos := pos.Update(move)
// Change turn
newPos := pos.ChangeTurn()// Get valid moves
moves := pos.ValidMoves()
// Check position status
method := pos.Status()
// Access current board state
board := pos.Board()The position maintains the board state including:
- Piece positions
- Side to move
- Castling availability
- En passant targets
The position handles move validation through:
- Valid move generation
- Check detection
- Castle rights verification
- En passant validation
Two separate counters are maintained:
- Half-move clock (fifty-move rule)
- Full move counter
The Update method handles position updates including:
- Piece movement
- Castle rights updates
- En passant square updates
- Move clock management
Example:
newPos := pos.Update(move)Castling rights are updated based on:
- King moves
- Rook moves
- Rook captures
Example:
if pos.CastleRights().CanCastle(White, KingSide) {
// Can castle kingside
}En passant squares are tracked for:
- Pawn double moves
- En passant captures
The position implements comparison methods:
// Check if two positions are the same
same := pos.samePosition(otherPos)
// Generate unique position hash
hash := pos.Hash()The position supports FEN notation:
// Convert to FEN
fen := pos.String()
// Create from FEN
pos.UnmarshalText([]byte(fen))Binary serialization is supported:
// Convert to binary
data, err := pos.MarshalBinary()
// Create from binary
pos.UnmarshalBinary(data)-
Position Updates
- Always use
Updatefor position changes - Don't modify position fields directly
- Handle move validation properly
- Always use
-
Position Comparison
- Use
samePositionfor position comparison - Consider all relevant fields
- Handle move count appropriately
- Use
-
Move Validation
- Cache valid moves when possible
- Check castle rights before moves
- Validate en passant properly
-
Serialization
- Use FEN for human-readable format
- Use binary for efficient storage
- Handle all position components
-
Move Generation
- Iterator-based move generation
- Improved move validation
- Enhanced caching
-
Serialization
- Compressed binary format
- Alternative notation support
- Improved FEN parsing