1 module ShogiServer # for a namespace
21 def danToAlphabet(int)
30 if RUBY_VERSION >= "1.9.1"
37 def csaPieceToUsi(csa, sente)
69 return sente ? str.upcase : str
72 def usiPieceToCsa(str)
110 str += "%s*%s%s" % [csaPieceToUsi(move.name, move.sente).upcase, move.x1, danToAlphabet(move.y1)]
112 str += "%s%s%s%s" % [move.x0, danToAlphabet(move.y0), move.x1, danToAlphabet(move.y1)]
113 str += "+" if move.promotion
119 def usiToCsa(str, board, sente)
123 ret += "00%s%s%s" % [str[2..2], alphabetToDan(str[3..3]), usiPieceToCsa(str[0..0])]
126 from_y = alphabetToDan(str[1..1])
127 ret += "%s%s%s%s" % [from_x, from_y, str[2..2], alphabetToDan(str[3..3])]
128 csa_piece = board.array[from_x.to_i][from_y.to_i]
129 if str.size == 5 && str[4..4] == "+"
131 ret += csa_piece.promoted_name
133 ret += csa_piece.current_name
136 return (sente ? "+" : "-") + ret
140 # Convert USI moves to CSA one by one from the initial position
143 attr_reader :board, :csa_moves, :usi_moves
148 @board = ShogiServer::Board.new
156 return Marshal.load(Marshal.dump(self))
159 # Parses a usi move string and returns an array of [move_result_state,
164 csa = Usi.usiToCsa(usi, @board, @sente)
165 state = @board.handle_one_move(csa, @sente)
173 # Convert CSA moves to USI one by one from the initial position
176 attr_reader :board, :csa_moves, :usi_moves
181 @board = ShogiServer::Board.new
189 return Marshal.load(Marshal.dump(self))
192 # Parses a csa move string and returns an array of [move_result_state,
197 state = @board.handle_one_move(csa, @sente)
199 usi = Usi.moveToUsi(@board.move)
233 return [piece, player]
236 def piece2char(piece)
256 s.downcase! if !piece.sente
263 def parseBoard(word, board)
266 while (i < word.length)
270 piece, player = charToPiece(c)
271 piece.new(board, x, y, player)
275 piece, player = charToPiece(cc)
276 piece.new(board, x, y, player, true)
293 return "" if hands.empty?
296 mapping = [[ShogiServer::PieceHI, "R"],
297 [ShogiServer::PieceKA, "B"],
298 [ShogiServer::PieceKI, "G"],
299 [ShogiServer::PieceGI, "S"],
300 [ShogiServer::PieceKE, "N"],
301 [ShogiServer::PieceKY, "L"],
302 [ShogiServer::PieceFU, "P"]]
304 mapping.each do |klass, str|
305 pieces = hands.find_all {|piece| piece.class == klass}
308 s += "%d" % [pieces.size]
316 # "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b -"
318 def board2usi(board, turn)
323 piece = board.array[x][y]
327 when ShogiServer::Piece
332 s += piece2char(piece)
347 if board.sente_hands.empty? && board.gote_hands.empty?
350 s += hands2usi(board.sente_hands).upcase
351 s += hands2usi(board.gote_hands).downcase