return "" if hands.empty?
s = ""
- mapping = [[ShogiServer::PieceHi, R],
- [ShogiServer::PieceKA, B],
- [ShogiServer::PieceKI, G],
- [ShogiServer::PieceGI, S],
- [ShogiServer::PieceKE, N],
- [ShogiServer::PieceKY, L],
- [ShogiServer::PieceFU, P]]
+ mapping = [[ShogiServer::PieceHI, "R"],
+ [ShogiServer::PieceKA, "B"],
+ [ShogiServer::PieceKI, "G"],
+ [ShogiServer::PieceGI, "S"],
+ [ShogiServer::PieceKE, "N"],
+ [ShogiServer::PieceKY, "L"],
+ [ShogiServer::PieceFU, "P"]]
mapping.each do |klass, str|
- pieces = hands.find_all {|piece| piece === klass}
+ pieces = hands.find_all {|piece| piece.class == klass}
unless pieces.empty?
if pieces.size > 1
- s += "%d" [pieces.size]
+ s += "%d" % [pieces.size]
end
s += str
end
end
+ return s
end
+ # "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b -"
+ #
def board2usi(board, turn)
s = ""
for y in 1..9
end
s += hands2usi(board.sente_hands).upcase
s += hands2usi(board.gote_hands).downcase
+ return s
end
end # class
def test_hirate_board
board = ShogiServer::Board.new
board.initial
- hirate_sfen = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b -";
+ hirate_sfen = "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b -";
+ hirate_sfen_escaped = "lnsgkgsnl_1r5b1_ppppppppp_9_9_9_PPPPPPPPP_1B5R1_LNSGKGSNL.b.-";
assert_equal hirate_sfen, @usi.board2usi(board, board.teban)
+ assert_equal hirate_sfen_escaped, ShogiServer::Usi.escape(@usi.board2usi(board, board.teban))
+ end
+
+ def test_board_with_hands1
+ b = ShogiServer::Board.new
+ b.initial
+ b.set_from_str(<<EOB)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI * * * * * -KA *
+P3-FU-FU-FU-FU-FU-FU-FU-FU *
+P4 * * * * * * * * *
+P5 * * * * * * * * *
+P6 * * * * * * * * *
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA * * * * * +HI *
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+P+00FU
+EOB
+ assert_equal "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b P", @usi.board2usi(b, b.teban)
end
-end
+ def test_board_with_hands2
+ b = ShogiServer::Board.new
+ b.initial
+ b.set_from_str(<<EOB)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI * * * * * -KA *
+P3-FU-FU-FU-FU-FU-FU-FU-FU *
+P4 * * * * * * * * *
+P5 * * * * * * * * *
+P6 * * * * * * * * *
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA * * * * * +HI *
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+P-00FU
+EOB
+ assert_equal "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b p", @usi.board2usi(b, b.teban)
+ end
+
+ def test_board_with_hands3
+ b = ShogiServer::Board.new
+ b.initial
+ b.set_from_str(<<EOB)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI * * * * * -KA *
+P3-FU-FU-FU-FU-FU-FU-FU * *
+P4 * * * * * * * * *
+P5 * * * * * * * * *
+P6 * * * * * * * * *
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA * * * * * +HI *
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+P-00FU00FU
+EOB
+ assert_equal "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b 2p", @usi.board2usi(b, b.teban)
+ end
+end