class Ugoki::Selector

The selector class.

@see developer.sashite.com/specs/general-actor-notation

Attributes

position[R]

@!attribute [r] position

@return [Ugoki::Position] The state of the game.
tablebase[R]

@!attribute [r] tablebase

@return [Hash] A tablebase containing precalculated pseudo-legal moves
  of the game.

Public Class Methods

new(tablebase, position) click to toggle source

Initialize a selector.

@param tablebase [Hash] A tablebase containing precalculated pseudo-legal

moves of the game.

@param position [Ugoki::Position] The state of the game.

# File lib/ugoki/selector.rb, line 24
def initialize(tablebase, position)
  @tablebase = tablebase
  @position = position
end

Public Instance Methods

call() click to toggle source

Returns selected pseudo-legal moves.

@return [Set] All pseudo-legal moves.

# File lib/ugoki/selector.rb, line 32
def call
  selected_moves.to_set + allowed_drops.to_set
end

Private Instance Methods

allowed_drops() click to toggle source

@return [Set] A list of allowed drops.

# File lib/ugoki/selector.rb, line 85
def allowed_drops
  position.in_hand_owned_pieces.inject([]) do |moves, piece_name|
    moves + gameplays(piece_name, nil).map do |gameplay|
      meta_action_to_action(nil, *gameplay[1..])
    end
  end
end
capture(piece_name) click to toggle source

@return [String] The name of the captured piece.

# File lib/ugoki/selector.rb, line 75
def capture(piece_name)
  position.turn_to_topside? ? piece_name.downcase : piece_name.upcase
end
gameplays(piece_name, src_square_id) click to toggle source

@param piece_name [String] The name of a piece. @param src_square_id [Integer] The source square of the piece.

@return [Array] A subset of the tablebase.

# File lib/ugoki/selector.rb, line 42
def gameplays(piece_name, src_square_id)
  tablebase.fetch(piece_name).fetch(src_square_id).select do |gameplay|
    # @example
    #   gameplay # => [{45=>:enemy}, 45, "S:P", true]
    conditions = gameplay.fetch(0)
    position.match?(conditions)
  end
end
meta_action_to_action(src_index, dst_index, moved_piece, is_capture) click to toggle source

@param src_index [Integer] A source square. @param dst_index [Integer] A target square. @param moved_piece [String] A name for the moved piece. @param is_capture [Boolean] Is it a capture?

@return [Array] An action.

# File lib/ugoki/selector.rb, line 68
def meta_action_to_action(src_index, dst_index, moved_piece, is_capture)
  captured = capture(unpromote(position.square.fetch(dst_index))) if is_capture

  [src_index, dst_index, moved_piece, captured]
end
selected_moves() click to toggle source

List the selected moves.

@return [Set] A list of selected moves.

# File lib/ugoki/selector.rb, line 54
def selected_moves
  position.square_owned_pieces.inject([]) do |moves, (src_square_id, piece_name)|
    moves + gameplays(piece_name, src_square_id).map do |gameplay|
      meta_action_to_action(src_square_id, *gameplay[1..])
    end
  end
end
unpromote(piece_name) click to toggle source

@return [String] The name of the unpromoted piece.

# File lib/ugoki/selector.rb, line 80
def unpromote(piece_name)
  piece_name.delete("+")
end