module Chess
  class Board
    def self.from_fen(fen)
      new(FenInput.new(fen).squares)
    end

    def initialize(squares = nil)
      @squares = squares || default_starting_position
    end

    def to_s
      @squares.strip.tr(' ', '')
    end

    private
    def default_starting_position
      <<-EOF
        rnbqkbnr
        pppppppp
        ........
        ........
        ........
        ........
        PPPPPPPP
        RNBQKBNR
      EOF
    end
  end
end

module Chess
  class Board
    def self.from_fen(fen)
      new(FenInput.new(fen).squares)
    end

    def self.array_to_string(array)
      array.map {|rank| rank.map {|file| file.nil? ? "." : file }.join }.join("\n")
    end

    def initialize(squares = nil)
      @squares = squares || default_starting_position
    end

    def to_s
      @squares.strip.tr(' ', '')
    end

    private
    def default_starting_position
      Board.array_to_string([
        %w{r n b q k b n r},
        %w{p p p p p p p p},
        [nil] * 8,
        [nil] * 8,
        [nil] * 8,
        [nil] * 8,
        %w{P P P P P P P P},
        %w{R N B Q K B N R}
      ])
    end
  end
end

Move towards making Board accept an array.
Move towards making Board accept an array.

Looking at the tests for checking the bounds of the board, it became apparent
that string manipulation was going to be a pain. So it seems like a good time
to introduce the squares as an array.

I want to do this a bit at a time to avoid breaking the tests, so I've created
the array_to_string method which turns an array into the appropriate string.
It's purely temporary in its current form, but may eventually form part of the
to_s method.

I've started by making the default_starting_position an array and then
immediately converting it back. I can do this for every place I
create a string instead of an array. Then I can switch the entire system to
use arrays in one commit.
