OSDN Git Service

* Starting a buoy game by %%GAME caused an server error.
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sun, 9 Aug 2009 06:17:35 +0000 (06:17 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sun, 9 Aug 2009 06:17:35 +0000 (06:17 +0000)
* To_Move of a proposed game lines was incorrect.

shogi_server/board.rb
shogi_server/command.rb
shogi_server/game.rb
test/TC_board.rb
test/TC_command.rb

index 9c9f6d4..dce239b 100644 (file)
 
 module ShogiServer # for a namespace
 
+class WrongMoves < ArgumentError; end
+
 class Board
+  
+  # Split a moves line into an array of a move string.
+  # If it fails to parse the moves, it raises WrongMoves.
+  # @param moves a moves line. Ex. "+776FU-3334Fu"
+  # @return an array of a move string. Ex. ["+7776FU", "-3334FU"]
+  #
+  def Board.split_moves(moves)
+    ret = []
+
+    rs = moves.gsub %r{[\+\-]\d{4}\w{2}} do |s|
+           ret << s
+           ""
+         end
+    raise WrongMoves, rs unless rs.empty?
+
+    return ret
+  end
 
   def initialize(move_count=0)
     @sente_hands = Array::new
index b677c5e..9d45fdf 100644 (file)
@@ -444,9 +444,11 @@ module ShogiServer
             if buoy_game.instance_of? NilBuoyGame
               # error. never reach
             end
+
+            moves_array = Board::split_moves(buoy_game.moves)
             board = Board.new
             begin
-              board.set_from_moves(buoy_game.moves)
+              board.set_from_moves(moves_array)
             rescue => err
               # it will never happen since moves have already been checked
               log_error "Failed to set up a buoy game: #{moves}"
@@ -608,7 +610,6 @@ module ShogiServer
   #
   #
   class SetBuoyCommand < Command
-    class WrongMoves < ArgumentError; end
 
     def initialize(str, player, game_name, moves, count)
       super(str, player)
@@ -636,8 +637,7 @@ module ShogiServer
       end
 
       # check moves
-      moves_array = split_moves @moves
-
+      moves_array = Board::split_moves(@moves)
       board = Board.new
       begin
         board.set_from_moves(moves_array)
@@ -662,25 +662,6 @@ module ShogiServer
       log_error "Received wrong moves: %s from %s. [%s]" % [@moves, @player.name, e.message]
       return :continue
     end
-
-    private
-    
-    # Split a moves line into an array of a move string.
-    # If it fails to parse the moves, it raises WrongMoves.
-    # @param moves a moves line. Ex. "+776FU-3334Fu"
-    # @return an array of a move string. Ex. ["+7776FU", "-3334FU"]
-    #
-    def split_moves(moves)
-      ret = []
-
-      rs = moves.gsub %r{[\+\-]\d{4}\w{2}} do |s|
-             ret << s
-             ""
-           end
-      raise WrongMoves, rs unless rs.empty?
-
-      return ret
-    end
   end
 
   #
index cf6ab42..6225c79 100644 (file)
@@ -589,7 +589,7 @@ Name+:#{@sente.name}
 Name-:#{@gote.name}
 Your_Turn:#{sg_flag}
 Rematch_On_Draw:NO
-To_Move:+
+To_Move:#{@board.teban ? "+" : "-"}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:#{@total_time}
index 8ca437d..6008af5 100644 (file)
@@ -734,3 +734,26 @@ EOF
   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
index 49f15aa..2f5342b 100644 (file)
@@ -706,32 +706,6 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     @p.name = "set_buoy_player"
   end
 
-  def test_split_moves1
-    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
-    rs = cmd.__send__ :split_moves, "+7776FU"
-    assert_equal ["+7776FU"], rs
-  end
-
-  def test_split_moves2
-    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
-    rs = cmd.__send__ :split_moves, "+7776FU-3334FU"
-    assert_equal ["+7776FU", "-3334FU"], rs
-  end
-
-  def test_split_moves3
-    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
-    assert_nothing_raised do
-      cmd.__send__ :split_moves, ""
-    end
-  end
-
-  def test_split_moves_error1
-    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
-    assert_raise ShogiServer::SetBuoyCommand::WrongMoves do
-      cmd.__send__ :split_moves, "dummy"
-    end
-  end
-
   def test_call_2
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2