OSDN Git Service

Merge branch '201312-KinPenalty' into wdoor-stable
[shogi-server/shogi-server.git] / test / TC_board.rb
index f55a83c..b9d54fc 100644 (file)
@@ -5,99 +5,6 @@ require 'shogi_server'
 require 'shogi_server/board'
 require 'shogi_server/piece'
 
-module ShogiServer
-class Board
-  def set_from_str(strs)
-    strs.split(/\n/).each do |str|
-      if (str =~ /^P\d/)
-        str.sub!(/^P(.)/, '')
-        y = $1.to_i
-        x = 9
-        while (str.length > 2)
-          str.sub!(/^(...?)/, '')
-          one = $1
-          if (one =~ /^([\+\-])(..)/)
-            sg = $1
-            name = $2
-            if (sg == "+")
-              sente = true
-            else
-              sente = false
-            end
-            if ((x < 1) || (9 < x) || (y < 1) || (9 < y))
-              raise "bad position #{x} #{y}"
-            end
-            case (name)
-            when "FU"
-              PieceFU::new(self, x, y, sente)
-            when "KY"
-              PieceKY::new(self, x, y, sente)
-            when "KE"
-              PieceKE::new(self, x, y, sente)
-            when "GI"
-              PieceGI::new(self, x, y, sente)
-            when "KI"
-              PieceKI::new(self, x, y, sente)
-            when "OU"
-              PieceOU::new(self, x, y, sente)
-            when "KA"
-              PieceKA::new(self, x, y, sente)
-            when "HI"
-              PieceHI::new(self, x, y, sente)
-            when "TO"
-              PieceFU::new(self, x, y, sente, true)
-            when "NY"
-              PieceKY::new(self, x, y, sente, true)
-            when "NK"
-              PieceKE::new(self, x, y, sente, true)
-            when "NG"
-              PieceGI::new(self, x, y, sente, true)
-            when "UM"
-              PieceKA::new(self, x, y, sente, true)
-            when "RY"
-              PieceHI::new(self, x, y, sente, true)
-            else
-              raise "unkown piece #{name}"
-            end
-          end
-          x = x - 1
-        end
-      elsif (str =~ /^P([\+\-])/)
-        sg = $1
-        if (sg == "+")
-          sente = true
-        else
-          sente = false
-        end
-        str.sub!(/^../, '')
-        while (str.length > 3)
-          str.sub!(/^..(..)/, '')
-          name = $1
-          case (name)
-          when "FU"
-            PieceFU::new(self, 0, 0, sente)
-          when "KY"
-            PieceKY::new(self, 0, 0, sente)
-          when "KE"
-            PieceKE::new(self, 0, 0, sente)
-          when "GI"
-            PieceGI::new(self, 0, 0, sente)
-          when "KI"
-            PieceKI::new(self, 0, 0, sente)
-          when "KA"
-            PieceKA::new(self, 0, 0, sente)
-          when "HI"
-            PieceHI::new(self, 0, 0, sente)
-          else
-            raise "unkown piece #{name}"
-          end
-        end
-      end
-    end
-  end
-end
-end
-
 class Test_kachi < Test::Unit::TestCase
   def test_kachi_good
     b = ShogiServer::Board.new
@@ -301,6 +208,255 @@ EOM
 end
 
 
+class TestMoveBack < Test::Unit::TestCase
+  def validate_before_after(board, move)
+    orig = board.to_s
+    orig_teban = board.teban
+    orig_move_count = board.move_count
+
+    assert_equal true, board.move_to(move)
+    assert_equal !orig_teban, board.teban
+    assert_equal (orig_move_count+1), board.move_count
+
+    assert_equal true, board.move_back(move)
+    assert_equal orig_teban, board.teban
+    assert_equal orig_move_count, board.move_count
+    assert_equal orig, board.to_s
+  end
+
+  def test_normal
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-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
++
+EOM
+    m = ShogiServer::Move.new(2,7,2,6,"FU",true)
+    validate_before_after(b,m)
+    m = ShogiServer::Move.new(3,3,3,4,"FU",false)
+    validate_before_after(b,m)
+    m = ShogiServer::Move.new(7,7,7,6,"FU",true)
+    validate_before_after(b,m)
+    m = ShogiServer::Move.new(8,3,8,4,"FU",false)
+    validate_before_after(b,m)
+  end
+
+  def test_promote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"TO",true)
+    validate_before_after(b,m)
+  end
+
+  def test_unpromote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"FU",true)
+    validate_before_after(b,m)
+  end
+
+  def test_promoted
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU+TO-FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+EOM
+    m = ShogiServer::Move.new(2,3,1,2,"TO",true)
+    assert !m.promotion
+    validate_before_after(b,m)
+    assert !m.promotion
+  end
+
+  def test_capture
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * -FU * 
+P5 *  *  *  *  *  *  * +FU * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOM
+    m = ShogiServer::Move.new(2,5,2,4,"FU",true)
+    validate_before_after(b,m)
+  end
+  def test_capture_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * -FU * 
+P5 *  *  *  *  *  *  * +FU * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+EOM
+    m = ShogiServer::Move.new(2,4,2,5,"FU",false)
+    validate_before_after(b,m)
+  end
+  def test_capture_promote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"TO",true)
+    validate_before_after(b,m)
+  end
+
+  def test_capture_promote_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+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 *  *  *  *  *  *  * -FU * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+EOM
+    m = ShogiServer::Move.new(2,6,2,7,"TO",false)
+    validate_before_after(b,m)
+  end
+
+  def test_capture_unpromote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"FU",true)
+    validate_before_after(b,m)
+  end
+
+  def test_capture_unpromote_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+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 *  *  *  *  *  *  * -FU * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+EOM
+    m = ShogiServer::Move.new(2,6,2,7,"FU",false)
+    validate_before_after(b,m)
+  end
+
+  def test_drop
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+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
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+P-00FU
+EOM
+    m = ShogiServer::Move.new(0,0,2,3,"FU",true)
+    validate_before_after(b,m)
+  end
+
+  def test_drop_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+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
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+P+00FU
+P-00FU
+EOM
+    m = ShogiServer::Move.new(0,0,2,3,"FU",false)
+    validate_before_after(b,m)
+  end
+end # class TestMoveBack
+
+
 class Test_promote < Test::Unit::TestCase
   def test_fu
     b = ShogiServer::Board.new
@@ -423,7 +579,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+7172HI"))
     assert_equal(:normal, b.handle_one_move("-9291OU"))
 
+    orig = b.deep_copy
     assert_equal(:oute_sennichite_sente_lose, b.handle_one_move("+7271HI")) # 4
+    assert_equal orig, b
   end
 
   def test_oute_sennichite1 #330
@@ -474,7 +632,9 @@ b.history[b.to_s] = 1
     assert_equal(:normal, b.handle_one_move("-4849RY"))
 
     assert_equal(:normal, b.handle_one_move("+6968OU")) # added
+    orig = b.deep_copy
     assert_equal(:oute_sennichite_gote_lose, b.handle_one_move("-4948RY"))
+    assert_equal orig, b
   end
 
   def test_not_oute_sennichite
@@ -499,7 +659,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+7172HI"))
     assert_equal(:normal, b.handle_one_move("-9291OU"))
 
+    orig = b.deep_copy
     assert_equal(:sennichite, b.handle_one_move("+7271HI")) # 4
+    assert_equal orig, b
   end
 
   def test_sennichite0
@@ -521,7 +683,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+7172OU"))
     assert_equal(:normal, b.handle_one_move("-9192OU"))
     assert_equal(:normal, b.handle_one_move("+7271OU"))
+    orig = b.deep_copy
     assert_equal(:sennichite, b.handle_one_move("-9291OU")) # 4
+    assert_equal orig, b
   end
 
   def test_sennichite1          # 329
@@ -550,7 +714,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+2858HI"))
     assert_equal(:normal, b.handle_one_move("-8252HI"))
     assert_equal(:normal, b.handle_one_move("+5828HI"))
+    orig = b.deep_copy
     assert_equal(:sennichite, b.handle_one_move("-5282HI"))
+    assert_equal orig, b
   end
 end
 
@@ -778,3 +944,75 @@ EOM
     assert_equal(false, b.uchifuzume?(true))
  end
 end
+
+class TestBoardForBuoy < Test::Unit::TestCase
+  def setup
+    @board = ShogiServer::Board.new
+  end
+
+  def test_set_from_moves_empty
+    moves = []
+    rt = @board.set_from_moves moves
+    assert_equal(:normal, rt)
+  end
+
+  def test_set_from_moves
+    moves = ["+7776FU", "-3334FU"]
+    assert_nothing_raised do
+      @board.set_from_moves moves
+    end
+
+    correct = ShogiServer::Board.new
+    correct.set_from_str <<EOF
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU * -FU-FU
+P4 *  *  *  *  *  * -FU *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  * +FU *  *  *  *  *  * 
+P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOF
+    assert_equal(correct.to_s, @board.to_s)
+  end
+
+  def test_set_from_moves_error1
+    moves = ["+7776FU", "-3435FU"]
+    assert_raise ArgumentError do
+      @board.set_from_moves moves
+    end
+  end
+
+  def test_set_from_moves_error2
+    moves = ["+7776FU", "+8786FU"]
+    assert_raise ArgumentError do
+      @board.set_from_moves moves
+    end
+  end
+end # TestBoardForBuoy
+
+class TestSplitMoves < Test::Unit::TestCase
+  def test_split_moves1
+    rs = ShogiServer::Board::split_moves "+7776FU"
+    assert_equal ["+7776FU"], rs
+  end
+
+  def test_split_moves2
+    rs = ShogiServer::Board::split_moves "+7776FU-3334FU"
+    assert_equal ["+7776FU", "-3334FU"], rs
+  end
+
+  def test_split_moves3
+    assert_nothing_raised do
+      ShogiServer::Board::split_moves ""
+    end
+  end
+
+  def test_split_moves_error1
+    assert_raise ShogiServer::WrongMoves do
+      ShogiServer::Board::split_moves "dummy"
+    end
+  end
+end