OSDN Git Service

[shogi-server] Write more game results in record files
[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 $options["max-moves"] = 0
11
12 def log_message(str)
13   $stderr.puts str
14 end
15
16 def log_warning(str)
17   $stderr.puts str
18 end
19
20 def log_error(str)
21   $stderr.puts str
22 end
23
24 $league = ShogiServer::League.new(File.dirname(__FILE__))
25 $league.event = "test"
26
27 class TestGame < Test::Unit::TestCase
28
29   def test_parse_time
30     assert_equal ShogiServer::Game::TimeControlParams.new(1500,0,0,false,false),
31                  ShogiServer::Game.parse_time("hoge-1500-0")
32     assert_equal ShogiServer::Game::TimeControlParams.new(600, 0, 10, false, false),
33                  ShogiServer::Game.parse_time("hoge-600-10F")
34     assert_equal true, ShogiServer::Game.parse_time("hoge-600-10f").error
35   end
36
37   def test_new
38     game_name = "hoge-1500-0"
39     board = ShogiServer::Board.new
40     board.initial
41     p1 = MockPlayer.new
42     p1.sente = true
43     p1.name  = "p1"
44     p2 = MockPlayer.new
45     p2.sente = false
46     p2.name  = "p2"
47     
48     game = ShogiServer::Game.new game_name, p1, p2, board 
49     assert_equal "", game.last_move
50
51     p1_out = <<EOF
52 BEGIN Game_Summary
53 Protocol_Version:1.2
54 Protocol_Mode:Server
55 Format:Shogi 1.0
56 Declaration:Jishogi 1.1
57 Game_ID:#{game.game_id}
58 Name+:p1
59 Name-:p2
60 Your_Turn:+
61 Rematch_On_Draw:NO
62 To_Move:+
63 Max_Moves:#{$options["max-moves"]}
64 BEGIN Time
65 Time_Unit:1sec
66 Total_Time:1500
67 Byoyomi:0
68 Increment:0
69 Least_Time_Per_Move:#{$options["least-time-per-move"]}
70 END Time
71 BEGIN Position
72 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
73 P2 * -HI *  *  *  *  * -KA * 
74 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
75 P4 *  *  *  *  *  *  *  *  * 
76 P5 *  *  *  *  *  *  *  *  * 
77 P6 *  *  *  *  *  *  *  *  * 
78 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
79 P8 * +KA *  *  *  *  * +HI * 
80 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
81 +
82 END Position
83 END Game_Summary
84 EOF
85     assert_equal(p1_out, p1.out.first)
86
87     p2_out = <<EOF
88 BEGIN Game_Summary
89 Protocol_Version:1.2
90 Protocol_Mode:Server
91 Format:Shogi 1.0
92 Declaration:Jishogi 1.1
93 Game_ID:#{game.game_id}
94 Name+:p1
95 Name-:p2
96 Your_Turn:-
97 Rematch_On_Draw:NO
98 To_Move:+
99 Max_Moves:#{$options["max-moves"]}
100 BEGIN Time
101 Time_Unit:1sec
102 Total_Time:1500
103 Byoyomi:0
104 Increment:0
105 Least_Time_Per_Move:#{$options["least-time-per-move"]}
106 END Time
107 BEGIN Position
108 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
109 P2 * -HI *  *  *  *  * -KA * 
110 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
111 P4 *  *  *  *  *  *  *  *  * 
112 P5 *  *  *  *  *  *  *  *  * 
113 P6 *  *  *  *  *  *  *  *  * 
114 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
115 P8 * +KA *  *  *  *  * +HI * 
116 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
117 +
118 END Position
119 END Game_Summary
120 EOF
121     assert_equal(p2_out, p2.out.first)
122
123     file = Pathname.new(game.logfile)
124     log = file.read
125     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
126 V2
127 N+p1
128 N-p2
129 'Max_Moves:#{$options["max-moves"]}
130 'Least_Time_Per_Move:#{$options["least-time-per-move"]}
131 $EVENT:#{game.game_id}
132 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
133 P2 * -HI *  *  *  *  * -KA * 
134 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
135 P4 *  *  *  *  *  *  *  *  * 
136 P5 *  *  *  *  *  *  *  *  * 
137 P6 *  *  *  *  *  *  *  *  * 
138 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
139 P8 * +KA *  *  *  *  * +HI * 
140 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
141 +
142 EOF
143   end
144
145   def test_new_fischer
146     game_name = "hoge-600-10F"
147     board = ShogiServer::Board.new
148     board.initial
149     p1 = MockPlayer.new
150     p1.sente = true
151     p1.name  = "p1"
152     p2 = MockPlayer.new
153     p2.sente = false
154     p2.name  = "p2"
155
156     game = ShogiServer::Game.new game_name, p1, p2, board
157     assert_equal "", game.last_move
158
159     p1_out = <<EOF
160 BEGIN Game_Summary
161 Protocol_Version:1.2
162 Protocol_Mode:Server
163 Format:Shogi 1.0
164 Declaration:Jishogi 1.1
165 Game_ID:#{game.game_id}
166 Name+:p1
167 Name-:p2
168 Your_Turn:+
169 Rematch_On_Draw:NO
170 To_Move:+
171 Max_Moves:#{$options["max-moves"]}
172 BEGIN Time
173 Time_Unit:1sec
174 Total_Time:600
175 Byoyomi:0
176 Increment:10
177 Least_Time_Per_Move:#{$options["least-time-per-move"]}
178 END Time
179 BEGIN Position
180 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
181 P2 * -HI *  *  *  *  * -KA * 
182 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
183 P4 *  *  *  *  *  *  *  *  * 
184 P5 *  *  *  *  *  *  *  *  * 
185 P6 *  *  *  *  *  *  *  *  * 
186 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
187 P8 * +KA *  *  *  *  * +HI * 
188 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
189 +
190 END Position
191 END Game_Summary
192 EOF
193     assert_equal(p1_out, p1.out.first)
194
195     p2_out = <<EOF
196 BEGIN Game_Summary
197 Protocol_Version:1.2
198 Protocol_Mode:Server
199 Format:Shogi 1.0
200 Declaration:Jishogi 1.1
201 Game_ID:#{game.game_id}
202 Name+:p1
203 Name-:p2
204 Your_Turn:-
205 Rematch_On_Draw:NO
206 To_Move:+
207 Max_Moves:#{$options["max-moves"]}
208 BEGIN Time
209 Time_Unit:1sec
210 Total_Time:600
211 Byoyomi:0
212 Increment:10
213 Least_Time_Per_Move:#{$options["least-time-per-move"]}
214 END Time
215 BEGIN Position
216 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
217 P2 * -HI *  *  *  *  * -KA * 
218 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
219 P4 *  *  *  *  *  *  *  *  * 
220 P5 *  *  *  *  *  *  *  *  * 
221 P6 *  *  *  *  *  *  *  *  * 
222 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
223 P8 * +KA *  *  *  *  * +HI * 
224 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
225 +
226 END Position
227 END Game_Summary
228 EOF
229     assert_equal(p2_out, p2.out.first)
230
231     file = Pathname.new(game.logfile)
232     log = file.read
233     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
234 V2
235 N+p1
236 N-p2
237 'Max_Moves:#{$options["max-moves"]}
238 'Least_Time_Per_Move:#{$options["least-time-per-move"]}
239 'Increment:10
240 $EVENT:#{game.game_id}
241 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
242 P2 * -HI *  *  *  *  * -KA * 
243 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
244 P4 *  *  *  *  *  *  *  *  * 
245 P5 *  *  *  *  *  *  *  *  * 
246 P6 *  *  *  *  *  *  *  *  * 
247 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
248 P8 * +KA *  *  *  *  * +HI * 
249 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
250 +
251 EOF
252   end
253
254   def test_new_buoy_1_move
255     game_name = "buoyhoge-1500-0"
256     board = ShogiServer::Board.new
257     board.set_from_moves ["+7776FU"]
258     p1 = MockPlayer.new
259     p1.sente = true
260     p1.name  = "p1"
261     p2 = MockPlayer.new
262     p2.sente = false
263     p2.name  = "p2"
264     
265     game = ShogiServer::Game.new game_name, p1, p2, board 
266     assert_equal "+7776FU,T1", game.last_move
267
268     p1_out = <<EOF
269 BEGIN Game_Summary
270 Protocol_Version:1.2
271 Protocol_Mode:Server
272 Format:Shogi 1.0
273 Declaration:Jishogi 1.1
274 Game_ID:#{game.game_id}
275 Name+:p1
276 Name-:p2
277 Your_Turn:+
278 Rematch_On_Draw:NO
279 To_Move:-
280 Max_Moves:#{$options["max-moves"]}
281 BEGIN Time
282 Time_Unit:1sec
283 Total_Time:1500
284 Byoyomi:0
285 Increment:0
286 Least_Time_Per_Move:#{$options["least-time-per-move"]}
287 END Time
288 BEGIN Position
289 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
290 P2 * -HI *  *  *  *  * -KA * 
291 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
292 P4 *  *  *  *  *  *  *  *  * 
293 P5 *  *  *  *  *  *  *  *  * 
294 P6 *  *  *  *  *  *  *  *  * 
295 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
296 P8 * +KA *  *  *  *  * +HI * 
297 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
298 +
299 +7776FU,T1
300 END Position
301 END Game_Summary
302 EOF
303     assert_equal(p1_out, p1.out.first)
304
305     p2_out = <<EOF
306 BEGIN Game_Summary
307 Protocol_Version:1.2
308 Protocol_Mode:Server
309 Format:Shogi 1.0
310 Declaration:Jishogi 1.1
311 Game_ID:#{game.game_id}
312 Name+:p1
313 Name-:p2
314 Your_Turn:-
315 Rematch_On_Draw:NO
316 To_Move:-
317 Max_Moves:#{$options["max-moves"]}
318 BEGIN Time
319 Time_Unit:1sec
320 Total_Time:1500
321 Byoyomi:0
322 Increment:0
323 Least_Time_Per_Move:#{$options["least-time-per-move"]}
324 END Time
325 BEGIN Position
326 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
327 P2 * -HI *  *  *  *  * -KA * 
328 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
329 P4 *  *  *  *  *  *  *  *  * 
330 P5 *  *  *  *  *  *  *  *  * 
331 P6 *  *  *  *  *  *  *  *  * 
332 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
333 P8 * +KA *  *  *  *  * +HI * 
334 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
335 +
336 +7776FU,T1
337 END Position
338 END Game_Summary
339 EOF
340     assert_equal(p2_out, p2.out.first)
341
342     file = Pathname.new(game.logfile)
343     log = file.read
344     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
345 V2
346 N+p1
347 N-p2
348 'Max_Moves:#{$options["max-moves"]}
349 'Least_Time_Per_Move:#{$options["least-time-per-move"]}
350 $EVENT:#{game.game_id}
351 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
352 P2 * -HI *  *  *  *  * -KA * 
353 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
354 P4 *  *  *  *  *  *  *  *  * 
355 P5 *  *  *  *  *  *  *  *  * 
356 P6 *  *  *  *  *  *  *  *  * 
357 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
358 P8 * +KA *  *  *  *  * +HI * 
359 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
360 +
361 'buoy game starting with 1 moves
362 +7776FU
363 T1
364 EOF
365   end
366
367   def test_new_buoy_2_moves
368     game_name = "buoyhoge-1500-0"
369     board = ShogiServer::Board.new
370     board.set_from_moves ["+7776FU", "-3334FU"]
371     p1 = MockPlayer.new
372     p1.sente = true
373     p1.name  = "p1"
374     p2 = MockPlayer.new
375     p2.sente = false
376     p2.name  = "p2"
377     
378     game = ShogiServer::Game.new game_name, p1, p2, board 
379     assert_equal "-3334FU,T1", game.last_move
380
381     p1_out = <<EOF
382 BEGIN Game_Summary
383 Protocol_Version:1.2
384 Protocol_Mode:Server
385 Format:Shogi 1.0
386 Declaration:Jishogi 1.1
387 Game_ID:#{game.game_id}
388 Name+:p1
389 Name-:p2
390 Your_Turn:+
391 Rematch_On_Draw:NO
392 To_Move:+
393 Max_Moves:#{$options["max-moves"]}
394 BEGIN Time
395 Time_Unit:1sec
396 Total_Time:1500
397 Byoyomi:0
398 Increment:0
399 Least_Time_Per_Move:#{$options["least-time-per-move"]}
400 END Time
401 BEGIN Position
402 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
403 P2 * -HI *  *  *  *  * -KA * 
404 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
405 P4 *  *  *  *  *  *  *  *  * 
406 P5 *  *  *  *  *  *  *  *  * 
407 P6 *  *  *  *  *  *  *  *  * 
408 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
409 P8 * +KA *  *  *  *  * +HI * 
410 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
411 +
412 +7776FU,T1
413 -3334FU,T1
414 END Position
415 END Game_Summary
416 EOF
417     assert_equal(p1_out, p1.out.first)
418
419     p2_out = <<EOF
420 BEGIN Game_Summary
421 Protocol_Version:1.2
422 Protocol_Mode:Server
423 Format:Shogi 1.0
424 Declaration:Jishogi 1.1
425 Game_ID:#{game.game_id}
426 Name+:p1
427 Name-:p2
428 Your_Turn:-
429 Rematch_On_Draw:NO
430 To_Move:+
431 Max_Moves:#{$options["max-moves"]}
432 BEGIN Time
433 Time_Unit:1sec
434 Total_Time:1500
435 Byoyomi:0
436 Increment:0
437 Least_Time_Per_Move:#{$options["least-time-per-move"]}
438 END Time
439 BEGIN Position
440 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
441 P2 * -HI *  *  *  *  * -KA * 
442 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
443 P4 *  *  *  *  *  *  *  *  * 
444 P5 *  *  *  *  *  *  *  *  * 
445 P6 *  *  *  *  *  *  *  *  * 
446 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
447 P8 * +KA *  *  *  *  * +HI * 
448 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
449 +
450 +7776FU,T1
451 -3334FU,T1
452 END Position
453 END Game_Summary
454 EOF
455     assert_equal(p2_out, p2.out.first)
456
457     file = Pathname.new(game.logfile)
458     log = file.read
459     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
460 V2
461 N+p1
462 N-p2
463 'Max_Moves:#{$options["max-moves"]}
464 'Least_Time_Per_Move:#{$options["least-time-per-move"]}
465 $EVENT:#{game.game_id}
466 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
467 P2 * -HI *  *  *  *  * -KA * 
468 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
469 P4 *  *  *  *  *  *  *  *  * 
470 P5 *  *  *  *  *  *  *  *  * 
471 P6 *  *  *  *  *  *  *  *  * 
472 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
473 P8 * +KA *  *  *  *  * +HI * 
474 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
475 +
476 'buoy game starting with 2 moves
477 +7776FU
478 T1
479 -3334FU
480 T1
481 EOF
482   end
483   
484   def test_monitor_add
485     game_name = "hoge-1500-0"
486     board = ShogiServer::Board.new
487     board.initial
488     p1 = MockPlayer.new
489     p1.sente = true
490     p1.name  = "p1"
491     p2 = MockPlayer.new
492     p2.sente = false
493     p2.name  = "p2"
494     
495     game = ShogiServer::Game.new game_name, p1, p2, board 
496     handler1 = ShogiServer::MonitorHandler1.new p1
497     handler2 = ShogiServer::MonitorHandler2.new p2
498
499     assert_equal(0, game.monitors.size)
500     game.monitoron(handler1)
501     assert_equal(1, game.monitors.size)
502     game.monitoron(handler2)
503     assert_equal(2, game.monitors.size)
504     game.monitoroff(handler1)
505     assert_equal(1, game.monitors.size)
506     assert_equal(handler2, game.monitors.last)
507     game.monitoroff(handler2)
508     assert_equal(0, game.monitors.size)
509   end
510
511   def test_decide_turns
512     p1 = MockPlayer.new
513     p1.name = "p1"
514     p2 = MockPlayer.new
515     p2.name = "p2"
516
517     p1.sente=nil; p2.sente=false
518     ShogiServer::Game::decide_turns(p1, "+", p2)
519     assert_equal true, p1.sente
520
521     p1.sente=nil; p2.sente=nil
522     ShogiServer::Game::decide_turns(p1, "+", p2)
523     assert_equal true, p1.sente
524
525     p1.sente=nil; p2.sente=true
526     ShogiServer::Game::decide_turns(p1, "-", p2)
527     assert_equal false, p1.sente
528
529     p1.sente=nil; p2.sente=nil
530     ShogiServer::Game::decide_turns(p1, "-", p2)
531     assert_equal false, p1.sente
532
533     p1.sente=nil; p2.sente=false
534     ShogiServer::Game::decide_turns(p1, "*", p2)
535     assert_equal true, p1.sente
536
537     p1.sente=nil; p2.sente=true
538     ShogiServer::Game::decide_turns(p1, "*", p2)
539     assert_equal false, p1.sente
540
541     p1.sente=nil; p2.sente=nil
542     ShogiServer::Game::decide_turns(p1, "*", p2)
543     assert (p1.sente == true  && p2.sente == false) ||
544            (p1.sente == false && p2.sente == true)
545   end
546 end
547