OSDN Git Service

util/statistics.rb: Updated to investigate floodgate-600-10 games.
[shogi-server/shogi-server.git] / test / TC_game.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'test/mock_player'
4 require 'shogi_server/board'
5 require 'shogi_server/game'
6 require 'shogi_server/player'
7
8 $options = {}
9 $options["least-time-per-move"] = 1
10
11 def log_message(str)
12   $stderr.puts str
13 end
14
15 def log_warning(str)
16   $stderr.puts str
17 end
18
19 def log_error(str)
20   $stderr.puts str
21 end
22
23 $league = ShogiServer::League.new(File.dirname(__FILE__))
24 $league.event = "test"
25
26 class TestGame < Test::Unit::TestCase
27
28   def test_new
29     game_name = "hoge-1500-0"
30     board = ShogiServer::Board.new
31     board.initial
32     p1 = MockPlayer.new
33     p1.sente = true
34     p1.name  = "p1"
35     p2 = MockPlayer.new
36     p2.sente = false
37     p2.name  = "p2"
38     
39     game = ShogiServer::Game.new game_name, p1, p2, board 
40     assert_equal "", game.last_move
41
42     p1_out = <<EOF
43 BEGIN Game_Summary
44 Protocol_Version:1.1
45 Protocol_Mode:Server
46 Format:Shogi 1.0
47 Declaration:Jishogi 1.1
48 Game_ID:#{game.game_id}
49 Name+:p1
50 Name-:p2
51 Your_Turn:+
52 Rematch_On_Draw:NO
53 To_Move:+
54 BEGIN Time
55 Time_Unit:1sec
56 Total_Time:1500
57 Byoyomi:0
58 Least_Time_Per_Move:#{$options["least-time-per-move"]}
59 END Time
60 BEGIN Position
61 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
62 P2 * -HI *  *  *  *  * -KA * 
63 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
64 P4 *  *  *  *  *  *  *  *  * 
65 P5 *  *  *  *  *  *  *  *  * 
66 P6 *  *  *  *  *  *  *  *  * 
67 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
68 P8 * +KA *  *  *  *  * +HI * 
69 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
70 +
71 END Position
72 END Game_Summary
73 EOF
74     assert_equal(p1_out, p1.out.first)
75
76     p2_out = <<EOF
77 BEGIN Game_Summary
78 Protocol_Version:1.1
79 Protocol_Mode:Server
80 Format:Shogi 1.0
81 Declaration:Jishogi 1.1
82 Game_ID:#{game.game_id}
83 Name+:p1
84 Name-:p2
85 Your_Turn:-
86 Rematch_On_Draw:NO
87 To_Move:+
88 BEGIN Time
89 Time_Unit:1sec
90 Total_Time:1500
91 Byoyomi:0
92 Least_Time_Per_Move:#{$options["least-time-per-move"]}
93 END Time
94 BEGIN Position
95 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
96 P2 * -HI *  *  *  *  * -KA * 
97 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
98 P4 *  *  *  *  *  *  *  *  * 
99 P5 *  *  *  *  *  *  *  *  * 
100 P6 *  *  *  *  *  *  *  *  * 
101 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
102 P8 * +KA *  *  *  *  * +HI * 
103 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
104 +
105 END Position
106 END Game_Summary
107 EOF
108     assert_equal(p2_out, p2.out.first)
109
110     file = Pathname.new(game.logfile)
111     log = file.read
112     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
113 V2
114 N+p1
115 N-p2
116 $EVENT:#{game.game_id}
117 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
118 P2 * -HI *  *  *  *  * -KA * 
119 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
120 P4 *  *  *  *  *  *  *  *  * 
121 P5 *  *  *  *  *  *  *  *  * 
122 P6 *  *  *  *  *  *  *  *  * 
123 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
124 P8 * +KA *  *  *  *  * +HI * 
125 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
126 +
127 EOF
128   end
129
130   def test_new_buoy_1_move
131     game_name = "buoyhoge-1500-0"
132     board = ShogiServer::Board.new
133     board.set_from_moves ["+7776FU"]
134     p1 = MockPlayer.new
135     p1.sente = true
136     p1.name  = "p1"
137     p2 = MockPlayer.new
138     p2.sente = false
139     p2.name  = "p2"
140     
141     game = ShogiServer::Game.new game_name, p1, p2, board 
142     assert_equal "+7776FU,T1", game.last_move
143
144     p1_out = <<EOF
145 BEGIN Game_Summary
146 Protocol_Version:1.1
147 Protocol_Mode:Server
148 Format:Shogi 1.0
149 Declaration:Jishogi 1.1
150 Game_ID:#{game.game_id}
151 Name+:p1
152 Name-:p2
153 Your_Turn:+
154 Rematch_On_Draw:NO
155 To_Move:-
156 BEGIN Time
157 Time_Unit:1sec
158 Total_Time:1500
159 Byoyomi:0
160 Least_Time_Per_Move:#{$options["least-time-per-move"]}
161 END Time
162 BEGIN Position
163 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
164 P2 * -HI *  *  *  *  * -KA * 
165 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
166 P4 *  *  *  *  *  *  *  *  * 
167 P5 *  *  *  *  *  *  *  *  * 
168 P6 *  *  *  *  *  *  *  *  * 
169 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
170 P8 * +KA *  *  *  *  * +HI * 
171 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
172 +
173 +7776FU,T1
174 END Position
175 END Game_Summary
176 EOF
177     assert_equal(p1_out, p1.out.first)
178
179     p2_out = <<EOF
180 BEGIN Game_Summary
181 Protocol_Version:1.1
182 Protocol_Mode:Server
183 Format:Shogi 1.0
184 Declaration:Jishogi 1.1
185 Game_ID:#{game.game_id}
186 Name+:p1
187 Name-:p2
188 Your_Turn:-
189 Rematch_On_Draw:NO
190 To_Move:-
191 BEGIN Time
192 Time_Unit:1sec
193 Total_Time:1500
194 Byoyomi:0
195 Least_Time_Per_Move:#{$options["least-time-per-move"]}
196 END Time
197 BEGIN Position
198 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
199 P2 * -HI *  *  *  *  * -KA * 
200 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
201 P4 *  *  *  *  *  *  *  *  * 
202 P5 *  *  *  *  *  *  *  *  * 
203 P6 *  *  *  *  *  *  *  *  * 
204 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
205 P8 * +KA *  *  *  *  * +HI * 
206 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
207 +
208 +7776FU,T1
209 END Position
210 END Game_Summary
211 EOF
212     assert_equal(p2_out, p2.out.first)
213
214     file = Pathname.new(game.logfile)
215     log = file.read
216     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
217 V2
218 N+p1
219 N-p2
220 $EVENT:#{game.game_id}
221 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
222 P2 * -HI *  *  *  *  * -KA * 
223 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
224 P4 *  *  *  *  *  *  *  *  * 
225 P5 *  *  *  *  *  *  *  *  * 
226 P6 *  *  *  *  *  *  *  *  * 
227 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
228 P8 * +KA *  *  *  *  * +HI * 
229 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
230 +
231 'buoy game starting with 1 moves
232 +7776FU
233 T1
234 EOF
235   end
236
237   def test_new_buoy_2_moves
238     game_name = "buoyhoge-1500-0"
239     board = ShogiServer::Board.new
240     board.set_from_moves ["+7776FU", "-3334FU"]
241     p1 = MockPlayer.new
242     p1.sente = true
243     p1.name  = "p1"
244     p2 = MockPlayer.new
245     p2.sente = false
246     p2.name  = "p2"
247     
248     game = ShogiServer::Game.new game_name, p1, p2, board 
249     assert_equal "-3334FU,T1", game.last_move
250
251     p1_out = <<EOF
252 BEGIN Game_Summary
253 Protocol_Version:1.1
254 Protocol_Mode:Server
255 Format:Shogi 1.0
256 Declaration:Jishogi 1.1
257 Game_ID:#{game.game_id}
258 Name+:p1
259 Name-:p2
260 Your_Turn:+
261 Rematch_On_Draw:NO
262 To_Move:+
263 BEGIN Time
264 Time_Unit:1sec
265 Total_Time:1500
266 Byoyomi:0
267 Least_Time_Per_Move:#{$options["least-time-per-move"]}
268 END Time
269 BEGIN Position
270 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
271 P2 * -HI *  *  *  *  * -KA * 
272 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
273 P4 *  *  *  *  *  *  *  *  * 
274 P5 *  *  *  *  *  *  *  *  * 
275 P6 *  *  *  *  *  *  *  *  * 
276 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
277 P8 * +KA *  *  *  *  * +HI * 
278 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
279 +
280 +7776FU,T1
281 -3334FU,T1
282 END Position
283 END Game_Summary
284 EOF
285     assert_equal(p1_out, p1.out.first)
286
287     p2_out = <<EOF
288 BEGIN Game_Summary
289 Protocol_Version:1.1
290 Protocol_Mode:Server
291 Format:Shogi 1.0
292 Declaration:Jishogi 1.1
293 Game_ID:#{game.game_id}
294 Name+:p1
295 Name-:p2
296 Your_Turn:-
297 Rematch_On_Draw:NO
298 To_Move:+
299 BEGIN Time
300 Time_Unit:1sec
301 Total_Time:1500
302 Byoyomi:0
303 Least_Time_Per_Move:#{$options["least-time-per-move"]}
304 END Time
305 BEGIN Position
306 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
307 P2 * -HI *  *  *  *  * -KA * 
308 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
309 P4 *  *  *  *  *  *  *  *  * 
310 P5 *  *  *  *  *  *  *  *  * 
311 P6 *  *  *  *  *  *  *  *  * 
312 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
313 P8 * +KA *  *  *  *  * +HI * 
314 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
315 +
316 +7776FU,T1
317 -3334FU,T1
318 END Position
319 END Game_Summary
320 EOF
321     assert_equal(p2_out, p2.out.first)
322
323     file = Pathname.new(game.logfile)
324     log = file.read
325     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
326 V2
327 N+p1
328 N-p2
329 $EVENT:#{game.game_id}
330 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
331 P2 * -HI *  *  *  *  * -KA * 
332 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
333 P4 *  *  *  *  *  *  *  *  * 
334 P5 *  *  *  *  *  *  *  *  * 
335 P6 *  *  *  *  *  *  *  *  * 
336 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
337 P8 * +KA *  *  *  *  * +HI * 
338 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
339 +
340 'buoy game starting with 2 moves
341 +7776FU
342 T1
343 -3334FU
344 T1
345 EOF
346   end
347   
348   def test_monitor_add
349     game_name = "hoge-1500-0"
350     board = ShogiServer::Board.new
351     board.initial
352     p1 = MockPlayer.new
353     p1.sente = true
354     p1.name  = "p1"
355     p2 = MockPlayer.new
356     p2.sente = false
357     p2.name  = "p2"
358     
359     game = ShogiServer::Game.new game_name, p1, p2, board 
360     handler1 = ShogiServer::MonitorHandler1.new p1
361     handler2 = ShogiServer::MonitorHandler2.new p2
362
363     assert_equal(0, game.monitors.size)
364     game.monitoron(handler1)
365     assert_equal(1, game.monitors.size)
366     game.monitoron(handler2)
367     assert_equal(2, game.monitors.size)
368     game.monitoroff(handler1)
369     assert_equal(1, game.monitors.size)
370     assert_equal(handler2, game.monitors.last)
371     game.monitoroff(handler2)
372     assert_equal(0, game.monitors.size)
373   end
374
375   def test_decide_turns
376     p1 = MockPlayer.new
377     p1.name = "p1"
378     p2 = MockPlayer.new
379     p2.name = "p2"
380
381     p1.sente=nil; p2.sente=false
382     ShogiServer::Game::decide_turns(p1, "+", p2)
383     assert_equal true, p1.sente
384
385     p1.sente=nil; p2.sente=nil
386     ShogiServer::Game::decide_turns(p1, "+", p2)
387     assert_equal true, p1.sente
388
389     p1.sente=nil; p2.sente=true
390     ShogiServer::Game::decide_turns(p1, "-", p2)
391     assert_equal false, p1.sente
392
393     p1.sente=nil; p2.sente=nil
394     ShogiServer::Game::decide_turns(p1, "-", p2)
395     assert_equal false, p1.sente
396
397     p1.sente=nil; p2.sente=false
398     ShogiServer::Game::decide_turns(p1, "*", p2)
399     assert_equal true, p1.sente
400
401     p1.sente=nil; p2.sente=true
402     ShogiServer::Game::decide_turns(p1, "*", p2)
403     assert_equal false, p1.sente
404
405     p1.sente=nil; p2.sente=nil
406     ShogiServer::Game::decide_turns(p1, "*", p2)
407     assert (p1.sente == true  && p2.sente == false) ||
408            (p1.sente == false && p2.sente == true)
409   end
410 end
411