1 module ShogiServer # for a namespace
21 def danToAlphabet(int)
30 if RUBY_VERSION >= "1.9.1"
31 # String.bytes is incompatible:
32 # - Ruby 1.9.3 String.bytes returns Enumerator
33 # - Ruby 2.1.0 String.bytes returns [Integer]
34 return s.each_byte.next-96
40 def csaPieceToUsi(csa, sente)
72 return sente ? str.upcase : str
75 def usiPieceToCsa(str)
113 str += "%s*%s%s" % [csaPieceToUsi(move.name, move.sente).upcase, move.x1, danToAlphabet(move.y1)]
115 str += "%s%s%s%s" % [move.x0, danToAlphabet(move.y0), move.x1, danToAlphabet(move.y1)]
116 str += "+" if move.promotion
122 def usiToCsa(str, board, sente)
126 ret += "00%s%s%s" % [str[2..2], alphabetToDan(str[3..3]), usiPieceToCsa(str[0..0])]
129 from_y = alphabetToDan(str[1..1])
130 ret += "%s%s%s%s" % [from_x, from_y, str[2..2], alphabetToDan(str[3..3])]
131 csa_piece = board.array[from_x.to_i][from_y.to_i]
132 if str.size == 5 && str[4..4] == "+"
134 ret += csa_piece.promoted_name
136 ret += csa_piece.current_name
139 return (sente ? "+" : "-") + ret
143 # Convert USI moves to CSA one by one from the initial position
146 attr_reader :board, :csa_moves, :usi_moves
151 @board = ShogiServer::Board.new
159 return Marshal.load(Marshal.dump(self))
162 # Parses a usi move string and returns an array of [move_result_state,
167 csa = Usi.usiToCsa(usi, @board, @sente)
168 state = @board.handle_one_move(csa, @sente)
176 # Convert CSA moves to USI one by one from the initial position
179 attr_reader :board, :csa_moves, :usi_moves
184 @board = ShogiServer::Board.new
192 return Marshal.load(Marshal.dump(self))
195 # Parses a csa move string and returns an array of [move_result_state,
200 state = @board.handle_one_move(csa, @sente)
202 usi = Usi.moveToUsi(@board.move)
236 return [piece, player]
239 def piece2char(piece)
259 s.downcase! if !piece.sente
266 def parseBoard(word, board)
269 while (i < word.length)
273 piece, player = charToPiece(c)
274 piece.new(board, x, y, player)
278 piece, player = charToPiece(cc)
279 piece.new(board, x, y, player, true)
296 return "" if hands.empty?
299 mapping = [[ShogiServer::PieceHI, "R"],
300 [ShogiServer::PieceKA, "B"],
301 [ShogiServer::PieceKI, "G"],
302 [ShogiServer::PieceGI, "S"],
303 [ShogiServer::PieceKE, "N"],
304 [ShogiServer::PieceKY, "L"],
305 [ShogiServer::PieceFU, "P"]]
307 mapping.each do |klass, str|
308 pieces = hands.find_all {|piece| piece.class == klass}
311 s += "%d" % [pieces.size]
319 # "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b -"
321 def board2usi(board, turn)
326 piece = board.array[x][y]
330 when ShogiServer::Piece
335 s += piece2char(piece)
350 if board.sente_hands.empty? && board.gote_hands.empty?
353 s += hands2usi(board.sente_hands).upcase
354 s += hands2usi(board.gote_hands).downcase