Class: Sashite::Feen::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/feen/position.rb,
lib/sashite/feen/position/hands.rb,
lib/sashite/feen/position/style_turn.rb,
lib/sashite/feen/position/piece_placement.rb

Overview

Represents a parsed FEEN (Field Expression Encoding Notation) position.

A Position encapsulates the three FEEN fields:

  • Piece Placement: Board occupancy

  • Hands: Off-board pieces held by each player

  • Style-Turn: Player styles and active player

Instances are immutable (frozen after creation).

Examples:

Creating a position

position = Sashite::Feen.parse("lnsgk^gsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGK^GSNL / S/s")
position.to_s  # => "lnsgk^gsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGK^GSNL / S/s"

See Also:

Defined Under Namespace

Classes: Hand, Hands, PiecePlacement, StyleTurn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(piece_placement:, hands:, style_turn:) ⇒ Position

Creates a new Position instance from parsed components.

Parameters:

  • piece_placement (Hash)

    Parsed piece placement with :segments and :separators

  • hands (Hash)

    Parsed hands with :first and :second

  • style_turn (Hash)

    Parsed style-turn with :active and :inactive



43
44
45
46
47
48
49
# File 'lib/sashite/feen/position.rb', line 43

def initialize(piece_placement:, hands:, style_turn:)
  @piece_placement = PiecePlacement.new(**piece_placement)
  @hands = Hands.new(**hands)
  @style_turn = StyleTurn.new(**style_turn)

  freeze
end

Instance Attribute Details

#handsHands (readonly)

Returns Off-board pieces.

Returns:

  • (Hands)

    Off-board pieces



32
33
34
# File 'lib/sashite/feen/position.rb', line 32

def hands
  @hands
end

#piece_placementPiecePlacement (readonly)

Returns Board occupancy.

Returns:



29
30
31
# File 'lib/sashite/feen/position.rb', line 29

def piece_placement
  @piece_placement
end

#style_turnStyleTurn (readonly)

Returns Player styles and active player.

Returns:

  • (StyleTurn)

    Player styles and active player



35
36
37
# File 'lib/sashite/feen/position.rb', line 35

def style_turn
  @style_turn
end

Instance Method Details

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

Checks equality with another Position.

Parameters:

  • other (Object)

    The object to compare

Returns:

  • (Boolean)

    true if equal



66
67
68
69
70
71
72
# File 'lib/sashite/feen/position.rb', line 66

def ==(other)
  return false unless self.class === other

  piece_placement == other.piece_placement &&
    hands == other.hands &&
    style_turn == other.style_turn
end

#hashInteger

Returns a hash code for the Position.

Returns:

  • (Integer)

    Hash code



79
80
81
# File 'lib/sashite/feen/position.rb', line 79

def hash
  [piece_placement, hands, style_turn].hash
end

#inspectString

Returns an inspect string for the Position.

Returns:

  • (String)

    Inspect representation



86
87
88
# File 'lib/sashite/feen/position.rb', line 86

def inspect
  "#<#{self.class} #{self}>"
end

#to_sString

Returns the canonical FEEN string representation.

Returns:

  • (String)

    The FEEN string



54
55
56
57
58
59
60
# File 'lib/sashite/feen/position.rb', line 54

def to_s
  [
    piece_placement.to_s,
    hands.to_s,
    style_turn.to_s
  ].join(Constants::FIELD_SEPARATOR)
end