lib/pgn2fen.rb
##
# convert short ply to long ply
convert short ply to long ply
convert short ply to long ply
def short_ply_to_long_ply(ply, is_white)
      from_idx = -1
      to_idx = -1
      from_pgn = ""
      to_pgn = ""
      hint = nil
      if ply.eql?("O-O"); return ply; end
      if ply.eql?("O-O-O"); return ply; end
      unless ply.index('+').nil?; ply.sub!("+", ""); end
      unless ply.index('x').nil?; ply.sub!("x", ""); @halfmove = 0; end
      unless ply.index('=').nil?
        @promotion = true
        @promotion_piece = ply[-1]
        ply = ply.chop.chop
      end
      if ply.length == 2 
        to_pgn = ply
        to_idx = @@pgn_number_hash[to_pgn]
        if is_white
          if !@board[to_idx+8].empty?
            from_idx = to_idx+8
          else
            from_idx = to_idx+16
              @potential_enpassent_ply = @@number_pgn_hash[from_idx-8]
          end
        else  
          if !@board[to_idx-8].empty?
            from_idx = to_idx-8
          else
            from_idx = to_idx-16
              @potential_enpassent_ply = @@number_pgn_hash[from_idx+8]
          end
        end
        from_pgn = @@number_pgn_hash[from_idx]
        @halfmove = 0
        return from_pgn + to_pgn
      end
      if ('a'..'h').include?(ply[0]) && ply.length == 3 
        to_pgn = ply[1..-1]
        to_idx = @@pgn_number_hash[to_pgn]
        if is_white
          if @board[to_idx+7].eql?("P") && file_for_hint(ply[0]).include?(to_idx+7)
            from_idx = to_idx+7
          elsif @board[to_idx+9].eql?("P")&& file_for_hint(ply[0]).include?(to_idx+9)
            from_idx = to_idx+9
          end
        else 
          if @board[to_idx-7].eql?("p") && file_for_hint(ply[0]).include?(to_idx-7)
            from_idx = to_idx-7
          elsif @board[to_idx-9].eql?("p") && file_for_hint(ply[0]).include?(to_idx-9)
            from_idx = to_idx-9
          end
        end
        if from_idx == -1; raise Pgn2FenError, "Error parsing pawn capture at ply #{ply}"; end
        from_pgn = @@number_pgn_hash[from_idx]
        @halfmove = 0
        return from_pgn + to_pgn
      end
      if ply.length == 3; to_pgn = ply[1,2]; end
      if ply.length() == 4
        to_pgn = ply[2,2]
        hint = ply[1]
      end
      to_idx = @@pgn_number_hash[to_pgn]
      if ply[0].downcase.eql?('r'); return short_ply_to_long_ply_for_rook(to_idx, to_pgn, hint, is_white); end
      if ply[0].downcase.eql?('n'); return short_ply_to_long_ply_for_knight(to_idx, to_pgn, hint, is_white); end
      if ply[0].downcase.eql?('b'); return short_ply_to_long_ply_for_bishop(to_idx, to_pgn, hint, is_white); end
      if ply[0].downcase.eql?('q'); return short_ply_to_long_ply_for_queen(to_idx, to_pgn, hint, is_white); end
      if ply[0].downcase.eql?('k'); return short_ply_to_long_ply_for_king(to_idx, to_pgn, hint, is_white); end
      return from_pgn + to_pgn
    end
def short_ply_to_long_ply ( ply , is_white ) from_idx = - 1 to_idx = - 1 from_pgn = "" to_pgn = "" hint = nil if ply . eql? ( "O-O" ) ; return ply ; end if ply . eql? ( "O-O-O" ) ; return ply ; end unless ply . index ( '+' ) . nil? ; ply . sub! ( "+" , "" ) ; end unless ply . index ( 'x' ) . nil? ; ply . sub! ( "x" , "" ) ; @halfmove = 0 ; end unless ply . index ( '=' ) . nil? @promotion = true @promotion_piece = ply [ - 1 ] ply = ply . chop . chop end if ply . length == 2 to_pgn = ply to_idx = @@pgn_number_hash [ to_pgn ] if is_white if ! @board [ to_idx + 8 ] . empty? from_idx = to_idx + 8 else from_idx = to_idx + 16 @potential_enpassent_ply = @@number_pgn_hash [ from_idx - 8 ] end else if ! @board [ to_idx - 8 ] . empty? from_idx = to_idx - 8 else from_idx = to_idx - 16 @potential_enpassent_ply = @@number_pgn_hash [ from_idx + 8 ] end end from_pgn = @@number_pgn_hash [ from_idx ] @halfmove = 0 return from_pgn + to_pgn end if ( 'a' .. 'h' ) . include? ( ply [ 0 ] ) && ply . length == 3 to_pgn = ply [ 1 .. - 1 ] to_idx = @@pgn_number_hash [ to_pgn ] if is_white if @board [ to_idx + 7 ] . eql? ( "P" ) && file_for_hint ( ply [ 0 ] ) . include? ( to_idx + 7 ) from_idx = to_idx + 7 elsif @board [ to_idx + 9 ] . eql? ( "P" ) && file_for_hint ( ply [ 0 ] ) . include? ( to_idx + 9 ) from_idx = to_idx + 9 end else if @board [ to_idx - 7 ] . eql? ( "p" ) && file_for_hint ( ply [ 0 ] ) . include? ( to_idx - 7 ) from_idx = to_idx - 7 elsif @board [ to_idx - 9 ] . eql? ( "p" ) && file_for_hint ( ply [ 0 ] ) . include? ( to_idx - 9 ) from_idx = to_idx - 9 end end if from_idx == - 1 ; raise Pgn2FenError , "Error parsing pawn capture at ply #{ply}" ; end from_pgn = @@number_pgn_hash [ from_idx ] @halfmove = 0 return from_pgn + to_pgn end if ply . length == 3 ; to_pgn = ply [ 1 , 2 ] ; end if ply . length ( ) == 4 to_pgn = ply [ 2 , 2 ] hint = ply [ 1 ] end to_idx = @@pgn_number_hash [ to_pgn ] if ply [ 0 ] . downcase . eql? ( 'r' ) ; return short_ply_to_long_ply_for_rook ( to_idx , to_pgn , hint , is_white ) ; end if ply [ 0 ] . downcase . eql? ( 'n' ) ; return short_ply_to_long_ply_for_knight ( to_idx , to_pgn , hint , is_white ) ; end if ply [ 0 ] . downcase . eql? ( 'b' ) ; return short_ply_to_long_ply_for_bishop ( to_idx , to_pgn , hint , is_white ) ; end if ply [ 0 ] . downcase . eql? ( 'q' ) ; return short_ply_to_long_ply_for_queen ( to_idx , to_pgn , hint , is_white ) ; end if ply [ 0 ] . downcase . eql? ( 'k' ) ; return short_ply_to_long_ply_for_king ( to_idx , to_pgn , hint , is_white ) ; end return from_pgn + to_pgn end
convert short ply to long ply
convert short ply to long ply
# pawn non-capture #            if @board[to_idx-1].eql?"p" or @board[to_idx+1].eql?"p" #            end # isBlack #            if @board[to_idx-1].eql?"P" or @board[to_idx+1].eql?"P" #            end #pawn capture, non-enpassent #is_black
ply
<not_specific>
is_white
<not_specific>