Sha256: 2a350768e86e6cfa98d58d59465767f5ef425b25fe9320fb8cc490a6a3e28cdd

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# frozen-string-literal: true

module Bioshogi
  module InputAdapter
    class SfenAdapter < AbstractAdapter
      include OriginSoldierMethods
      include SharedValidation

      def piece
        if drop_trigger
          Piece.fetch(input[:sfen_drop_piece])
        else
          origin_soldier.piece
        end
      end

      def promoted
        if drop_trigger
          false
        else
          origin_soldier.promoted || promote_trigger
        end
      end

      def place_from
        if v = input[:sfen_from]
          Place.fetch(alpha_to_digit(v))
        end
      end

      def place
        Place.fetch(alpha_to_digit(input[:sfen_to]))
      end

      def drop_trigger
        !!input[:sfen_drop_trigger]
      end

      def promote_trigger
        !!input[:sfen_promote_trigger]
      end

      def hard_validations
        super

        if drop_trigger
          unless player.piece_box.exist?(piece)
            errors_add HoldPieceNotFound, "#{piece}を打とうとしましたが#{piece}を持っていません"
          end
        end
      end

      private

      def alpha_to_digit(s)
        s.gsub(/[[:lower:]]/) { |s| s.ord - 'a'.ord + 1 }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bioshogi-0.0.15 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.14 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.10 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.9 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.8 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.7 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.5 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.4 lib/bioshogi/input_adapter/sfen_adapter.rb
bioshogi-0.0.3 lib/bioshogi/input_adapter/sfen_adapter.rb