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
148 @board = ShogiServer::Board.new
154 # Parses a usi move string and returns an array of [move_result_state,
158 csa = Usi.usiToCsa(usi, @board, @sente)
159 state = @board.handle_one_move(csa, @sente)
166 # Convert CSA moves to USI one by one from the initial position
169 attr_reader :board, :usi_moves
174 @board = ShogiServer::Board.new
180 # Parses a csa move string and returns an array of [move_result_state,
184 state = @board.handle_one_move(csa, @sente)
186 usi = Usi.moveToUsi(@board.move)
220 return [piece, player]
223 def piece2char(piece)
243 s.downcase! if !piece.sente
250 def parseBoard(word, board)
253 while (i < word.length)
257 piece, player = charToPiece(c)
258 piece.new(board, x, y, player)
262 piece, player = charToPiece(cc)
263 piece.new(board, x, y, player, true)
280 return "" if hands.empty?
283 mapping = [[ShogiServer::PieceHI, "R"],
284 [ShogiServer::PieceKA, "B"],
285 [ShogiServer::PieceKI, "G"],
286 [ShogiServer::PieceGI, "S"],
287 [ShogiServer::PieceKE, "N"],
288 [ShogiServer::PieceKY, "L"],
289 [ShogiServer::PieceFU, "P"]]
291 mapping.each do |klass, str|
292 pieces = hands.find_all {|piece| piece.class == klass}
295 s += "%d" % [pieces.size]
303 # "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b -"
305 def board2usi(board, turn)
310 piece = board.array[x][y]
314 when ShogiServer::Piece
319 s += piece2char(piece)
334 if board.sente_hands.empty? && board.gote_hands.empty?
337 s += hands2usi(board.sente_hands).upcase
338 s += hands2usi(board.gote_hands).downcase