class Ugoki::Selector
The selector class.
Attributes
@!attribute [r] position
@return [Ugoki::Position] The state of the game.
@!attribute [r] tablebase
@return [Hash] A tablebase containing precalculated pseudo-legal moves of the game.
Public Class Methods
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
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
@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
@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
@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
@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
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
@return [String] The name of the unpromoted piece.
# File lib/ugoki/selector.rb, line 80 def unpromote(piece_name) piece_name.delete("+") end