OSDN Git Service

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