OSDN Git Service

shogi_server/board.rb, piece.rb: Refactoring to cache OU pieces, which was inspired...
authorDaigo Moriwaki <daigo@debian.org>
Sun, 18 Dec 2011 13:37:41 +0000 (22:37 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 18 Dec 2011 13:37:41 +0000 (22:37 +0900)
(74b24b88c843f1dd767412475b117481d1d5e8eb).

changelog
shogi_server/board.rb
shogi_server/piece.rb

index 37d087e..270573b 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,10 @@
+2011-12-18  Daigo Moriwaki <daigo at debian dot org>
+
+       * [sohgi-server]
+         - shogi_server/board.rb, piece.rb: Refactoring to cache OU pieces,
+           which was inspired by 81SquareShogi-server's change
+           (74b24b88c843f1dd767412475b117481d1d5e8eb).
+
 2011-12-12  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server]
index d2b304b..21093b4 100644 (file)
@@ -69,6 +69,7 @@ EOF
     @move_count = move_count
     @teban = nil # black => true, white => false
     @initial_moves = []
+    @ous = [nil, nil] # keep OU pieces of Sente and Gote
   end
   attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history, :teban
   attr_reader :move_count
@@ -128,6 +129,17 @@ EOF
     @teban = true
   end
 
+  # Cache OU piece.
+  # Piece#new will call back this method.
+  #
+  def add_ou(ou)
+    if ou.sente
+      @ous[0] = ou
+    else
+      @ous[1] = ou
+    end
+  end
+
   # Set up a board with the strs.
   # Failing to parse the moves raises an StandardError.
   # @param strs a board text
@@ -324,22 +336,13 @@ EOF
   end
   
   # def each_reserved_square
-
+  
   def look_for_ou(sente)
-    x = 1
-    while (x <= 9)
-      y = 1
-      while (y <= 9)
-        if (@array[x][y] &&
-            (@array[x][y].name == "OU") &&
-            (@array[x][y].sente == sente))
-          return @array[x][y]
-        end
-        y = y + 1
-      end
-      x = x + 1
+    if sente
+      return @ous[0]
+    else
+      return @ous[1]
     end
-    raise "can't find ou"
   end
 
   # See if sente is checked (i.e. loosing) or not.
index 93d085f..ab8efba 100644 (file)
@@ -393,6 +393,7 @@ class PieceOU < Piece
     @name = "OU"
     @promoted_name = nil
     super
+    @board.add_ou(self)
   end
 end