Class: Sashite::Feen::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/feen/position.rb

Overview

Immutable representation of a complete board game position.

Combines piece placement, pieces in hand, and style-turn information into a single unified position object. This represents a complete snapshot of the game state at a given moment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(placement, hands, styles) ⇒ Position

Create a new immutable Position object.

Examples:

Create a chess starting position

position = Position.new(placement, hands, styles)

Parse from FEEN string

position = Sashite::Feen.parse("+rnbq+kbn+r/+p+p+p+p+p+p+p+p/8/8/8/8/+P+P+P+P+P+P+P+P/+RNBQ+KBN+R / C/c")

Parameters:

  • placement (Placement)

    Board configuration

  • hands (Hands)

    Captured pieces in hand

  • styles (Styles)

    Style-turn information



33
34
35
36
37
38
39
# File 'lib/sashite/feen/position.rb', line 33

def initialize(placement, hands, styles)
  @placement = placement
  @hands = hands
  @styles = styles

  freeze
end

Instance Attribute Details

#handsHands (readonly)

Returns Pieces held in hand by each player.

Returns:

  • (Hands)

    Pieces held in hand by each player



17
18
19
# File 'lib/sashite/feen/position.rb', line 17

def hands
  @hands
end

#placementPlacement (readonly)

Returns Board piece placement configuration.

Returns:

  • (Placement)

    Board piece placement configuration



14
15
16
# File 'lib/sashite/feen/position.rb', line 14

def placement
  @placement
end

#stylesStyles (readonly)

Returns Game styles and active player indicator.

Returns:

  • (Styles)

    Game styles and active player indicator



20
21
22
# File 'lib/sashite/feen/position.rb', line 20

def styles
  @styles
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare two positions for equality.

Parameters:

  • other (Position)

    Another position object

Returns:

  • (Boolean)

    True if all components are equal



60
61
62
63
64
65
# File 'lib/sashite/feen/position.rb', line 60

def ==(other)
  other.is_a?(Position) &&
    placement == other.placement &&
    hands == other.hands &&
    styles == other.styles
end

#hashInteger

Generate hash code for position.

Returns:

  • (Integer)

    Hash code based on all components



72
73
74
# File 'lib/sashite/feen/position.rb', line 72

def hash
  [placement, hands, styles].hash
end

#to_sString

Convert position to its canonical FEEN string representation.

Generates a deterministic FEEN string. The same position will always produce the same canonical string, enabling position equality via string comparison.

Examples:

position.to_s
# => "+rnbq+kbn+r/+p+p+p+p+p+p+p+p/8/8/8/8/+P+P+P+P+P+P+P+P/+RNBQ+KBN+R / C/c"

Returns:

  • (String)

    Canonical FEEN notation string



52
53
54
# File 'lib/sashite/feen/position.rb', line 52

def to_s
  Dumper.dump(self)
end