OSDN Git Service

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