OSDN Git Service

Start up shogi-server in foreground
[shogi-server/shogi-server.git] / shogi_server / usi.rb
index f18f8c3..788bd4f 100644 (file)
@@ -28,7 +28,10 @@ module ShogiServer # for a namespace
       # i -> 9
       def alphabetToDan(s)
         if RUBY_VERSION >= "1.9.1"
-          return s.bytes[0]-96
+          # String.bytes is incompatible:
+          #  - Ruby 1.9.3 String.bytes returns Enumerator
+          #  - Ruby 2.1.0 String.bytes returns [Integer]
+          return s.each_byte.next-96
         else
           return s[0]-96
         end
@@ -140,7 +143,7 @@ module ShogiServer # for a namespace
     # Convert USI moves to CSA one by one from the initial position
     #
     class UsiToCsa
-      attr_reader :board, :csa_moves
+      attr_reader :board, :csa_moves, :usi_moves
 
       # Constructor
       #
@@ -149,24 +152,31 @@ module ShogiServer # for a namespace
         @board.initial
         @sente = true
         @csa_moves = []
+        @usi_moves = []
+      end
+
+      def deep_copy
+        return Marshal.load(Marshal.dump(self))
       end
 
       # Parses a usi move string and returns an array of [move_result_state,
       # csa_move_string]
       #
       def next(usi)
+        usi_moves << 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
+      attr_reader :board, :csa_moves, :usi_moves
 
       # Constructor
       #
@@ -174,13 +184,19 @@ module ShogiServer # for a namespace
         @board = ShogiServer::Board.new
         @board.initial
         @sente = true
+        @csa_moves = []
         @usi_moves = []
       end
 
+      def deep_copy
+        return Marshal.load(Marshal.dump(self))
+      end
+      
       # Parses a csa move string and returns an array of [move_result_state,
       # usi_move_string]
       #
       def next(csa)
+        csa_moves << csa
         state = @board.handle_one_move(csa, @sente)
         @sente = !@sente
         usi = Usi.moveToUsi(@board.move)