Class: Sashite::Pmn::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/pmn/action.rb

Overview

Represents an atomic action within a PMN move.

Each action encodes a single transformation:

[source, destination]            # inferred piece
[source, destination, piece]     # explicit QPI piece

Locations are either CELL coordinates or HAND (“*”). Actions where source == destination are allowed (pass / in-place).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, piece = nil) ⇒ Action

Create an immutable action.

Parameters:

  • source (String)

    CELL or “*”

  • destination (String)

    CELL or “*”

  • piece (String, nil) (defaults to: nil)

    QPI string (optional)

Raises:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sashite/pmn/action.rb', line 36

def initialize(source, destination, piece = nil)
  validate_source!(source)
  validate_destination!(destination)
  validate_piece!(piece) if piece

  @source = source.freeze
  @destination = destination.freeze
  @piece = piece&.freeze

  freeze
end

Instance Attribute Details

#destinationString (readonly)

Returns the destination location (CELL or “*”).

Returns:

  • (String)

    the destination location (CELL or “*”)



24
25
26
# File 'lib/sashite/pmn/action.rb', line 24

def destination
  @destination
end

#pieceString? (readonly)

Returns the piece in QPI format, or nil if inferred.

Returns:

  • (String, nil)

    the piece in QPI format, or nil if inferred



27
28
29
# File 'lib/sashite/pmn/action.rb', line 27

def piece
  @piece
end

#sourceString (readonly)

Returns the source location (CELL or “*”).

Returns:

  • (String)

    the source location (CELL or “*”)



21
22
23
# File 'lib/sashite/pmn/action.rb', line 21

def source
  @source
end

Class Method Details

.from_hash(hash) ⇒ Action

Build from a hash with keys :source, :destination, optional :piece.

Parameters:

  • hash (Hash)

Returns:

Raises:

  • (ArgumentError)

    if required keys are missing



148
149
150
151
152
153
# File 'lib/sashite/pmn/action.rb', line 148

def self.from_hash(hash)
  raise ::ArgumentError, "Hash must include :source" unless hash.key?(:source)
  raise ::ArgumentError, "Hash must include :destination" unless hash.key?(:destination)

  new(hash[:source], hash[:destination], hash[:piece])
end

Instance Method Details

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

Returns equality by source, destination, piece.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

    equality by source, destination, piece



120
121
122
123
124
125
126
# File 'lib/sashite/pmn/action.rb', line 120

def ==(other)
  return false unless other.is_a?(Action)

  source == other.source &&
    destination == other.destination &&
    piece == other.piece
end

#board_move?Boolean

Returns true when neither drop nor capture.

Returns:

  • (Boolean)

    true when neither drop nor capture



93
94
95
# File 'lib/sashite/pmn/action.rb', line 93

def board_move?
  !drop? && !capture?
end

#board_to_board?Boolean

Returns true if both endpoints are board locations.

Returns:

  • (Boolean)

    true if both endpoints are board locations



78
79
80
# File 'lib/sashite/pmn/action.rb', line 78

def board_to_board?
  ::Sashite::Cell.valid?(source) && ::Sashite::Cell.valid?(destination)
end

#capture?Boolean

Returns true if the action takes from board to reserve.

Returns:

  • (Boolean)

    true if the action takes from board to reserve



88
89
90
# File 'lib/sashite/pmn/action.rb', line 88

def capture?
  ::Sashite::Cell.valid?(source) && to_reserve?
end

#drop?Boolean

Returns true if the action places from reserve to board.

Returns:

  • (Boolean)

    true if the action places from reserve to board



83
84
85
# File 'lib/sashite/pmn/action.rb', line 83

def drop?
  from_reserve? && ::Sashite::Cell.valid?(destination)
end

#from_reserve?Boolean

Returns true if source is HAND (“*”).

Returns:

  • (Boolean)

    true if source is HAND (“*”)



68
69
70
# File 'lib/sashite/pmn/action.rb', line 68

def from_reserve?
  ::Sashite::Hand.reserve?(source)
end

#hashInteger

Returns:

  • (Integer)


130
131
132
# File 'lib/sashite/pmn/action.rb', line 130

def hash
  [source, destination, piece].hash
end

#inferred?Boolean

Returns true if piece is not explicitly specified.

Returns:

  • (Boolean)

    true if piece is not explicitly specified



51
52
53
# File 'lib/sashite/pmn/action.rb', line 51

def inferred?
  piece.nil?
end

#inspectString

Returns:

  • (String)


135
136
137
138
139
# File 'lib/sashite/pmn/action.rb', line 135

def inspect
  attrs = ["source=#{source.inspect}", "destination=#{destination.inspect}"]
  attrs << "piece=#{piece.inspect}" if piece
  "#<#{self.class.name} #{attrs.join(' ')}>"
end

#piece_specified?Boolean

Returns true if a piece string is explicitly specified.

Returns:

  • (Boolean)

    true if a piece string is explicitly specified



56
57
58
# File 'lib/sashite/pmn/action.rb', line 56

def piece_specified?
  !piece.nil?
end

#piece_valid?Boolean

Returns true if a specified piece is valid QPI (false if none).

Returns:

  • (Boolean)

    true if a specified piece is valid QPI (false if none)



61
62
63
64
65
# File 'lib/sashite/pmn/action.rb', line 61

def piece_valid?
  return false if piece.nil?

  ::Sashite::Qpi.valid?(piece)
end

#to_aArray<String>

Returns 2 elems if inferred, 3 if explicit.

Returns:

  • (Array<String>)

    2 elems if inferred, 3 if explicit



100
101
102
# File 'lib/sashite/pmn/action.rb', line 100

def to_a
  inferred? ? [source, destination] : [source, destination, piece]
end

#to_hHash

Returns { source:, destination:, piece: } (piece omitted if nil).

Returns:

  • (Hash)

    { source:, destination:, piece: } (piece omitted if nil)



105
106
107
# File 'lib/sashite/pmn/action.rb', line 105

def to_h
  { source: source, destination: destination, piece: piece }.compact
end

#to_reserve?Boolean

Returns true if destination is HAND (“*”).

Returns:

  • (Boolean)

    true if destination is HAND (“*”)



73
74
75
# File 'lib/sashite/pmn/action.rb', line 73

def to_reserve?
  ::Sashite::Hand.reserve?(destination)
end

#valid?Boolean

Returns true if all components are valid.

Returns:

  • (Boolean)

    true if all components are valid



112
113
114
115
116
# File 'lib/sashite/pmn/action.rb', line 112

def valid?
  valid_location?(source) &&
    valid_location?(destination) &&
    (piece.nil? || ::Sashite::Qpi.valid?(piece))
end