OSDN Git Service

Pieces in hands are correctly recognized now.
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sun, 30 Nov 2008 09:15:34 +0000 (09:15 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sun, 30 Nov 2008 09:15:34 +0000 (09:15 +0000)
shogi_server/usi.rb
test/TC_usi.rb

index a932529..7f70122 100644 (file)
@@ -106,25 +106,28 @@ module ShogiServer # for a namespace
       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
@@ -159,6 +162,7 @@ module ShogiServer # for a namespace
       end
       s += hands2usi(board.sente_hands).upcase
       s += hands2usi(board.gote_hands).downcase
+      return s
     end
   end # class
 
index 360956c..206dab1 100644 (file)
@@ -25,8 +25,63 @@ class TestUsi < Test::Unit::TestCase
   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