shogi-server source
修订版 | 728a7894c2ec33a0a339d905249f5c3751bec7d7 (tree) |
---|---|
时间 | 2008-10-19 23:02:29 |
作者 | beatles <beatles@b8c6...> |
Commiter | beatles |
Did some refactorings.
@@ -1,29 +1,32 @@ | ||
1 | -#!/usr/bin/env ruby | |
2 | -# This generates graphs of evaluation values from comments in CSA files. | |
3 | -# Ruby libraries that are required: | |
4 | -# - RubyGems: http://rubyforge.org/projects/rubygems/ | |
5 | -# - rgplot: http://rubyforge.org/projects/rgplot/ | |
6 | -# OS librariles that is required: | |
7 | -# - Gnuplot: http://www.gnuplot.info/ | |
8 | -# On Debian, $ sudo apt-get install gnuplot | |
9 | -# | |
10 | -# Copyright (C) 2006 Daigo Moriwaki <daigo@debian.org> | |
1 | +#!/usr/bin/ruby | |
2 | +# This generates graphs of evaluation values from comments in CSA files. | |
3 | +# Ruby libraries that are required: | |
4 | +# * RubyGems: http://rubyforge.org/projects/rubygems/ | |
5 | +# * rgplot: http://rubyforge.org/projects/rgplot/ | |
6 | +# OS librariles that is required: | |
7 | +# * Gnuplot: http://www.gnuplot.info/ | |
8 | +# * On Debian, $ sudo apt-get install gnuplot | |
11 | 9 | # |
12 | -# Version: $Id$ | |
10 | +# Author:: Daigo Moriwaki <daigo at debian dot org> | |
11 | +# Copyright:: Copyright (C) 2006-2008 Daigo Moriwaki <daigo at debian dot org> | |
13 | 12 | # |
14 | -# This program is free software; you can redistribute it and/or modify | |
15 | -# it under the terms of the GNU General Public License as published by | |
16 | -# the Free Software Foundation; either version 2 of the License, or | |
17 | -# (at your option) any later version. | |
13 | +# $Id$ | |
18 | 14 | # |
19 | -# This program is distributed in the hope that it will be useful, | |
20 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | -# GNU General Public License for more details. | |
15 | +#-- | |
16 | +# This program is free software; you can redistribute it and/or modify | |
17 | +# it under the terms of the GNU General Public License as published by | |
18 | +# the Free Software Foundation; either version 2 of the License, or | |
19 | +# (at your option) any later version. | |
23 | 20 | # |
24 | -# You should have received a copy of the GNU General Public License | |
25 | -# along with this program; if not, write to the Free Software | |
26 | -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | +# This program is distributed in the hope that it will be useful, | |
22 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | +# GNU General Public License for more details. | |
25 | +# | |
26 | +# You should have received a copy of the GNU General Public License | |
27 | +# along with this program; if not, write to the Free Software | |
28 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
29 | +#++ | |
27 | 30 | |
28 | 31 | require 'pathname' |
29 | 32 | require 'getoptlong' |
@@ -40,6 +43,16 @@ def reformat_svg(str) | ||
40 | 43 | END |
41 | 44 | end |
42 | 45 | |
46 | +# Parse play time from the game_name, then return it. If the game_name is | |
47 | +# not valid, return 0. | |
48 | +# | |
49 | +def play_time(game_name) | |
50 | + if /.*?\+.*?\-(\d*?)\-/ =~ game_name | |
51 | + return $1.to_i | |
52 | + end | |
53 | + return 0 | |
54 | +end | |
55 | + | |
43 | 56 | module EvalGraph |
44 | 57 | def parse_comment(str) |
45 | 58 | return nil unless str |
@@ -62,9 +75,10 @@ module EvalGraph | ||
62 | 75 | @comments = [] |
63 | 76 | @times = [] |
64 | 77 | @type = type |
65 | - @regexp_move = Regexp.new("^\\#{@type}\\d{4}\\w{2}") | |
66 | - @regexp_name = Regexp.new("^N\\#{@type}(.*)") | |
67 | - @regexp_time = Regexp.new(/^T(\d+)/) | |
78 | + @regexp_move = Regexp.new("^\\#{@type}\\d{4}\\w{2}") | |
79 | + @regexp_name = Regexp.new("^N\\#{@type}(.*)") | |
80 | + @regexp_time = Regexp.new(/^T(\d+)/) | |
81 | + @regexp_comment = Regexp.new(/^'\*\*(.*)/) | |
68 | 82 | @flag = false |
69 | 83 | @name = nil |
70 | 84 | end |
@@ -85,7 +99,7 @@ module EvalGraph | ||
85 | 99 | if @flag |
86 | 100 | @times << $1.to_i |
87 | 101 | end |
88 | - when /^'\*\*(.*)/ | |
102 | + when @regexp_comment | |
89 | 103 | if @flag |
90 | 104 | @comments << EvalGraph::parse_comment($1) |
91 | 105 | @flag = false |
@@ -97,10 +111,13 @@ module EvalGraph | ||
97 | 111 | end |
98 | 112 | end |
99 | 113 | |
100 | - def time_values(y_max, full_time) | |
101 | - consume = full_time | |
114 | + # Return times for each move which the player played. | |
115 | + # return[0] is the initial play_time. | |
116 | + # | |
117 | + def time_values(y_max, play_time) | |
118 | + consume = play_time | |
102 | 119 | values = [] |
103 | - values << 1.0*y_max/full_time*consume | |
120 | + values << 1.0*y_max/play_time*consume | |
104 | 121 | @times.each do |t| |
105 | 122 | if consume == 0 |
106 | 123 | break |
@@ -109,12 +126,11 @@ module EvalGraph | ||
109 | 126 | if consume < 0 |
110 | 127 | consume = 0 |
111 | 128 | end |
112 | - values << 1.0*y_max/full_time*consume | |
129 | + values << 1.0*y_max/play_time*consume | |
113 | 130 | end |
114 | 131 | return values |
115 | 132 | end |
116 | - | |
117 | - end | |
133 | + end # Player | |
118 | 134 | |
119 | 135 | class Black < Player |
120 | 136 | def name |
@@ -132,7 +148,8 @@ module EvalGraph | ||
132 | 148 | [moves, comments.compact.unshift(0)] |
133 | 149 | end |
134 | 150 | |
135 | - def time_values(y_max, full_time) | |
151 | + # Return moves and times. For example, [[0,1,3], [900, 899, 898]] | |
152 | + def time_values(y_max, play_time) | |
136 | 153 | values = super |
137 | 154 | moves = [0] |
138 | 155 | return [moves, values] if values.size <= 1 |
@@ -144,7 +161,7 @@ module EvalGraph | ||
144 | 161 | end |
145 | 162 | return [moves, values] |
146 | 163 | end |
147 | - end | |
164 | + end # Black | |
148 | 165 | |
149 | 166 | class White < Player |
150 | 167 | def name |
@@ -160,7 +177,7 @@ module EvalGraph | ||
160 | 177 | [moves, comments.compact.unshift(0)] |
161 | 178 | end |
162 | 179 | |
163 | - def time_values(y_max, full_time) | |
180 | + def time_values(y_max, play_time) | |
164 | 181 | values = super |
165 | 182 | moves = [0] |
166 | 183 | return [moves, values] if values.size <= 1 |
@@ -171,8 +188,8 @@ module EvalGraph | ||
171 | 188 | i += 2 |
172 | 189 | end |
173 | 190 | return [moves, values] |
174 | - end | |
175 | - end | |
191 | + end | |
192 | + end # White | |
176 | 193 | |
177 | 194 | |
178 | 195 | def create_players |
@@ -183,14 +200,8 @@ module EvalGraph | ||
183 | 200 | return black,white |
184 | 201 | end |
185 | 202 | module_function :create_players |
186 | -end | |
203 | +end # module EvalGraph | |
187 | 204 | |
188 | -def max_time(game_name) | |
189 | - if /.*?\+.*?\-(\d*?)\-/ =~ game_name | |
190 | - return $1.to_i | |
191 | - end | |
192 | - return 0 | |
193 | -end | |
194 | 205 | |
195 | 206 | def plot(csa_file, title, black, white) |
196 | 207 | width = [black.comments.size, white.comments.size].max * 2 + 1 |
@@ -214,7 +225,7 @@ def plot(csa_file, title, black, white) | ||
214 | 225 | plot.key "left" |
215 | 226 | |
216 | 227 | plot.style "line 1 linewidth 5 linetype 0 linecolor rgbcolor \"red\"" |
217 | - plot.style "line 2 linewidth 5 linetype 0 linecolor rgbcolor \"blue\"" | |
228 | + plot.style "line 2 linewidth 4 linetype 0 linecolor rgbcolor \"blue\"" | |
218 | 229 | |
219 | 230 | plot.data << Gnuplot::DataSet.new( black.eval_values ) do |ds| |
220 | 231 | ds.with = "lines ls 1" |
@@ -226,20 +237,21 @@ def plot(csa_file, title, black, white) | ||
226 | 237 | ds.title = white.name |
227 | 238 | end |
228 | 239 | |
229 | - full_time = max_time(csa_file) | |
230 | - if full_time > 0 | |
240 | + a_play_time = play_time(csa_file) | |
241 | + if a_play_time > 0 | |
231 | 242 | plot.style "line 5 linewidth 1 linetype 0 linecolor rgbcolor \"red\"" |
232 | 243 | plot.style "line 6 linewidth 1 linetype 0 linecolor rgbcolor \"blue\"" |
233 | 244 | plot.style "fill solid 0.25 noborder" |
234 | 245 | |
235 | - plot.data << Gnuplot::DataSet.new( black.time_values(2000, full_time) ) do |ds| | |
246 | + plot.data << Gnuplot::DataSet.new( black.time_values(2000, a_play_time) ) do |ds| | |
236 | 247 | ds.with = "boxes notitle ls 5" |
237 | 248 | end |
238 | 249 | |
239 | - plot.data << Gnuplot::DataSet.new( white.time_values(-2000, full_time) ) do |ds| | |
250 | + plot.data << Gnuplot::DataSet.new( white.time_values(-2000, a_play_time) ) do |ds| | |
240 | 251 | ds.with = "boxes notitle ls 6" |
241 | 252 | end |
242 | - end | |
253 | + end # if | |
254 | + | |
243 | 255 | end |
244 | 256 | end |
245 | 257 | end |