OSDN Git Service

Remove a debug message
[shogi-server/shogi-server.git] / shogi_server / board.rb
index 21093b4..4c9c607 100644 (file)
@@ -1,7 +1,7 @@
 ## $Id$
 
 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
-## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
+## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -43,33 +43,66 @@ EOF
 
   # 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"]
+  # @param moves a moves line. Ex. "+776FU-3334FU" or
+  #              moves with times. Ex "+776FU,T2-3334FU,T5"
+  # @return an array of a move string. Ex. ["+7776FU", "-3334FU"] or
+  #         an array of arrays. Ex. [["+7776FU","T2"], ["-3334FU", "T5"]]
   #
   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?
+    i=0
+    tmp = ""
+    while i<moves.size
+      if moves[i,1] == "+" ||
+         moves[i,1] == "-" ||
+         i == moves.size - 1
+        if i == moves.size - 1
+          tmp << moves[i,1]
+        end
+        unless tmp.empty?
+          a = tmp.split(",")
+          if a[0].size != 7
+            raise WrongMoves, a[0]
+          end
+          if a.size == 1 # "+7776FU"
+            ret << a[0]
+          else           # "+7776FU,T2"
+            unless /^T\d+/ =~ a[1] 
+              raise WrongMoves, a[1]
+            end
+            ret << a
+          end
+          tmp = ""
+        end
+      end
+      tmp << moves[i,1]
+      i += 1
+    end
 
     return ret
   end
 
 
-  def initialize(move_count=0)
+  def initialize(options={})
     @sente_hands = Array::new
     @gote_hands  = Array::new
     @history       = Hash::new(0)
     @sente_history = Hash::new(0)
     @gote_history  = Hash::new(0)
     @array = [[], [], [], [], [], [], [], [], [], []]
-    @move_count = move_count
+    @move_count = 0
     @teban = nil # black => true, white => false
     @initial_moves = []
+    @move = nil
     @ous = [nil, nil] # keep OU pieces of Sente and Gote
+
+    @max_moves = options[:max_moves] ||
+                 ($options && $options["max-moves"]) ||
+                 Default_Max_Moves
+    @least_time_per_move = options[:least_time_per_move] ||
+                           ($options && $options["least-time-per-move"]) ||
+                           Default_Least_Time_Per_Move
   end
   attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history, :teban
   attr_reader :move_count
@@ -79,6 +112,17 @@ EOF
   # moves.
   attr_reader :initial_moves
 
+  # A move parsed by handle_one_move. If the move is not :normal, the board
+  # position may or may not be rolled back.
+  #
+  attr_reader :move
+
+  # Max_Moves of the CSA protocol
+  attr_reader :max_moves
+
+  # Least_Time_Per_Move of the CSA protocol
+  attr_reader :least_time_per_move
+
   # See if self equals rhs, including a logical board position (i.e.
   # not see object IDs) and sennichite stuff.
   #
@@ -242,14 +286,21 @@ EOF
 
   # Set up a board starting with a position after the moves.
   # Failing to parse the moves raises an ArgumentError.
-  # @param moves an array of moves. ex. ["+7776FU", "-3334FU"]
+  # @param moves an array of moves. ex. ["+7776FU", "-3334FU"] or
+  #        an array of arrays. ex. [["+7776FU","T2"], ["-3334FU","T5"]]
   #
   def set_from_moves(moves)
     initial()
     return :normal if moves.empty?
     rt = nil
     moves.each do |move|
-      rt = handle_one_move(move, @teban)
+      rt = nil
+      case move
+      when Array
+        rt = handle_one_move(move[0], @teban)
+      when String
+        rt = handle_one_move(move, @teban)
+      end
       raise ArgumentError, "bad moves: #{moves}" unless rt == :normal
     end
     @initial_moves = moves.dup
@@ -588,6 +639,7 @@ EOF
   #   - :uchifuzume 
   #   - :oute_kaihimore 
   #   - (:outori will not be returned)
+  #   - :max_moves
   #
   def handle_one_move(str, sente=nil)
     if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
@@ -646,18 +698,18 @@ EOF
       return :illegal           # can't put on existing piece
     end
 
-    move = Move.new(x0, y0, x1, y1, name, sente)
-    result = move_to(move)
+    @move = Move.new(x0, y0, x1, y1, name, sente)
+    result = move_to(@move)
     if (result == :illegal)
       # self is unchanged
       return :illegal 
     end
     if (checkmated?(sente))
-      move_back(move)
+      move_back(@move)
       return :oute_kaihimore 
     end
     if ((x0 == 0) && (y0 == 0) && (name == "FU") && uchifuzume?(sente))
-      move_back(move)
+      move_back(@move)
       return :uchifuzume
     end
 
@@ -665,16 +717,23 @@ EOF
     update_sennichite(sente)
     os_result = oute_sennichite?(sente)
     if os_result # :oute_sennichite_sente_lose or :oute_sennichite_gote_lose
-      move_back(move)
+      move_back(@move)
       restore_sennichite_stuff(*sennichite_stuff)
       return os_result 
     end
     if sennichite?
-      move_back(move)
+      move_back(@move)
       restore_sennichite_stuff(*sennichite_stuff)
       return :sennichite 
     end
 
+    # New rule that CSA introduced in November 2014.
+    # If a game with 256 plies does not end, make the game a draw.
+    # When running test cases $options might be nil.
+    if @max_moves > 0 && @move_count >= @max_moves
+      return :max_moves
+    end
+
     return :normal
   end