From 48caf92ecf5d5669a5ccef83bf351f612e8aab70 Mon Sep 17 00:00:00 2001 From: Daigo Moriwaki Date: Fri, 13 Dec 2013 15:09:35 +0900 Subject: [PATCH] [shogi-server] - Implemented conversion of move representation between CSA format and USI one. --- changelog | 6 ++ shogi_server/board.rb | 18 +++-- shogi_server/piece.rb | 11 ++-- shogi_server/usi.rb | 177 +++++++++++++++++++++++++++++++++++++++++++++++++- test/TC_usi.rb | 18 +++++ 5 files changed, 217 insertions(+), 13 deletions(-) diff --git a/changelog b/changelog index b8648e3..03541cc 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,9 @@ +2013-12-13 Daigo Moriwaki + + * [shogi-server] + - Implemented conversion of move representation between CSA format + and USI one. + 2013-11-24 Daigo Moriwaki * [shogi-server] diff --git a/shogi_server/board.rb b/shogi_server/board.rb index 3bd01b1..66d4d01 100644 --- a/shogi_server/board.rb +++ b/shogi_server/board.rb @@ -94,6 +94,7 @@ EOF @move_count = move_count @teban = nil # black => true, white => false @initial_moves = [] + @move = nil @ous = [nil, nil] # keep OU pieces of Sente and Gote end attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history, :teban @@ -104,6 +105,11 @@ EOF # moves. attr_reader :initial_moves + # A move parsed by handle_one_move. If the move is not :normal, the board + # position may or may not be rolled back. + # + attr_reader :move + # See if self equals rhs, including a logical board position (i.e. # not see object IDs) and sennichite stuff. # @@ -678,18 +684,18 @@ EOF return :illegal # can't put on existing piece end - move = Move.new(x0, y0, x1, y1, name, sente) - result = move_to(move) + @move = Move.new(x0, y0, x1, y1, name, sente) + result = move_to(@move) if (result == :illegal) # self is unchanged return :illegal end if (checkmated?(sente)) - move_back(move) + move_back(@move) return :oute_kaihimore end if ((x0 == 0) && (y0 == 0) && (name == "FU") && uchifuzume?(sente)) - move_back(move) + move_back(@move) return :uchifuzume end @@ -697,12 +703,12 @@ EOF update_sennichite(sente) os_result = oute_sennichite?(sente) if os_result # :oute_sennichite_sente_lose or :oute_sennichite_gote_lose - move_back(move) + move_back(@move) restore_sennichite_stuff(*sennichite_stuff) return os_result end if sennichite? - move_back(move) + move_back(@move) restore_sennichite_stuff(*sennichite_stuff) return :sennichite end diff --git a/shogi_server/piece.rb b/shogi_server/piece.rb index 64b918a..8717dff 100644 --- a/shogi_server/piece.rb +++ b/shogi_server/piece.rb @@ -154,18 +154,17 @@ class Piece @promoted_name end + def current_name + return @promoted ? @promoted_name : @name + end + def to_s if (@sente) sg = "+" else sg = "-" end - if (@promoted) - n = @promoted_name - else - n = @name - end - return sg + n + return sg + current_name end end diff --git a/shogi_server/usi.rb b/shogi_server/usi.rb index 7f70122..f18f8c3 100644 --- a/shogi_server/usi.rb +++ b/shogi_server/usi.rb @@ -13,7 +13,181 @@ module ShogiServer # for a namespace gsub("@", "+"). gsub(".", " ") end - end + + # 1 -> a + # 2 -> b + # ... + # 9 -> i + def danToAlphabet(int) + return (int+96).chr + end + + # a -> 1 + # b -> 2 + # ... + # i -> 9 + def alphabetToDan(s) + if RUBY_VERSION >= "1.9.1" + return s.bytes[0]-96 + else + return s[0]-96 + end + end + + def csaPieceToUsi(csa, sente) + str = "" + case csa + when "FU" + str = "p" + when "KY" + str = "l" + when "KE" + str = "n" + when "GI" + str = "s" + when "KI" + str = "g" + when "KA" + str = "b" + when "HI" + str = "r" + when "OU" + str = "k" + when "TO" + str = "+p" + when "NY" + str = "+l" + when "NK" + str = "+n" + when "NG" + str = "+s" + when "UM" + str = "+b" + when "RY" + str = "+r" + end + return sente ? str.upcase : str + end + + def usiPieceToCsa(str) + ret = "" + case str.downcase + when "p" + ret = "FU" + when "l" + ret = "KY" + when "n" + ret = "KE" + when "s" + ret = "GI" + when "g" + ret = "KI" + when "b" + ret = "KA" + when "r" + ret = "HI" + when "+p" + ret = "TO" + when "+l" + ret = "NY" + when "+n" + ret = "NK" + when "+s" + ret = "NG" + when "+b" + ret = "UM" + when "+r" + ret = "RY" + when "k" + ret = "OU" + end + return ret + end + + def moveToUsi(move) + str = "" + if move.is_drop? + str += "%s*%s%s" % [csaPieceToUsi(move.name, move.sente).upcase, move.x1, danToAlphabet(move.y1)] + else + str += "%s%s%s%s" % [move.x0, danToAlphabet(move.y0), move.x1, danToAlphabet(move.y1)] + str += "+" if move.promotion + end + + return str + end + + def usiToCsa(str, board, sente) + ret = "" + if str[1..1] == "*" + # drop + ret += "00%s%s%s" % [str[2..2], alphabetToDan(str[3..3]), usiPieceToCsa(str[0..0])] + else + from_x = str[0..0] + from_y = alphabetToDan(str[1..1]) + ret += "%s%s%s%s" % [from_x, from_y, str[2..2], alphabetToDan(str[3..3])] + csa_piece = board.array[from_x.to_i][from_y.to_i] + if str.size == 5 && str[4..4] == "+" + # Promoting move + ret += csa_piece.promoted_name + else + ret += csa_piece.current_name + end + end + return (sente ? "+" : "-") + ret + end + end # class methods + + # Convert USI moves to CSA one by one from the initial position + # + class UsiToCsa + attr_reader :board, :csa_moves + + # Constructor + # + def initialize + @board = ShogiServer::Board.new + @board.initial + @sente = true + @csa_moves = [] + end + + # Parses a usi move string and returns an array of [move_result_state, + # csa_move_string] + # + def next(usi) + csa = Usi.usiToCsa(usi, @board, @sente) + state = @board.handle_one_move(csa, @sente) + @sente = !@sente + @csa_moves << csa + return [state, csa] + end + end # class UsiToCsa + + # Convert CSA moves to USI one by one from the initial position + # + class CsaToUsi + attr_reader :board, :usi_moves + + # Constructor + # + def initialize + @board = ShogiServer::Board.new + @board.initial + @sente = true + @usi_moves = [] + end + + # Parses a csa move string and returns an array of [move_result_state, + # usi_move_string] + # + def next(csa) + state = @board.handle_one_move(csa, @sente) + @sente = !@sente + usi = Usi.moveToUsi(@board.move) + @usi_moves << usi + return [state, usi] + end + end # class CsaToUsi def charToPiece(c) player = nil @@ -164,6 +338,7 @@ module ShogiServer # for a namespace s += hands2usi(board.gote_hands).downcase return s end + end # class end # module diff --git a/test/TC_usi.rb b/test/TC_usi.rb index 206dab1..8dbe045 100644 --- a/test/TC_usi.rb +++ b/test/TC_usi.rb @@ -84,4 +84,22 @@ P-00FU00FU EOB assert_equal "lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b 2p", @usi.board2usi(b, b.teban) end + + def test_usiToCsa + # 26th Ryuousen 5th match Moriuchi vs Watanabe on Nov 28th, 2013 + usi_moves = %w!7g7f 8c8d 7i6h 3c3d 6g6f 7a6b 5g5f 5c5d 3i4h 3a4b 4i5h 4a3b 6i7h 5a4a 5i6i 6a5b 6h7g 4b3c 8h7i 2b3a 3g3f 4c4d 4h3g 3a6d 5h6g 7c7d 7i6h 5b4c 6i7i 4a3a 7i8h 9c9d 3g4f 6b5c 2i3g 6d7c 1g1f 1c1d 2g2f 3c2d 2h3h 9d9e 1i1h 3a2b 3g2e 4d4e 4f4e 7c1i+ 6h4f 1i4f 4g4f B*5i B*3g 5i3g+ 3h3g B*1i 3g3h 1i4f+ P*4d 4c4d B*7a 4d4c 6g5g 4f5g 7a8b+ P*4d 4e3d 4c3d R*5a 5c4b 5a8a+ S*6i 7h6h 5g6h 3h6h G*5h 8b4f 5h6h 4f6h R*4h G*7i 6i5h+ 6h7h G*6i 7i6i 5h6i B*5g 4h1h+ P*4h 1h2g 5g2d 3d2d 7h6i L*6g S*6h 6g6h+ 6i6h S*5g N*3c 4b3c 2e3c+ 2b3c L*3e P*3d 8a2a G*3a 2a1a 5g6h 7g6h N*6d S*6g 3d3e S*5c 2g3f N*2e 3c4c 5c6d+ 6c6d G*3c 4c5c 3c3b 3a3b N*5e 5d5e 1a5a L*5b L*5d 5c5d 5a5b 5d4e L*4g 3f4g 4h4g 4e3f G*3h! + uc = ShogiServer::Usi::UsiToCsa.new + usi_moves.each do |m| + state, csa = uc.next(m) + assert_equal(:normal, state) + end + + cu = ShogiServer::Usi::CsaToUsi.new + uc.csa_moves.each do |m| + state, usi = cu.next(m) + assert_equal(:normal, state) + end + + assert_equal(usi_moves, cu.usi_moves) + end end -- 2.11.0