OSDN Git Service

[mk_game_results] Flush after each output line.
[shogi-server/shogi-server.git] / test / TC_board.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2
3 require 'test/unit'
4 require 'shogi_server'
5 require 'shogi_server/board'
6 require 'shogi_server/piece'
7
8 class Test_kachi < Test::Unit::TestCase
9   def test_kachi_good
10     b = ShogiServer::Board.new
11     b.set_from_str(<<EOM)
12 P1+HI+HI+KA+KA+OU *  *  *  * 
13 P2+FU+FU+FU+FU+FU+FU *  *  * 
14 P+00FU00FU
15 EOM
16     assert_equal(true, b.good_kachi?(true))
17
18     b = ShogiServer::Board.new
19     b.set_from_str(<<EOM)
20 P8-HI-HI-KA-KA-OU *  *  *  * 
21 P9-FU-FU-FU-FU-FU-FU *  *  * 
22 P-00FU
23 EOM
24     assert_equal(true, b.good_kachi?(false))
25   end
26
27   def test_kachi_good
28     b = ShogiServer::Board.new
29     b.set_from_str(<<EOM)
30 P1+HI+HI+KA+KA+OU *  *  *  * 
31 P2+FU+FU+FU+FU+FU+FU *  *  * 
32 P+00FU00FU
33 EOM
34     assert_equal(true, b.good_kachi?(true))
35
36     b = ShogiServer::Board.new
37     b.set_from_str(<<EOM)
38 P8-HI-HI-KA-KA-OU *  *  *  * 
39 P9-FU-FU-FU-FU-FU-FU *  *  * 
40 P-00FU
41 EOM
42     assert_equal(true, b.good_kachi?(false))
43   end
44
45   def test_kachi_bad
46     b = ShogiServer::Board.new
47     b.set_from_str(<<EOM)
48 P1+HI+HI+KA+KA+OU *  *  *  * 
49 P2+FU+FU+FU+FU+FU+FU *  *  * 
50 P+00FU
51 EOM
52     assert_equal(false, b.good_kachi?(true)) # point is not enough
53
54     b = ShogiServer::Board.new
55     b.set_from_str(<<EOM)
56 P8-HI-HI-KA-KA-OU *  *  *  * 
57 P9-FU-FU-FU-FU-FU-FU *  *  * 
58 EOM
59     assert_equal(false, b.good_kachi?(false)) # point is not enough
60
61     b = ShogiServer::Board.new
62     b.set_from_str(<<EOM)
63 P1+HI+HI+KA+KA+OU *  *  *  * 
64 P2+FU+FU+FU+FU+FU *  *  *  *
65 P+00FU00FU00FU
66 EOM
67     assert_equal(false, b.good_kachi?(true)) # number on board is not enough
68
69     b = ShogiServer::Board.new
70     b.set_from_str(<<EOM)
71 P8-HI-HI-KA-KA-OU *  *  *  * 
72 P9-FU-FU-FU-FU-FU *  *  *  * 
73 P-00FU00FU
74 EOM
75     assert_equal(false, b.good_kachi?(false)) # number on board is not enough
76
77     b = ShogiServer::Board.new
78     b.set_from_str(<<EOM)
79 P1+HI+HI+KA+KA+OU *  *  * -HI
80 P2+FU+FU+FU+FU+FU+FU *  *  * 
81 P+00FU00FU
82 EOM
83     assert_equal(false, b.good_kachi?(true)) # checkmate
84
85     b = ShogiServer::Board.new
86     b.set_from_str(<<EOM)
87 P8-HI-HI-KA-KA-OU *  *  * +HI
88 P9-FU-FU-FU-FU-FU-FU *  *  * 
89 P-00FU
90 EOM
91     assert_equal(false, b.good_kachi?(false)) # checkmate
92
93     b = ShogiServer::Board.new
94     b.set_from_str(<<EOM)
95 P1+TO+TO * +TO+TO+OU * +TO * 
96 P2 *  *  *  *  *  *  *  * +KI
97 P3 *  *  * +TO+NG+TO+TO+TO+NY
98 P4 *  *  *  *  *  *  *  *  * 
99 P5 *  *  *  *  *  *  *  * +UM
100 P6 *  *  * -KI-NG-RY *  *  * 
101 P7-TO * -TO-NG * -TO-TO-TO * 
102 P8-RY *  * -NK-TO-OU-TO-TO * 
103 P9 * -TO *  *  *  *  *  *  * 
104 P+00KI00KI00KE
105 P-00KA00GI00KE00KE00KY00KY00KY
106 -
107 EOM
108     assert_equal(true, b.good_kachi?(false))
109   end
110 end
111
112 class Test_gps < Test::Unit::TestCase
113   def test_gote_promote
114     b = ShogiServer::Board.new
115     b.set_from_str(<<EOM)
116 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
117 P2 * -HI *  *  *  *  *  *  * 
118 P3-FU * -FU-FU-FU-FU-KA-FU-FU
119 P4 *  *  *  *  *  *  *  *  * 
120 P5 * -FU+FU *  *  *  *  *  * 
121 P6 *  *  *  *  *  *  *  *  * 
122 P7+FU+FU+KE+FU+FU+FU+FU+FU+FU
123 P8 *  *  *  *  *  *  * +HI * 
124 P9+KY * +GI+KI+OU+KI+GI+KE+KY
125 P+00FU
126 P-00KA
127 EOM
128     assert_equal(:normal, b.handle_one_move("-3377UM"))
129   end
130
131   def test_capture_promoted_and_put
132     b = ShogiServer::Board.new
133     b.set_from_str(<<EOM)
134 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
135 P2 * -HI *  *  *  *  * -KA * 
136 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
137 P4 *  *  *  *  *  *  *  *  * 
138 P5 *  *  *  *  *  *  *  *  * 
139 P6 *  *  *  *  *  *  *  *  * 
140 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
141 P8 * +KA *  *  *  *  * +HI * 
142 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
143 EOM
144
145     assert_equal(:normal, b.handle_one_move("+7776FU"))
146     assert_equal(:normal, b.handle_one_move("-3334FU"))
147     assert_equal(:normal, b.handle_one_move("+2726FU"))
148     assert_equal(:normal, b.handle_one_move("-4344FU"))
149     assert_equal(:normal, b.handle_one_move("+3948GI"))
150     assert_equal(:normal, b.handle_one_move("-8242HI"))
151     assert_equal(:normal, b.handle_one_move("+5756FU"))
152     assert_equal(:normal, b.handle_one_move("-3132GI"))
153     assert_equal(:normal, b.handle_one_move("+5968OU"))
154     assert_equal(:normal, b.handle_one_move("-5162OU"))
155     assert_equal(:normal, b.handle_one_move("+6878OU"))
156     assert_equal(:normal, b.handle_one_move("-6272OU"))
157     assert_equal(:normal, b.handle_one_move("+4958KI"))
158     assert_equal(:normal, b.handle_one_move("-7282OU"))
159     assert_equal(:normal, b.handle_one_move("+9796FU"))
160     assert_equal(:normal, b.handle_one_move("-9394FU"))
161     assert_equal(:normal, b.handle_one_move("+2625FU"))
162     assert_equal(:normal, b.handle_one_move("-2233KA"))
163     assert_equal(:normal, b.handle_one_move("+3736FU"))
164     assert_equal(:normal, b.handle_one_move("-7172GI"))
165     assert_equal(:normal, b.handle_one_move("+7968GI"))
166     assert_equal(:normal, b.handle_one_move("-4152KI"))
167     assert_equal(:normal, b.handle_one_move("+6857GI"))
168     assert_equal(:normal, b.handle_one_move("-3243GI"))
169     assert_equal(:normal, b.handle_one_move("+6968KI"))
170     assert_equal(:normal, b.handle_one_move("-5354FU"))
171     assert_equal(:normal, b.handle_one_move("+1716FU"))
172     assert_equal(:normal, b.handle_one_move("-1314FU"))
173     assert_equal(:normal, b.handle_one_move("+4746FU"))
174     assert_equal(:normal, b.handle_one_move("-6364FU"))
175     assert_equal(:normal, b.handle_one_move("+4645FU"))
176     assert_equal(:normal, b.handle_one_move("-5263KI"))
177     assert_equal(:normal, b.handle_one_move("+2937KE"))
178     assert_equal(:normal, b.handle_one_move("-7374FU"))
179     assert_equal(:normal, b.handle_one_move("+2524FU"))
180     assert_equal(:normal, b.handle_one_move("-2324FU"))
181     assert_equal(:normal, b.handle_one_move("+4544FU"))
182     assert_equal(:normal, b.handle_one_move("-4344GI"))
183     assert_equal(:normal, b.handle_one_move("+0045FU"))
184     assert_equal(:normal, b.handle_one_move("-4445GI"))
185     assert_equal(:normal, b.handle_one_move("+8833UM"))
186     assert_equal(:normal, b.handle_one_move("-2133KE"))
187     assert_equal(:normal, b.handle_one_move("+0088KA"))
188     assert_equal(:normal, b.handle_one_move("-5455FU"))
189     assert_equal(:normal, b.handle_one_move("+8855KA"))
190     assert_equal(:normal, b.handle_one_move("-4243HI"))
191     assert_equal(:normal, b.handle_one_move("+2824HI"))
192     assert_equal(:normal, b.handle_one_move("-4554GI"))
193     assert_equal(:normal, b.handle_one_move("+0044FU"))
194     assert_equal(:normal, b.handle_one_move("-4353HI"))
195     assert_equal(:normal, b.handle_one_move("+2422RY"))
196     assert_equal(:normal, b.handle_one_move("-5455GI"))
197     assert_equal(:normal, b.handle_one_move("+5655FU"))
198     assert_equal(:normal, b.handle_one_move("-0056FU"))
199     assert_equal(:normal, b.handle_one_move("+5756GI"))
200     assert_equal(:normal, b.handle_one_move("-0057FU"))
201     assert_equal(:normal, b.handle_one_move("+4857GI"))
202     assert_equal(:normal, b.handle_one_move("-9495FU"))
203     assert_equal(:normal, b.handle_one_move("+9695FU"))
204     assert_equal(:normal, b.handle_one_move("-0096FU"))
205     assert_equal(:normal, b.handle_one_move("+9996KY"))
206     assert_equal(:normal, b.handle_one_move("-0085KA"))
207   end
208 end
209
210
211 class TestMoveBack < Test::Unit::TestCase
212   def validate_before_after(board, move)
213     orig = board.to_s
214     orig_teban = board.teban
215     orig_move_count = board.move_count
216
217     assert_equal true, board.move_to(move)
218     assert_equal !orig_teban, board.teban
219     assert_equal (orig_move_count+1), board.move_count
220
221     assert_equal true, board.move_back(move)
222     assert_equal orig_teban, board.teban
223     assert_equal orig_move_count, board.move_count
224     assert_equal orig, board.to_s
225   end
226
227   def test_normal
228     b = ShogiServer::Board.new
229     b.set_from_str(<<EOM)
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 EOM
241     m = ShogiServer::Move.new(2,7,2,6,"FU",true)
242     validate_before_after(b,m)
243     m = ShogiServer::Move.new(3,3,3,4,"FU",false)
244     validate_before_after(b,m)
245     m = ShogiServer::Move.new(7,7,7,6,"FU",true)
246     validate_before_after(b,m)
247     m = ShogiServer::Move.new(8,3,8,4,"FU",false)
248     validate_before_after(b,m)
249   end
250
251   def test_promote
252     b = ShogiServer::Board.new
253     b.set_from_str(<<EOM)
254 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
255 P2 * -HI *  *  *  *  * -KA * 
256 P3-FU-FU-FU-FU-FU-FU-FU * -FU
257 P4 *  *  *  *  *  *  * +FU * 
258 P5 *  *  *  *  *  *  *  *  * 
259 P6 *  *  *  *  *  *  *  *  * 
260 P7+FU+FU+FU+FU+FU+FU+FU * +FU
261 P8 * +KA *  *  *  *  * +HI * 
262 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
263 +
264 P+00FU
265 EOM
266     m = ShogiServer::Move.new(2,4,2,3,"TO",true)
267     validate_before_after(b,m)
268   end
269
270   def test_unpromote
271     b = ShogiServer::Board.new
272     b.set_from_str(<<EOM)
273 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
274 P2 * -HI *  *  *  *  * -KA * 
275 P3-FU-FU-FU-FU-FU-FU-FU * -FU
276 P4 *  *  *  *  *  *  * +FU * 
277 P5 *  *  *  *  *  *  *  *  * 
278 P6 *  *  *  *  *  *  *  *  * 
279 P7+FU+FU+FU+FU+FU+FU+FU * +FU
280 P8 * +KA *  *  *  *  * +HI * 
281 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
282 +
283 P+00FU
284 EOM
285     m = ShogiServer::Move.new(2,4,2,3,"FU",true)
286     validate_before_after(b,m)
287   end
288
289   def test_promoted
290     b = ShogiServer::Board.new
291     b.set_from_str(<<EOM)
292 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
293 P2 * -HI *  *  *  *  * -KA * 
294 P3-FU-FU-FU-FU-FU-FU-FU+TO-FU
295 P4 *  *  *  *  *  *  *  *  * 
296 P5 *  *  *  *  *  *  *  *  * 
297 P6 *  *  *  *  *  *  *  *  * 
298 P7+FU+FU+FU+FU+FU+FU+FU * +FU
299 P8 * +KA *  *  *  *  * +HI * 
300 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
301 +
302 P+00FU
303 EOM
304     m = ShogiServer::Move.new(2,3,1,2,"TO",true)
305     assert !m.promotion
306     validate_before_after(b,m)
307     assert !m.promotion
308   end
309
310   def test_capture
311     b = ShogiServer::Board.new
312     b.set_from_str(<<EOM)
313 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
314 P2 * -HI *  *  *  *  * -KA * 
315 P3-FU-FU-FU-FU-FU-FU-FU * -FU
316 P4 *  *  *  *  *  *  * -FU * 
317 P5 *  *  *  *  *  *  * +FU * 
318 P6 *  *  *  *  *  *  *  *  * 
319 P7+FU+FU+FU+FU+FU+FU+FU * +FU
320 P8 * +KA *  *  *  *  * +HI * 
321 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
322 +
323 EOM
324     m = ShogiServer::Move.new(2,5,2,4,"FU",true)
325     validate_before_after(b,m)
326   end
327  
328   def test_capture_white
329     b = ShogiServer::Board.new
330     b.set_from_str(<<EOM)
331 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
332 P2 * -HI *  *  *  *  * -KA * 
333 P3-FU-FU-FU-FU-FU-FU-FU * -FU
334 P4 *  *  *  *  *  *  * -FU * 
335 P5 *  *  *  *  *  *  * +FU * 
336 P6 *  *  *  *  *  *  *  *  * 
337 P7+FU+FU+FU+FU+FU+FU+FU * +FU
338 P8 * +KA *  *  *  *  * +HI * 
339 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
340 -
341 EOM
342     m = ShogiServer::Move.new(2,4,2,5,"FU",false)
343     validate_before_after(b,m)
344   end
345  
346   def test_capture_promote
347     b = ShogiServer::Board.new
348     b.set_from_str(<<EOM)
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 *  *  *  *  *  *  * +FU * 
353 P5 *  *  *  *  *  *  *  *  * 
354 P6 *  *  *  *  *  *  *  *  * 
355 P7+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 EOM
360     m = ShogiServer::Move.new(2,4,2,3,"TO",true)
361     validate_before_after(b,m)
362   end
363
364   def test_capture_promote_white
365     b = ShogiServer::Board.new
366     b.set_from_str(<<EOM)
367 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
368 P2 * -HI *  *  *  *  * -KA * 
369 P3-FU-FU-FU-FU-FU-FU-FU * -FU
370 P4 *  *  *  *  *  *  *  *  * 
371 P5 *  *  *  *  *  *  *  *  * 
372 P6 *  *  *  *  *  *  * -FU * 
373 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
374 P8 * +KA *  *  *  *  * +HI * 
375 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
376 -
377 EOM
378     m = ShogiServer::Move.new(2,6,2,7,"TO",false)
379     validate_before_after(b,m)
380   end
381
382   def test_capture_unpromote
383     b = ShogiServer::Board.new
384     b.set_from_str(<<EOM)
385 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
386 P2 * -HI *  *  *  *  * -KA * 
387 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
388 P4 *  *  *  *  *  *  * +FU * 
389 P5 *  *  *  *  *  *  *  *  * 
390 P6 *  *  *  *  *  *  *  *  * 
391 P7+FU+FU+FU+FU+FU+FU+FU * +FU
392 P8 * +KA *  *  *  *  * +HI * 
393 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
394 +
395 EOM
396     m = ShogiServer::Move.new(2,4,2,3,"FU",true)
397     validate_before_after(b,m)
398   end
399
400   def test_capture_unpromote_white
401     b = ShogiServer::Board.new
402     b.set_from_str(<<EOM)
403 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
404 P2 * -HI *  *  *  *  * -KA * 
405 P3-FU-FU-FU-FU-FU-FU-FU * -FU
406 P4 *  *  *  *  *  *  *  *  * 
407 P5 *  *  *  *  *  *  *  *  * 
408 P6 *  *  *  *  *  *  * -FU * 
409 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
410 P8 * +KA *  *  *  *  * +HI * 
411 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
412 -
413 EOM
414     m = ShogiServer::Move.new(2,6,2,7,"FU",false)
415     validate_before_after(b,m)
416   end
417
418   def test_drop
419     b = ShogiServer::Board.new
420     b.set_from_str(<<EOM)
421 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
422 P2 * -HI *  *  *  *  * -KA * 
423 P3-FU-FU-FU-FU-FU-FU-FU * -FU
424 P4 *  *  *  *  *  *  *  *  * 
425 P5 *  *  *  *  *  *  *  *  * 
426 P6 *  *  *  *  *  *  *  *  * 
427 P7+FU+FU+FU+FU+FU+FU+FU * +FU
428 P8 * +KA *  *  *  *  * +HI * 
429 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
430 +
431 P+00FU
432 P-00FU
433 EOM
434     m = ShogiServer::Move.new(0,0,2,3,"FU",true)
435     validate_before_after(b,m)
436   end
437
438   def test_drop_white
439     b = ShogiServer::Board.new
440     b.set_from_str(<<EOM)
441 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
442 P2 * -HI *  *  *  *  * -KA * 
443 P3-FU-FU-FU-FU-FU-FU-FU * -FU
444 P4 *  *  *  *  *  *  *  *  * 
445 P5 *  *  *  *  *  *  *  *  * 
446 P6 *  *  *  *  *  *  *  *  * 
447 P7+FU+FU+FU+FU+FU+FU+FU * +FU
448 P8 * +KA *  *  *  *  * +HI * 
449 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
450 -
451 P+00FU
452 P-00FU
453 EOM
454     m = ShogiServer::Move.new(0,0,2,3,"FU",false)
455     validate_before_after(b,m)
456   end
457 end # class TestMoveBack
458
459
460 class Test_promote < Test::Unit::TestCase
461   def test_fu
462     b = ShogiServer::Board.new
463     b.set_from_str(<<EOM)
464 P1-OU * +OU *
465 P2 *  *  *  *
466 P3+FU *  * +HI
467 P4 * +FU *  *
468 P5 *  * +FU *
469 EOM
470     assert_equal(:normal, b.handle_one_move("+9392TO"))
471     assert_equal(:normal, b.handle_one_move("+8483TO"))
472     assert_equal(:illegal, b.handle_one_move("+7574TO"))
473     assert_equal(:normal, b.handle_one_move("+6364RY"))
474   end
475 end
476
477 class Test_move < Test::Unit::TestCase
478   def test_fu
479     b = ShogiServer::Board.new
480     b.set_from_str(<<EOM)
481 P1-OU * +OU
482 P2 * +FU *
483 EOM
484     assert_equal(:illegal, b.handle_one_move("+8281FU"))
485   end
486   def test_hi
487     b = ShogiServer::Board.new
488     b.set_from_str(<<EOM)
489 P1-OU * +OU
490 P2 * +HI *
491 EOM
492     assert_equal(:normal, b.handle_one_move("+8212HI"))
493     assert_equal(:illegal, b.handle_one_move("+1223HI"))
494   end
495   def test_ry
496     b = ShogiServer::Board.new
497     b.set_from_str(<<EOM)
498 P1-OU * +OU
499 P2 * +RY *
500 EOM
501     assert_equal(:normal, b.handle_one_move("+8212RY"))
502     assert_equal(:normal, b.handle_one_move("+1223RY"))
503   end
504 end
505
506 class Test_put < Test::Unit::TestCase
507   def test_fu
508     b = ShogiServer::Board.new
509     b.set_from_str(<<EOM)
510 P1-OU * +OU
511 P+00FU00FU
512 EOM
513     assert_equal(:illegal, b.handle_one_move("+0011FU"))
514     assert_equal(:normal, b.handle_one_move("+0022FU"))
515   end
516   def test_ky
517      b = ShogiServer::Board.new
518      b.set_from_str(<<EOM)
519 P1-OU * +OU
520 P+00KY00KY
521 EOM
522     assert_equal(:illegal, b.handle_one_move("+0011KY"))
523     assert_equal(:normal, b.handle_one_move("+0022KY"))
524   end
525
526   def test_ke
527     b = ShogiServer::Board.new
528     b.set_from_str(<<EOM)
529 P1-OU * +OU
530 P+00KE00KE00KE
531 EOM
532     assert_equal(:illegal, b.handle_one_move("+0011KE"))
533     assert_equal(:illegal, b.handle_one_move("+0022KE"))
534     assert_equal(:normal, b.handle_one_move("+0033KE"))
535   end
536 end
537
538
539 class Test_2fu < Test::Unit::TestCase
540   def test_2fu
541     b = ShogiServer::Board.new
542     b.set_from_str(<<EOM)
543 P1-OU * +OU
544 P+00FU00FU
545 EOM
546     assert_equal(:normal, b.handle_one_move("+0022FU"))
547     assert_equal(:illegal, b.handle_one_move("+0023FU"))
548
549     b = ShogiServer::Board.new
550     b.set_from_str(<<EOM)
551 P1-OU * +OU
552 P+00FU00FU
553 EOM
554     assert_equal(:normal, b.handle_one_move("+0022FU"))
555     assert_equal(:normal, b.handle_one_move("+0032FU"))
556   end
557 end
558
559 class Test_sennichite < Test::Unit::TestCase
560   def test_oute_sennichite0
561     b = ShogiServer::Board.new
562     b.set_from_str(<<EOM)
563 P1-OU *  * +OU
564 P2 *  * +HI *
565 EOM
566 ##    b.history[b.to_s] = 1
567     assert_equal(:normal, b.handle_one_move("+7271HI")) #1
568     assert_equal(:normal, b.handle_one_move("-9192OU"))
569     assert_equal(:normal, b.handle_one_move("+7172HI"))
570     assert_equal(:normal, b.handle_one_move("-9291OU"))
571
572     assert_equal(:normal, b.handle_one_move("+7271HI")) # 2
573     assert_equal(:normal, b.handle_one_move("-9192OU"))
574     assert_equal(:normal, b.handle_one_move("+7172HI"))
575     assert_equal(:normal, b.handle_one_move("-9291OU"))
576
577     assert_equal(:normal, b.handle_one_move("+7271HI")) # 3
578     assert_equal(:normal, b.handle_one_move("-9192OU"))
579     assert_equal(:normal, b.handle_one_move("+7172HI"))
580     assert_equal(:normal, b.handle_one_move("-9291OU"))
581
582     orig = b.deep_copy
583     assert_equal(:oute_sennichite_sente_lose, b.handle_one_move("+7271HI")) # 4
584     assert_equal orig, b
585   end
586
587   def test_oute_sennichite1 #330
588     b = ShogiServer::Board.new
589     b.set_from_str(<<EOM)
590 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
591 P2 * -HI *  *  *  *  * -KA * 
592 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
593 P4 *  *  *  *  *  *  *  *  * 
594 P5 *  *  *  *  *  *  *  *  * 
595 P6 *  *  *  *  *  *  *  *  * 
596 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
597 P8 * +KA *  *  *  *  * +HI * 
598 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
599 EOM
600 b.history[b.to_s] = 1
601
602     assert_equal(:normal, b.handle_one_move("+2726FU"))
603     assert_equal(:normal, b.handle_one_move("-8384FU"))
604     assert_equal(:normal, b.handle_one_move("+2625FU"))
605     assert_equal(:normal, b.handle_one_move("-8485FU"))
606     assert_equal(:normal, b.handle_one_move("+6978KI"))
607     assert_equal(:normal, b.handle_one_move("-4132KI"))
608     assert_equal(:normal, b.handle_one_move("+2524FU"))
609     assert_equal(:normal, b.handle_one_move("-2324FU"))
610     assert_equal(:normal, b.handle_one_move("+2824HI"))
611     assert_equal(:normal, b.handle_one_move("-0023FU"))
612     assert_equal(:normal, b.handle_one_move("+2484HI"))
613     assert_equal(:normal, b.handle_one_move("-8284HI"))
614     assert_equal(:normal, b.handle_one_move("+4938KI"))
615     assert_equal(:normal, b.handle_one_move("-9394FU"))
616     assert_equal(:normal, b.handle_one_move("+5969OU"))
617     assert_equal(:normal, b.handle_one_move("-0049HI"))
618
619     assert_equal(:normal, b.handle_one_move("+6968OU"))
620     assert_equal(:normal, b.handle_one_move("-4948RY"))
621     assert_equal(:normal, b.handle_one_move("+6869OU"))
622     assert_equal(:normal, b.handle_one_move("-4849RY"))
623
624     assert_equal(:normal, b.handle_one_move("+6968OU"))
625     assert_equal(:normal, b.handle_one_move("-4948RY"))
626     assert_equal(:normal, b.handle_one_move("+6869OU"))
627     assert_equal(:normal, b.handle_one_move("-4849RY"))
628
629     assert_equal(:normal, b.handle_one_move("+6968OU"))
630     assert_equal(:normal, b.handle_one_move("-4948RY"))
631     assert_equal(:normal, b.handle_one_move("+6869OU"))
632     assert_equal(:normal, b.handle_one_move("-4849RY"))
633
634     assert_equal(:normal, b.handle_one_move("+6968OU")) # added
635     orig = b.deep_copy
636     assert_equal(:oute_sennichite_gote_lose, b.handle_one_move("-4948RY"))
637     assert_equal orig, b
638   end
639
640   def test_not_oute_sennichite
641     b = ShogiServer::Board.new
642     b.set_from_str(<<EOM)
643 P1-OU *  * +OU
644 P2 *  * +HI *
645 EOM
646 ##    b.history[b.to_s] = 1
647     assert_equal(:normal, b.handle_one_move("+7271HI")) #1
648     assert_equal(:normal, b.handle_one_move("-9192OU"))
649     assert_equal(:normal, b.handle_one_move("+7172HI"))
650     assert_equal(:normal, b.handle_one_move("-9291OU"))
651
652     assert_equal(:normal, b.handle_one_move("+7271HI")) # 2
653     assert_equal(:normal, b.handle_one_move("-9192OU"))
654     assert_equal(:normal, b.handle_one_move("+7174HI")) # stop oute here
655     assert_equal(:normal, b.handle_one_move("-9291OU"))
656
657     assert_equal(:normal, b.handle_one_move("+7471HI")) # 3
658     assert_equal(:normal, b.handle_one_move("-9192OU"))
659     assert_equal(:normal, b.handle_one_move("+7172HI"))
660     assert_equal(:normal, b.handle_one_move("-9291OU"))
661
662     orig = b.deep_copy
663     assert_equal(:sennichite, b.handle_one_move("+7271HI")) # 4
664     assert_equal orig, b
665   end
666
667   def test_sennichite0
668     b = ShogiServer::Board.new
669     b.set_from_str(<<EOM)
670 P1-OU * +OU
671 EOM
672     b.history[b.to_s] = 1
673     assert_equal(:normal, b.handle_one_move("+7172OU"))
674     assert_equal(:normal, b.handle_one_move("-9192OU"))
675     assert_equal(:normal, b.handle_one_move("+7271OU"))
676     assert_equal(:normal, b.handle_one_move("-9291OU")) # 2
677
678     assert_equal(:normal, b.handle_one_move("+7172OU"))
679     assert_equal(:normal, b.handle_one_move("-9192OU"))
680     assert_equal(:normal, b.handle_one_move("+7271OU"))
681     assert_equal(:normal, b.handle_one_move("-9291OU")) # 3
682
683     assert_equal(:normal, b.handle_one_move("+7172OU"))
684     assert_equal(:normal, b.handle_one_move("-9192OU"))
685     assert_equal(:normal, b.handle_one_move("+7271OU"))
686     orig = b.deep_copy
687     assert_equal(:sennichite, b.handle_one_move("-9291OU")) # 4
688     assert_equal orig, b
689   end
690
691   def test_sennichite1          # 329
692     b = ShogiServer::Board.new
693     b.set_from_str(<<EOM)
694 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
695 P2 * -HI *  *  *  *  * -KA * 
696 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
697 P4 *  *  *  *  *  *  *  *  * 
698 P5 *  *  *  *  *  *  *  *  * 
699 P6 *  *  *  *  *  *  *  *  * 
700 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
701 P8 * +KA *  *  *  *  * +HI * 
702 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
703 EOM
704     b.history[b.to_s] = 1
705
706     assert_equal(:normal, b.handle_one_move("+2858HI"))
707     assert_equal(:normal, b.handle_one_move("-8252HI"))
708     assert_equal(:normal, b.handle_one_move("+5828HI"))
709     assert_equal(:normal, b.handle_one_move("-5282HI"))
710     assert_equal(:normal, b.handle_one_move("+2858HI"))
711     assert_equal(:normal, b.handle_one_move("-8252HI"))
712     assert_equal(:normal, b.handle_one_move("+5828HI"))
713     assert_equal(:normal, b.handle_one_move("-5282HI"))
714     assert_equal(:normal, b.handle_one_move("+2858HI"))
715     assert_equal(:normal, b.handle_one_move("-8252HI"))
716     assert_equal(:normal, b.handle_one_move("+5828HI"))
717     orig = b.deep_copy
718     assert_equal(:sennichite, b.handle_one_move("-5282HI"))
719     assert_equal orig, b
720   end
721 end
722
723 class Test_checkmate < Test::Unit::TestCase
724   def test_ki
725     b = ShogiServer::Board.new
726     b.set_from_str(<<EOM)
727 P1-OU * +OU
728 P2 * +KI
729 EOM
730     assert_equal(true, b.checkmated?(false)) # gote is loosing
731     assert_equal(false, b.checkmated?(true))
732   end
733
734   def test_hi
735     b = ShogiServer::Board.new
736     b.set_from_str(<<EOM)
737 P1-OU * +HI+OU
738 EOM
739     assert_equal(true, b.checkmated?(false)) # gote is loosing
740     assert_equal(false, b.checkmated?(true))
741
742     b = ShogiServer::Board.new
743     b.set_from_str(<<EOM)
744 P1-OU * 
745 P2 * +HI+OU
746 EOM
747     assert_equal(false, b.checkmated?(false)) # hisha can't capture
748     assert_equal(false, b.checkmated?(true))
749
750     b = ShogiServer::Board.new
751     b.set_from_str(<<EOM)
752 P1-OU * 
753 P2 * +RY+OU
754 EOM
755     assert_equal(true, b.checkmated?(false)) # ryu can capture
756     assert_equal(false, b.checkmated?(true))
757   end
758
759   def test_KE
760     b = ShogiServer::Board.new
761     b.set_from_str(<<EOM)
762 P1-OU * +OU
763 P2 *  *  *
764 P3 * +KE *
765 EOM
766     assert_equal(true, b.checkmated?(false))
767     assert_equal(false, b.checkmated?(true))
768   end
769 end
770
771 class Test_uchifuzume < Test::Unit::TestCase
772   def test_uchifuzume1          # 331
773     b = ShogiServer::Board.new
774     b.set_from_str(<<EOM)
775 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
776 P2 * -HI *  *  *  *  * -KA * 
777 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
778 P4 *  *  *  *  *  *  *  *  * 
779 P5 *  *  *  *  *  *  *  *  * 
780 P6 *  *  *  *  *  *  *  *  * 
781 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
782 P8 * +KA *  *  *  *  * +HI * 
783 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
784 EOM
785
786     assert_equal(:normal, b.handle_one_move("+2726FU"))
787     assert_equal(:normal, b.handle_one_move("-8384FU"))
788     assert_equal(:normal, b.handle_one_move("+2625FU"))
789     assert_equal(:normal, b.handle_one_move("-8485FU"))
790     assert_equal(:normal, b.handle_one_move("+2524FU"))
791     assert_equal(:normal, b.handle_one_move("-2324FU"))
792     assert_equal(:normal, b.handle_one_move("+2824HI"))
793     assert_equal(:normal, b.handle_one_move("-8586FU"))
794     assert_equal(:normal, b.handle_one_move("+8786FU"))
795     assert_equal(:normal, b.handle_one_move("-0087FU"))
796     assert_equal(:normal, b.handle_one_move("+0023FU"))
797     assert_equal(:normal, b.handle_one_move("-8788TO"))
798     assert_equal(:normal, b.handle_one_move("+2322TO"))
799     assert_equal(:normal, b.handle_one_move("-8879TO"))
800     assert_equal(:normal, b.handle_one_move("+2231TO"))
801     assert_equal(:normal, b.handle_one_move("-7969TO"))
802     assert_equal(:normal, b.handle_one_move("+5969OU"))
803     assert_equal(:normal, b.handle_one_move("-8286HI"))
804     assert_equal(:normal, b.handle_one_move("+3141TO"))
805     assert_equal(:normal, b.handle_one_move("-5141OU"))
806     assert_equal(:normal, b.handle_one_move("+2484HI"))
807     assert_equal(:normal, b.handle_one_move("-8684HI"))
808     assert_equal(:normal, b.handle_one_move("+6978OU"))
809     assert_equal(:normal, b.handle_one_move("-8424HI"))
810     assert_equal(:normal, b.handle_one_move("+7776FU"))
811     assert_equal(:normal, b.handle_one_move("-7374FU"))
812     assert_equal(:normal, b.handle_one_move("+7675FU"))
813     assert_equal(:normal, b.handle_one_move("-7475FU"))
814     assert_equal(:normal, b.handle_one_move("+0079KI"))
815     assert_equal(:normal, b.handle_one_move("-7576FU"))
816     assert_equal(:normal, b.handle_one_move("+7888OU"))
817     assert_equal(:normal, b.handle_one_move("-7677TO"))
818     assert_equal(:normal, b.handle_one_move("+8877OU"))
819     assert_equal(:normal, b.handle_one_move("-2474HI"))
820     assert_equal(:normal, b.handle_one_move("+7788OU"))
821     assert_equal(:normal, b.handle_one_move("-0086KI"))
822     assert_equal(:normal, b.handle_one_move("+9998KY"))
823     assert_equal(:normal, b.handle_one_move("-7424HI"))
824     assert_equal(:normal, b.handle_one_move("+0099GI"))
825     assert_equal(:normal, b.handle_one_move("-0028HI"))
826     assert_equal(:normal, b.handle_one_move("+0078FU"))
827     assert_equal(:uchifuzume, b.handle_one_move("-0087FU"))
828   end
829
830   def test_uchifuzume2
831     # http://wdoor.c.u-tokyo.ac.jp/shogi/tools/view/index.cgi?go_last=on&csa=http%3A%2F%2Fwdoor.c.u-tokyo.ac.jp%2Fshogi%2Flogs%2FLATEST%2Fwdoor%2Bfloodgate-900-0%2Busapyon-on-note%2BKShogi900%2B20080217020012.csa
832     b = ShogiServer::Board.new
833     b.set_from_str(<<EOM)
834 P1-KY-KE * -KI * -OU * -KE-KY
835 P2 *  * +TO *  *  *  *  *  *
836 P3 *  *  *  * -KI-KI * -FU *
837 P4 *  * -FU * -FU-FU-FU *  *
838 P5 * -RY+GI+OU *  *  * +FU+FU
839 P6-FU * +FU+KI+GI+FU+FU * +KY
840 P7 *  *  * -RY *  *  *  *  *
841 P8 *  *  *  *  *  *  *  *  *
842 P9+KY+KE *  *  *  *  *  *  *
843 P+00FU00FU00FU00FU00FU00GI00GI00KA00KE
844 P-00FU00KA
845 -
846 EOM
847     assert_equal(:uchifuzume, b.handle_one_move("-0064FU"))
848   end
849
850   def test_uchifuzume3
851     # http://wdoor.c.u-tokyo.ac.jp/shogi/tools/view/index.cgi?go_last=on&csa=http%3A%2F%2Fwdoor.c.u-tokyo.ac.jp%2Fshogi%2Flogs%2FLATEST%2Fwdoor%2Bfloodgate-900-0%2Busapyon-on-note%2Bgps_normal%2B20080215133008.csa
852     b = ShogiServer::Board.new
853     b.set_from_str(<<EOM)
854 P1 * -GI * -KI-OU * -GI * -KY
855 P2 *  *  *  * -FU *  *  *  *
856 P3+OU * -FU-FU * -FU-KI-FU *
857 P4+KI+KE-RY * -GI *  *  * -FU
858 P5 *  *  *  *  *  *  *  *  *
859 P6+FU *  *  * +KY *  *  * +FU
860 P7 * +FU *  * +FU *  *  *  *
861 P8 * +GI *  *  *  *  *  *  *
862 P9+KY+KE *  *  *  *  *  * -UM
863 P+00KA00KI00KE00KY00FU00FU00FU
864 P-00HI00KE00FU00FU00FU00FU00FU
865 -
866 EOM
867     assert_equal(:normal, b.handle_one_move("-0092FU"))
868   end
869
870   def test_ou
871     b = ShogiServer::Board.new
872     b.set_from_str(<<EOM)
873 P1-OU * +OU
874 P2+FU *  *
875 P3 * +HI *
876 EOM
877     assert_equal(false, b.uchifuzume?(true))
878
879     b = ShogiServer::Board.new
880     b.set_from_str(<<EOM)
881 P1-OU * +OU
882 P2+FU *  *
883 P3 * +RY *
884 EOM
885     assert_equal(true, b.uchifuzume?(true))
886
887     b = ShogiServer::Board.new
888     b.set_from_str(<<EOM)
889 P2-OU * +OU
890 P3+FU *  *
891 P4 * +RY *
892 EOM
893     assert_equal(false, b.uchifuzume?(true)) # ou can move backward
894
895     b = ShogiServer::Board.new
896     b.set_from_str(<<EOM)
897 P2-OU * +OU
898 P3+FU * +KA
899 P4 * +RY *
900 EOM
901     assert_equal(true, b.uchifuzume?(true)) # ou can move backward and kaku can capture it
902  end                   
903
904
905   def test_friend
906     b = ShogiServer::Board.new
907     b.set_from_str(<<EOM)
908 P1-OU * +OU
909 P2+FU * -HI
910 P3 * +RY *
911 EOM
912     assert_equal(false, b.uchifuzume?(true))
913
914     b = ShogiServer::Board.new
915     b.set_from_str(<<EOM)
916 P1-OU * +OU
917 P2+FU * +FU-HI
918 P3 * +RY *
919 EOM
920     assert_equal(true, b.uchifuzume?(true)) # hisha blocked by fu
921
922     b = ShogiServer::Board.new
923     b.set_from_str(<<EOM)
924 P1-OU * +OU
925 P2+FU *  *
926 P3-GI+RY *
927 EOM
928     assert_equal(true, b.uchifuzume?(true))
929
930     b = ShogiServer::Board.new
931     b.set_from_str(<<EOM)
932 P1-OU * +OU
933 P2+FU *  *
934 P3-KI+RY *
935 EOM
936     assert_equal(false, b.uchifuzume?(true))
937
938     b = ShogiServer::Board.new
939     b.set_from_str(<<EOM)
940 P1-OU * +OU
941 P2+FU *  *
942 P3-NG+RY *
943 EOM
944     assert_equal(false, b.uchifuzume?(true))
945  end
946 end
947
948 class TestBoardForBuoy < Test::Unit::TestCase
949   def setup
950     @board = ShogiServer::Board.new
951   end
952
953   def test_set_from_moves_empty
954     moves = []
955     rt = @board.set_from_moves moves
956     assert_equal(:normal, rt)
957   end
958
959   def test_set_from_moves
960     moves = ["+7776FU", "-3334FU"]
961     assert_nothing_raised do
962       @board.set_from_moves moves
963     end
964
965     correct = ShogiServer::Board.new
966     correct.set_from_str <<EOF
967 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
968 P2 * -HI *  *  *  *  * -KA * 
969 P3-FU-FU-FU-FU-FU-FU * -FU-FU
970 P4 *  *  *  *  *  * -FU *  * 
971 P5 *  *  *  *  *  *  *  *  * 
972 P6 *  * +FU *  *  *  *  *  * 
973 P7+FU+FU * +FU+FU+FU+FU+FU+FU
974 P8 * +KA *  *  *  *  * +HI * 
975 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
976 +
977 EOF
978     assert_equal(correct.to_s, @board.to_s)
979   end
980
981   def test_set_from_moves_error1
982     moves = ["+7776FU", "-3435FU"]
983     assert_raise ArgumentError do
984       @board.set_from_moves moves
985     end
986   end
987
988   def test_set_from_moves_error2
989     moves = ["+7776FU", "+8786FU"]
990     assert_raise ArgumentError do
991       @board.set_from_moves moves
992     end
993   end
994 end # TestBoardForBuoy
995
996 class TestSplitMoves < Test::Unit::TestCase
997   def test_split_moves1
998     rs = ShogiServer::Board::split_moves "+7776FU"
999     assert_equal ["+7776FU"], rs
1000   end
1001
1002   def test_split_moves2
1003     rs = ShogiServer::Board::split_moves "+7776FU-3334FU"
1004     assert_equal ["+7776FU", "-3334FU"], rs
1005   end
1006
1007   def test_split_moves3
1008     assert_nothing_raised do
1009       ShogiServer::Board::split_moves ""
1010     end
1011   end
1012
1013   def test_split_moves_error1
1014     assert_raise ShogiServer::WrongMoves do
1015       ShogiServer::Board::split_moves "dummy"
1016     end
1017   end
1018 end