lib/pgn2fen.rb
##
# get FEN from a ply and ply number 
get FEN from a ply and ply number
get FEN from a ply and ply number
def fen_for_ply(ply, ply_number)
      is_white = (ply_number % 2 != 0)
      @halfmove = @halfmove + 1
      long_ply = short_ply_to_long_ply(ply, is_white)
      if long_ply.eql? "O-O"
        if is_white
          make_ply_on_board("e1g1")
          make_ply_on_board("h1f1")
          @can_white_castle_kingside = false
          @can_white_castle_queenside = false
        else
          make_ply_on_board("e8g8")
          make_ply_on_board("h8f8")
          @can_black_castle_kingside = false
          @can_black_castle_queenside = false
        end
      elsif long_ply.eql? "O-O-O"
        if is_white
          make_ply_on_board("e1c1")
          make_ply_on_board("a1d1")
          @can_white_castle_kingside = false
          @can_white_castle_queenside = false
        else
          make_ply_on_board("e8c8")
          make_ply_on_board("a8d8")
          @can_black_castle_kingside = false
          @can_black_castle_queenside = false
        end
      else 
        if is_white
          if @can_white_castle_kingside && long_ply[0,2] == "h1"; @can_white_castle_kingside = false; end
          if @can_white_castle_queenside && long_ply[0,2] == "a1"; @can_white_castle_queenside = false; end
        else
          if @can_black_castle_kingside && long_ply[0,2] == "h8"; @can_white_castle_kingside = false; end
          if @can_black_castle_queenside && long_ply[0,2] == "a8"; @can_white_castle_queenside = false; end
        end
        make_ply_on_board(long_ply)
      end
      fen = board_to_fen
      fen.concat(" ").concat(is_white ? "b": "w")
      fen.concat(" ").concat(fen_castle_text)
      unless @potential_enpassent_ply.nil?
        fen.concat(" ").concat(@potential_enpassent_ply)
        @potential_enpassent_ply = nil
      else
        fen.concat(" ").concat("-")
      end
      fen.concat(" ").concat(@halfmove.to_s)
      if is_white 
        fen.concat(" ").concat((@fullmove).to_s)
      else
        fen.concat(" ").concat((@fullmove += 1).to_s)
      end
      fen
    end
def fen_for_ply ( ply , ply_number ) is_white = ( ply_number % 2 != 0 ) @halfmove = @halfmove + 1 long_ply = short_ply_to_long_ply ( ply , is_white ) if long_ply . eql? "O-O" if is_white make_ply_on_board ( "e1g1" ) make_ply_on_board ( "h1f1" ) @can_white_castle_kingside = false @can_white_castle_queenside = false else make_ply_on_board ( "e8g8" ) make_ply_on_board ( "h8f8" ) @can_black_castle_kingside = false @can_black_castle_queenside = false end elsif long_ply . eql? "O-O-O" if is_white make_ply_on_board ( "e1c1" ) make_ply_on_board ( "a1d1" ) @can_white_castle_kingside = false @can_white_castle_queenside = false else make_ply_on_board ( "e8c8" ) make_ply_on_board ( "a8d8" ) @can_black_castle_kingside = false @can_black_castle_queenside = false end else if is_white if @can_white_castle_kingside && long_ply [ 0 , 2 ] == "h1" ; @can_white_castle_kingside = false ; end if @can_white_castle_queenside && long_ply [ 0 , 2 ] == "a1" ; @can_white_castle_queenside = false ; end else if @can_black_castle_kingside && long_ply [ 0 , 2 ] == "h8" ; @can_white_castle_kingside = false ; end if @can_black_castle_queenside && long_ply [ 0 , 2 ] == "a8" ; @can_white_castle_queenside = false ; end end make_ply_on_board ( long_ply ) end fen = board_to_fen fen . concat ( " " ) . concat ( is_white ? "b" : "w" ) fen . concat ( " " ) . concat ( fen_castle_text ) unless @potential_enpassent_ply . nil? fen . concat ( " " ) . concat ( @potential_enpassent_ply ) @potential_enpassent_ply = nil else fen . concat ( " " ) . concat ( "-" ) end fen . concat ( " " ) . concat ( @halfmove . to_s ) if is_white fen . concat ( " " ) . concat ( ( @fullmove ) . to_s ) else fen . concat ( " " ) . concat ( ( @fullmove += 1 ) . to_s ) end fen end
get FEN from a ply and ply number
get FEN from a ply and ply number
#puts ply # rest of moves #enpassent square #pp_board
ply
<not_specific>
ply_number
<not_specific>