require 'shogi_server/league/floodgate'
require 'shogi_server/game_result'
+require 'shogi_server/util'
module ShogiServer # for a namespace
@@mutex = Mutex.new
@@time = 0
+
+ # Decide an actual turn of each player according to their turn preferences.
+ # p2 is a rival player of the p1 player.
+ # p1_sente_string must be "*", "+" or "-".
+ # After this call, the sente value of each player is always true or false, not
+ # nil.
+ #
+ def Game.decide_turns(p1, p1_sente_string, p2)
+ if ((p1_sente_string == "*") && (p2.sente == nil))
+ if (rand(2) == 0)
+ p1.sente = true
+ p2.sente = false
+ else
+ p1.sente = false
+ p2.sente = true
+ end
+ elsif (p2.sente == true) # rival has higher priority
+ p1.sente = false
+ elsif (p2.sente == false)
+ p1.sente = true
+ elsif (p1_sente_string == "+")
+ p1.sente = true
+ p2.sente = false
+ elsif (p1_sente_string == "-")
+ p1.sente = false
+ p2.sente = true
+ else
+ ## never reached
+ end
+ end
+
+
def initialize(game_name, player0, player1, board)
@monitors = Array::new # array of MonitorHandler*
@game_name = game_name
@sente.game = self
@gote.game = self
- @last_move = @board.initial_moves.empty? ? "" : "%s,T1" % [@board.initial_moves.last]
+ @last_move = ""
+ unless @board.initial_moves.empty?
+ last_move = @board.initial_moves.last
+ case last_move
+ when Array
+ @last_move = last_move.join(",")
+ when String
+ @last_move = "%s,T1" % [last_move]
+ end
+ end
@current_turn = @board.initial_moves.size
@sente.status = "agree_waiting"
@prepared_time.strftime("%Y"),
@prepared_time.strftime("%m"),
@prepared_time.strftime("%d"))
- FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
@logfile = File.join(log_dir_name, @game_id + ".csa")
+ Mkdir.mkdir_for(@logfile)
$league.games[@game_id] = self
@result.process if @result
finish() if finish_flag
@current_player, @next_player = @next_player, @current_player
- @start_time = Time::new
+ @start_time = Time.now
return finish_flag
end
@gote.write_safe(sprintf("START:%s\n", @game_id))
@sente.mytime = @total_time
@gote.mytime = @total_time
- @start_time = Time::new
+ @start_time = Time.now
end
def propose
@sente.write_safe(propose_message("+"))
@gote.write_safe(propose_message("-"))
- now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
+ now = Time.now.strftime("%Y/%m/%d %H:%M:%S")
@fh.puts("$START_TIME:#{now}")
- @fh.print <<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
+ @fh.print(@board.initial_string)
if rated?
black_name = @sente.rated? ? @sente.player_id : @sente.name
white_name = @gote.rated? ? @gote.player_id : @gote.name
unless @board.initial_moves.empty?
@fh.puts "'buoy game starting with %d moves" % [@board.initial_moves.size]
@board.initial_moves.each do |move|
- @fh.puts move
- @fh.puts "T1"
+ case move
+ when Array
+ @fh.puts move[0]
+ @fh.puts move[1]
+ when String
+ @fh.puts move
+ @fh.puts "T1"
+ end
end
end
end
Least_Time_Per_Move:#{Least_Time_Per_Move}
END Time
BEGIN Position
-#{@board.to_s.chomp}
+#{@board.initial_string.chomp}
+#{@board.initial_moves.collect do |m|
+ case m
+ when Array
+ m.join(",")
+ when String
+ m + ",T1"
+ end
+end.join("\n")}
END Position
END Game_Summary
EOM
- return str
+ # An empty @board.initial_moves causes an empty line, which should be
+ # eliminated.
+ return str.gsub("\n\n", "\n")
end
def prepared_expire?
return false
end
+
+ # Read the .csa file and returns an array of moves and times.
+ # ex. [["+7776FU","T2"], ["-3334FU","T5"]]
+ #
+ def read_moves
+ ret = []
+ IO.foreach(@logfile) do |line|
+ if /^[\+\-]\d{4}[A-Z]{2}/ =~ line
+ ret << [line.chomp]
+ elsif /^T\d*/ =~ line
+ ret[-1] << line.chomp
+ end
+ end
+ return ret
+ end
private
def issue_current_time
- time = Time::new.strftime("%Y%m%d%H%M%S").to_i
+ time = Time.now.strftime("%Y%m%d%H%M%S").to_i
@@mutex.synchronize do
while time <= @@time do
time += 1