+ # Cache OU piece.
+ # Piece#new will call back this method.
+ #
+ def add_ou(ou)
+ if ou.sente
+ @ous[0] = ou
+ else
+ @ous[1] = ou
+ end
+ end
+
+ # Set up a board with the strs.
+ # Failing to parse the moves raises an StandardError.
+ # @param strs a board text
+ #
+ def set_from_str(strs)
+ strs.each_line do |str|
+ case str
+ when /^P\d/
+ str.sub!(/^P(.)/, '')
+ y = $1.to_i
+ x = 9
+ while (str.length > 2)
+ str.sub!(/^(...?)/, '')
+ one = $1
+ if (one =~ /^([\+\-])(..)/)
+ sg = $1
+ name = $2
+ if (sg == "+")
+ sente = true
+ else
+ sente = false
+ end
+ if ((x < 1) || (9 < x) || (y < 1) || (9 < y))
+ raise "bad position #{x} #{y}"
+ end
+ case (name)
+ when "FU"
+ PieceFU::new(self, x, y, sente)
+ when "KY"
+ PieceKY::new(self, x, y, sente)
+ when "KE"
+ PieceKE::new(self, x, y, sente)
+ when "GI"
+ PieceGI::new(self, x, y, sente)
+ when "KI"
+ PieceKI::new(self, x, y, sente)
+ when "OU"
+ PieceOU::new(self, x, y, sente)
+ when "KA"
+ PieceKA::new(self, x, y, sente)
+ when "HI"
+ PieceHI::new(self, x, y, sente)
+ when "TO"
+ PieceFU::new(self, x, y, sente, true)
+ when "NY"
+ PieceKY::new(self, x, y, sente, true)
+ when "NK"
+ PieceKE::new(self, x, y, sente, true)
+ when "NG"
+ PieceGI::new(self, x, y, sente, true)
+ when "UM"
+ PieceKA::new(self, x, y, sente, true)
+ when "RY"
+ PieceHI::new(self, x, y, sente, true)
+ else
+ raise "unkown piece #{name}"
+ end
+ end
+ x = x - 1
+ end
+ when /^P([\+\-])/
+ sg = $1
+ if (sg == "+")
+ sente = true
+ else
+ sente = false
+ end
+ str.sub!(/^../, '')
+ while (str.length > 3)
+ str.sub!(/^..(..)/, '')
+ name = $1
+ case (name)
+ when "FU"
+ PieceFU::new(self, 0, 0, sente)
+ when "KY"
+ PieceKY::new(self, 0, 0, sente)
+ when "KE"
+ PieceKE::new(self, 0, 0, sente)
+ when "GI"
+ PieceGI::new(self, 0, 0, sente)
+ when "KI"
+ PieceKI::new(self, 0, 0, sente)
+ when "KA"
+ PieceKA::new(self, 0, 0, sente)
+ when "HI"
+ PieceHI::new(self, 0, 0, sente)
+ else
+ raise "unkown piece #{name}"
+ end
+ end # while
+ when /^\+$/
+ @teban = true
+ when /^\-$/
+ @teban = false
+ else
+ raise "bad line: #{str}"
+ end # case
+ end # do
+ end
+
+ # Set up a board starting with a position after the moves.
+ # Failing to parse the moves raises an ArgumentError.
+ # @param moves an array of moves. ex. ["+7776FU", "-3334FU"] or
+ # an array of arrays. ex. [["+7776FU","T2"], ["-3334FU","T5"]]
+ #
+ def set_from_moves(moves)
+ initial()
+ return :normal if moves.empty?
+ rt = nil
+ moves.each do |move|
+ rt = nil
+ case move
+ when Array
+ rt = handle_one_move(move[0], @teban)
+ when String
+ rt = handle_one_move(move, @teban)
+ end
+ raise ArgumentError, "bad moves: #{moves}" unless rt == :normal
+ end
+ @initial_moves = moves.dup
+ end
+