OSDN Git Service

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