OSDN Git Service

Improved the test case.
[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 module ShogiServer
9 class Board
10   def set_from_str(strs)
11     strs.split(/\n/).each do |str|
12       if (str =~ /^P\d/)
13         str.sub!(/^P(.)/, '')
14         y = $1.to_i
15         x = 9
16         while (str.length > 2)
17           str.sub!(/^(...?)/, '')
18           one = $1
19           if (one =~ /^([\+\-])(..)/)
20             sg = $1
21             name = $2
22             if (sg == "+")
23               sente = true
24             else
25               sente = false
26             end
27             if ((x < 1) || (9 < x) || (y < 1) || (9 < y))
28               raise "bad position #{x} #{y}"
29             end
30             case (name)
31             when "FU"
32               PieceFU::new(self, x, y, sente)
33             when "KY"
34               PieceKY::new(self, x, y, sente)
35             when "KE"
36               PieceKE::new(self, x, y, sente)
37             when "GI"
38               PieceGI::new(self, x, y, sente)
39             when "KI"
40               PieceKI::new(self, x, y, sente)
41             when "OU"
42               PieceOU::new(self, x, y, sente)
43             when "KA"
44               PieceKA::new(self, x, y, sente)
45             when "HI"
46               PieceHI::new(self, x, y, sente)
47             when "TO"
48               PieceFU::new(self, x, y, sente, true)
49             when "NY"
50               PieceKY::new(self, x, y, sente, true)
51             when "NK"
52               PieceKE::new(self, x, y, sente, true)
53             when "NG"
54               PieceGI::new(self, x, y, sente, true)
55             when "UM"
56               PieceKA::new(self, x, y, sente, true)
57             when "RY"
58               PieceHI::new(self, x, y, sente, true)
59             else
60               raise "unkown piece #{name}"
61             end
62           end
63           x = x - 1
64         end
65       elsif (str =~ /^P([\+\-])/)
66         sg = $1
67         if (sg == "+")
68           sente = true
69         else
70           sente = false
71         end
72         str.sub!(/^../, '')
73         while (str.length > 3)
74           str.sub!(/^..(..)/, '')
75           name = $1
76           case (name)
77           when "FU"
78             PieceFU::new(self, 0, 0, sente)
79           when "KY"
80             PieceKY::new(self, 0, 0, sente)
81           when "KE"
82             PieceKE::new(self, 0, 0, sente)
83           when "GI"
84             PieceGI::new(self, 0, 0, sente)
85           when "KI"
86             PieceKI::new(self, 0, 0, sente)
87           when "KA"
88             PieceKA::new(self, 0, 0, sente)
89           when "HI"
90             PieceHI::new(self, 0, 0, sente)
91           else
92             raise "unkown piece #{name}"
93           end
94         end
95       end
96     end
97   end
98 end
99 end
100
101 class Test_kachi < Test::Unit::TestCase
102   def test_kachi_good
103     b = ShogiServer::Board.new
104     b.set_from_str(<<EOM)
105 P1+HI+HI+KA+KA+OU *  *  *  * 
106 P2+FU+FU+FU+FU+FU+FU *  *  * 
107 P+00FU00FU
108 EOM
109     assert_equal(true, b.good_kachi?(true))
110
111     b = ShogiServer::Board.new
112     b.set_from_str(<<EOM)
113 P8-HI-HI-KA-KA-OU *  *  *  * 
114 P9-FU-FU-FU-FU-FU-FU *  *  * 
115 P-00FU
116 EOM
117     assert_equal(true, b.good_kachi?(false))
118   end
119
120   def test_kachi_good
121     b = ShogiServer::Board.new
122     b.set_from_str(<<EOM)
123 P1+HI+HI+KA+KA+OU *  *  *  * 
124 P2+FU+FU+FU+FU+FU+FU *  *  * 
125 P+00FU00FU
126 EOM
127     assert_equal(true, b.good_kachi?(true))
128
129     b = ShogiServer::Board.new
130     b.set_from_str(<<EOM)
131 P8-HI-HI-KA-KA-OU *  *  *  * 
132 P9-FU-FU-FU-FU-FU-FU *  *  * 
133 P-00FU
134 EOM
135     assert_equal(true, b.good_kachi?(false))
136   end
137
138   def test_kachi_bad
139     b = ShogiServer::Board.new
140     b.set_from_str(<<EOM)
141 P1+HI+HI+KA+KA+OU *  *  *  * 
142 P2+FU+FU+FU+FU+FU+FU *  *  * 
143 P+00FU
144 EOM
145     assert_equal(false, b.good_kachi?(true)) # point is not enough
146
147     b = ShogiServer::Board.new
148     b.set_from_str(<<EOM)
149 P8-HI-HI-KA-KA-OU *  *  *  * 
150 P9-FU-FU-FU-FU-FU-FU *  *  * 
151 EOM
152     assert_equal(false, b.good_kachi?(false)) # point is not enough
153
154     b = ShogiServer::Board.new
155     b.set_from_str(<<EOM)
156 P1+HI+HI+KA+KA+OU *  *  *  * 
157 P2+FU+FU+FU+FU+FU *  *  *  *
158 P+00FU00FU00FU
159 EOM
160     assert_equal(false, b.good_kachi?(true)) # number on board is not enough
161
162     b = ShogiServer::Board.new
163     b.set_from_str(<<EOM)
164 P8-HI-HI-KA-KA-OU *  *  *  * 
165 P9-FU-FU-FU-FU-FU *  *  *  * 
166 P-00FU00FU
167 EOM
168     assert_equal(false, b.good_kachi?(false)) # number on board is not enough
169
170     b = ShogiServer::Board.new
171     b.set_from_str(<<EOM)
172 P1+HI+HI+KA+KA+OU *  *  * -HI
173 P2+FU+FU+FU+FU+FU+FU *  *  * 
174 P+00FU00FU
175 EOM
176     assert_equal(false, b.good_kachi?(true)) # checkmate
177
178     b = ShogiServer::Board.new
179     b.set_from_str(<<EOM)
180 P8-HI-HI-KA-KA-OU *  *  * +HI
181 P9-FU-FU-FU-FU-FU-FU *  *  * 
182 P-00FU
183 EOM
184     assert_equal(false, b.good_kachi?(false)) # checkmate
185
186     b = ShogiServer::Board.new
187     b.set_from_str(<<EOM)
188 P1+TO+TO * +TO+TO+OU * +TO * 
189 P2 *  *  *  *  *  *  *  * +KI
190 P3 *  *  * +TO+NG+TO+TO+TO+NY
191 P4 *  *  *  *  *  *  *  *  * 
192 P5 *  *  *  *  *  *  *  * +UM
193 P6 *  *  * -KI-NG-RY *  *  * 
194 P7-TO * -TO-NG * -TO-TO-TO * 
195 P8-RY *  * -NK-TO-OU-TO-TO * 
196 P9 * -TO *  *  *  *  *  *  * 
197 P+00KI00KI00KE
198 P-00KA00GI00KE00KE00KY00KY00KY
199 -
200 EOM
201     assert_equal(true, b.good_kachi?(false))
202   end
203 end
204
205 class Test_gps < Test::Unit::TestCase
206   def test_gote_promote
207     b = ShogiServer::Board.new
208     b.set_from_str(<<EOM)
209 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
210 P2 * -HI *  *  *  *  *  *  * 
211 P3-FU * -FU-FU-FU-FU-KA-FU-FU
212 P4 *  *  *  *  *  *  *  *  * 
213 P5 * -FU+FU *  *  *  *  *  * 
214 P6 *  *  *  *  *  *  *  *  * 
215 P7+FU+FU+KE+FU+FU+FU+FU+FU+FU
216 P8 *  *  *  *  *  *  * +HI * 
217 P9+KY * +GI+KI+OU+KI+GI+KE+KY
218 P+00FU
219 P-00KA
220 EOM
221     assert_equal(:normal, b.handle_one_move("-3377UM"))
222   end
223
224   def test_capture_promoted_and_put
225     b = ShogiServer::Board.new
226     b.set_from_str(<<EOM)
227 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
228 P2 * -HI *  *  *  *  * -KA * 
229 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
230 P4 *  *  *  *  *  *  *  *  * 
231 P5 *  *  *  *  *  *  *  *  * 
232 P6 *  *  *  *  *  *  *  *  * 
233 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
234 P8 * +KA *  *  *  *  * +HI * 
235 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
236 EOM
237
238     assert_equal(:normal, b.handle_one_move("+7776FU"))
239     assert_equal(:normal, b.handle_one_move("-3334FU"))
240     assert_equal(:normal, b.handle_one_move("+2726FU"))
241     assert_equal(:normal, b.handle_one_move("-4344FU"))
242     assert_equal(:normal, b.handle_one_move("+3948GI"))
243     assert_equal(:normal, b.handle_one_move("-8242HI"))
244     assert_equal(:normal, b.handle_one_move("+5756FU"))
245     assert_equal(:normal, b.handle_one_move("-3132GI"))
246     assert_equal(:normal, b.handle_one_move("+5968OU"))
247     assert_equal(:normal, b.handle_one_move("-5162OU"))
248     assert_equal(:normal, b.handle_one_move("+6878OU"))
249     assert_equal(:normal, b.handle_one_move("-6272OU"))
250     assert_equal(:normal, b.handle_one_move("+4958KI"))
251     assert_equal(:normal, b.handle_one_move("-7282OU"))
252     assert_equal(:normal, b.handle_one_move("+9796FU"))
253     assert_equal(:normal, b.handle_one_move("-9394FU"))
254     assert_equal(:normal, b.handle_one_move("+2625FU"))
255     assert_equal(:normal, b.handle_one_move("-2233KA"))
256     assert_equal(:normal, b.handle_one_move("+3736FU"))
257     assert_equal(:normal, b.handle_one_move("-7172GI"))
258     assert_equal(:normal, b.handle_one_move("+7968GI"))
259     assert_equal(:normal, b.handle_one_move("-4152KI"))
260     assert_equal(:normal, b.handle_one_move("+6857GI"))
261     assert_equal(:normal, b.handle_one_move("-3243GI"))
262     assert_equal(:normal, b.handle_one_move("+6968KI"))
263     assert_equal(:normal, b.handle_one_move("-5354FU"))
264     assert_equal(:normal, b.handle_one_move("+1716FU"))
265     assert_equal(:normal, b.handle_one_move("-1314FU"))
266     assert_equal(:normal, b.handle_one_move("+4746FU"))
267     assert_equal(:normal, b.handle_one_move("-6364FU"))
268     assert_equal(:normal, b.handle_one_move("+4645FU"))
269     assert_equal(:normal, b.handle_one_move("-5263KI"))
270     assert_equal(:normal, b.handle_one_move("+2937KE"))
271     assert_equal(:normal, b.handle_one_move("-7374FU"))
272     assert_equal(:normal, b.handle_one_move("+2524FU"))
273     assert_equal(:normal, b.handle_one_move("-2324FU"))
274     assert_equal(:normal, b.handle_one_move("+4544FU"))
275     assert_equal(:normal, b.handle_one_move("-4344GI"))
276     assert_equal(:normal, b.handle_one_move("+0045FU"))
277     assert_equal(:normal, b.handle_one_move("-4445GI"))
278     assert_equal(:normal, b.handle_one_move("+8833UM"))
279     assert_equal(:normal, b.handle_one_move("-2133KE"))
280     assert_equal(:normal, b.handle_one_move("+0088KA"))
281     assert_equal(:normal, b.handle_one_move("-5455FU"))
282     assert_equal(:normal, b.handle_one_move("+8855KA"))
283     assert_equal(:normal, b.handle_one_move("-4243HI"))
284     assert_equal(:normal, b.handle_one_move("+2824HI"))
285     assert_equal(:normal, b.handle_one_move("-4554GI"))
286     assert_equal(:normal, b.handle_one_move("+0044FU"))
287     assert_equal(:normal, b.handle_one_move("-4353HI"))
288     assert_equal(:normal, b.handle_one_move("+2422RY"))
289     assert_equal(:normal, b.handle_one_move("-5455GI"))
290     assert_equal(:normal, b.handle_one_move("+5655FU"))
291     assert_equal(:normal, b.handle_one_move("-0056FU"))
292     assert_equal(:normal, b.handle_one_move("+5756GI"))
293     assert_equal(:normal, b.handle_one_move("-0057FU"))
294     assert_equal(:normal, b.handle_one_move("+4857GI"))
295     assert_equal(:normal, b.handle_one_move("-9495FU"))
296     assert_equal(:normal, b.handle_one_move("+9695FU"))
297     assert_equal(:normal, b.handle_one_move("-0096FU"))
298     assert_equal(:normal, b.handle_one_move("+9996KY"))
299     assert_equal(:normal, b.handle_one_move("-0085KA"))
300   end
301 end
302
303
304 class Test_promote < Test::Unit::TestCase
305   def test_fu
306     b = ShogiServer::Board.new
307     b.set_from_str(<<EOM)
308 P1-OU * +OU *
309 P2 *  *  *  *
310 P3+FU *  * +HI
311 P4 * +FU *  *
312 P5 *  * +FU *
313 EOM
314     assert_equal(:normal, b.handle_one_move("+9392TO"))
315     assert_equal(:normal, b.handle_one_move("+8483TO"))
316     assert_equal(:illegal, b.handle_one_move("+7574TO"))
317     assert_equal(:normal, b.handle_one_move("+6364RY"))
318   end
319 end
320
321 class Test_move < Test::Unit::TestCase
322   def test_fu
323     b = ShogiServer::Board.new
324     b.set_from_str(<<EOM)
325 P1-OU * +OU
326 P2 * +FU *
327 EOM
328     assert_equal(:illegal, b.handle_one_move("+8281FU"))
329   end
330   def test_hi
331     b = ShogiServer::Board.new
332     b.set_from_str(<<EOM)
333 P1-OU * +OU
334 P2 * +HI *
335 EOM
336     assert_equal(:normal, b.handle_one_move("+8212HI"))
337     assert_equal(:illegal, b.handle_one_move("+1223HI"))
338   end
339   def test_ry
340     b = ShogiServer::Board.new
341     b.set_from_str(<<EOM)
342 P1-OU * +OU
343 P2 * +RY *
344 EOM
345     assert_equal(:normal, b.handle_one_move("+8212RY"))
346     assert_equal(:normal, b.handle_one_move("+1223RY"))
347   end
348 end
349
350 class Test_put < Test::Unit::TestCase
351   def test_fu
352     b = ShogiServer::Board.new
353     b.set_from_str(<<EOM)
354 P1-OU * +OU
355 P+00FU00FU
356 EOM
357     assert_equal(:illegal, b.handle_one_move("+0011FU"))
358     assert_equal(:normal, b.handle_one_move("+0022FU"))
359   end
360   def test_ky
361      b = ShogiServer::Board.new
362      b.set_from_str(<<EOM)
363 P1-OU * +OU
364 P+00KY00KY
365 EOM
366     assert_equal(:illegal, b.handle_one_move("+0011KY"))
367     assert_equal(:normal, b.handle_one_move("+0022KY"))
368   end
369
370   def test_ke
371     b = ShogiServer::Board.new
372     b.set_from_str(<<EOM)
373 P1-OU * +OU
374 P+00KE00KE00KE
375 EOM
376     assert_equal(:illegal, b.handle_one_move("+0011KE"))
377     assert_equal(:illegal, b.handle_one_move("+0022KE"))
378     assert_equal(:normal, b.handle_one_move("+0033KE"))
379   end
380 end
381
382
383 class Test_2fu < Test::Unit::TestCase
384   def test_2fu
385     b = ShogiServer::Board.new
386     b.set_from_str(<<EOM)
387 P1-OU * +OU
388 P+00FU00FU
389 EOM
390     assert_equal(:normal, b.handle_one_move("+0022FU"))
391     assert_equal(:illegal, b.handle_one_move("+0023FU"))
392
393     b = ShogiServer::Board.new
394     b.set_from_str(<<EOM)
395 P1-OU * +OU
396 P+00FU00FU
397 EOM
398     assert_equal(:normal, b.handle_one_move("+0022FU"))
399     assert_equal(:normal, b.handle_one_move("+0032FU"))
400   end
401 end
402
403 class Test_sennichite < Test::Unit::TestCase
404   def test_oute_sennichite0
405     b = ShogiServer::Board.new
406     b.set_from_str(<<EOM)
407 P1-OU *  * +OU
408 P2 *  * +HI *
409 EOM
410 ##    b.history[b.to_s] = 1
411     assert_equal(:normal, b.handle_one_move("+7271HI")) #1
412     assert_equal(:normal, b.handle_one_move("-9192OU"))
413     assert_equal(:normal, b.handle_one_move("+7172HI"))
414     assert_equal(:normal, b.handle_one_move("-9291OU"))
415
416     assert_equal(:normal, b.handle_one_move("+7271HI")) # 2
417     assert_equal(:normal, b.handle_one_move("-9192OU"))
418     assert_equal(:normal, b.handle_one_move("+7172HI"))
419     assert_equal(:normal, b.handle_one_move("-9291OU"))
420
421     assert_equal(:normal, b.handle_one_move("+7271HI")) # 3
422     assert_equal(:normal, b.handle_one_move("-9192OU"))
423     assert_equal(:normal, b.handle_one_move("+7172HI"))
424     assert_equal(:normal, b.handle_one_move("-9291OU"))
425
426     assert_equal(:oute_sennichite_sente_lose, b.handle_one_move("+7271HI")) # 4
427   end
428
429   def test_oute_sennichite1 #330
430     b = ShogiServer::Board.new
431     b.set_from_str(<<EOM)
432 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
433 P2 * -HI *  *  *  *  * -KA * 
434 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
435 P4 *  *  *  *  *  *  *  *  * 
436 P5 *  *  *  *  *  *  *  *  * 
437 P6 *  *  *  *  *  *  *  *  * 
438 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
439 P8 * +KA *  *  *  *  * +HI * 
440 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
441 EOM
442 b.history[b.to_s] = 1
443
444     assert_equal(:normal, b.handle_one_move("+2726FU"))
445     assert_equal(:normal, b.handle_one_move("-8384FU"))
446     assert_equal(:normal, b.handle_one_move("+2625FU"))
447     assert_equal(:normal, b.handle_one_move("-8485FU"))
448     assert_equal(:normal, b.handle_one_move("+6978KI"))
449     assert_equal(:normal, b.handle_one_move("-4132KI"))
450     assert_equal(:normal, b.handle_one_move("+2524FU"))
451     assert_equal(:normal, b.handle_one_move("-2324FU"))
452     assert_equal(:normal, b.handle_one_move("+2824HI"))
453     assert_equal(:normal, b.handle_one_move("-0023FU"))
454     assert_equal(:normal, b.handle_one_move("+2484HI"))
455     assert_equal(:normal, b.handle_one_move("-8284HI"))
456     assert_equal(:normal, b.handle_one_move("+4938KI"))
457     assert_equal(:normal, b.handle_one_move("-9394FU"))
458     assert_equal(:normal, b.handle_one_move("+5969OU"))
459     assert_equal(:normal, b.handle_one_move("-0049HI"))
460
461     assert_equal(:normal, b.handle_one_move("+6968OU"))
462     assert_equal(:normal, b.handle_one_move("-4948RY"))
463     assert_equal(:normal, b.handle_one_move("+6869OU"))
464     assert_equal(:normal, b.handle_one_move("-4849RY"))
465
466     assert_equal(:normal, b.handle_one_move("+6968OU"))
467     assert_equal(:normal, b.handle_one_move("-4948RY"))
468     assert_equal(:normal, b.handle_one_move("+6869OU"))
469     assert_equal(:normal, b.handle_one_move("-4849RY"))
470
471     assert_equal(:normal, b.handle_one_move("+6968OU"))
472     assert_equal(:normal, b.handle_one_move("-4948RY"))
473     assert_equal(:normal, b.handle_one_move("+6869OU"))
474     assert_equal(:normal, b.handle_one_move("-4849RY"))
475
476     assert_equal(:normal, b.handle_one_move("+6968OU")) # added
477     assert_equal(:oute_sennichite_gote_lose, b.handle_one_move("-4948RY"))
478   end
479
480   def test_not_oute_sennichite
481     b = ShogiServer::Board.new
482     b.set_from_str(<<EOM)
483 P1-OU *  * +OU
484 P2 *  * +HI *
485 EOM
486 ##    b.history[b.to_s] = 1
487     assert_equal(:normal, b.handle_one_move("+7271HI")) #1
488     assert_equal(:normal, b.handle_one_move("-9192OU"))
489     assert_equal(:normal, b.handle_one_move("+7172HI"))
490     assert_equal(:normal, b.handle_one_move("-9291OU"))
491
492     assert_equal(:normal, b.handle_one_move("+7271HI")) # 2
493     assert_equal(:normal, b.handle_one_move("-9192OU"))
494     assert_equal(:normal, b.handle_one_move("+7174HI")) # stop oute here
495     assert_equal(:normal, b.handle_one_move("-9291OU"))
496
497     assert_equal(:normal, b.handle_one_move("+7471HI")) # 3
498     assert_equal(:normal, b.handle_one_move("-9192OU"))
499     assert_equal(:normal, b.handle_one_move("+7172HI"))
500     assert_equal(:normal, b.handle_one_move("-9291OU"))
501
502     assert_equal(:sennichite, b.handle_one_move("+7271HI")) # 4
503   end
504
505   def test_sennichite0
506     b = ShogiServer::Board.new
507     b.set_from_str(<<EOM)
508 P1-OU * +OU
509 EOM
510     b.history[b.to_s] = 1
511     assert_equal(:normal, b.handle_one_move("+7172OU"))
512     assert_equal(:normal, b.handle_one_move("-9192OU"))
513     assert_equal(:normal, b.handle_one_move("+7271OU"))
514     assert_equal(:normal, b.handle_one_move("-9291OU")) # 2
515
516     assert_equal(:normal, b.handle_one_move("+7172OU"))
517     assert_equal(:normal, b.handle_one_move("-9192OU"))
518     assert_equal(:normal, b.handle_one_move("+7271OU"))
519     assert_equal(:normal, b.handle_one_move("-9291OU")) # 3
520
521     assert_equal(:normal, b.handle_one_move("+7172OU"))
522     assert_equal(:normal, b.handle_one_move("-9192OU"))
523     assert_equal(:normal, b.handle_one_move("+7271OU"))
524     assert_equal(:sennichite, b.handle_one_move("-9291OU")) # 4
525   end
526
527   def test_sennichite1          # 329
528     b = ShogiServer::Board.new
529     b.set_from_str(<<EOM)
530 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
531 P2 * -HI *  *  *  *  * -KA * 
532 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
533 P4 *  *  *  *  *  *  *  *  * 
534 P5 *  *  *  *  *  *  *  *  * 
535 P6 *  *  *  *  *  *  *  *  * 
536 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
537 P8 * +KA *  *  *  *  * +HI * 
538 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
539 EOM
540     b.history[b.to_s] = 1
541
542     assert_equal(:normal, b.handle_one_move("+2858HI"))
543     assert_equal(:normal, b.handle_one_move("-8252HI"))
544     assert_equal(:normal, b.handle_one_move("+5828HI"))
545     assert_equal(:normal, b.handle_one_move("-5282HI"))
546     assert_equal(:normal, b.handle_one_move("+2858HI"))
547     assert_equal(:normal, b.handle_one_move("-8252HI"))
548     assert_equal(:normal, b.handle_one_move("+5828HI"))
549     assert_equal(:normal, b.handle_one_move("-5282HI"))
550     assert_equal(:normal, b.handle_one_move("+2858HI"))
551     assert_equal(:normal, b.handle_one_move("-8252HI"))
552     assert_equal(:normal, b.handle_one_move("+5828HI"))
553     assert_equal(:sennichite, b.handle_one_move("-5282HI"))
554   end
555 end
556
557 class Test_checkmate < Test::Unit::TestCase
558   def test_ki
559     b = ShogiServer::Board.new
560     b.set_from_str(<<EOM)
561 P1-OU * +OU
562 P2 * +KI
563 EOM
564     assert_equal(true, b.checkmated?(false)) # gote is loosing
565     assert_equal(false, b.checkmated?(true))
566   end
567
568   def test_hi
569     b = ShogiServer::Board.new
570     b.set_from_str(<<EOM)
571 P1-OU * +HI+OU
572 EOM
573     assert_equal(true, b.checkmated?(false)) # gote is loosing
574     assert_equal(false, b.checkmated?(true))
575
576     b = ShogiServer::Board.new
577     b.set_from_str(<<EOM)
578 P1-OU * 
579 P2 * +HI+OU
580 EOM
581     assert_equal(false, b.checkmated?(false)) # hisha can't capture
582     assert_equal(false, b.checkmated?(true))
583
584     b = ShogiServer::Board.new
585     b.set_from_str(<<EOM)
586 P1-OU * 
587 P2 * +RY+OU
588 EOM
589     assert_equal(true, b.checkmated?(false)) # ryu can capture
590     assert_equal(false, b.checkmated?(true))
591   end
592
593   def test_KE
594     b = ShogiServer::Board.new
595     b.set_from_str(<<EOM)
596 P1-OU * +OU
597 P2 *  *  *
598 P3 * +KE *
599 EOM
600     assert_equal(true, b.checkmated?(false))
601     assert_equal(false, b.checkmated?(true))
602   end
603 end
604
605 class Test_uchifuzume < Test::Unit::TestCase
606   def test_uchifuzume1          # 331
607     b = ShogiServer::Board.new
608     b.set_from_str(<<EOM)
609 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
610 P2 * -HI *  *  *  *  * -KA * 
611 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
612 P4 *  *  *  *  *  *  *  *  * 
613 P5 *  *  *  *  *  *  *  *  * 
614 P6 *  *  *  *  *  *  *  *  * 
615 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
616 P8 * +KA *  *  *  *  * +HI * 
617 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
618 EOM
619
620     assert_equal(:normal, b.handle_one_move("+2726FU"))
621     assert_equal(:normal, b.handle_one_move("-8384FU"))
622     assert_equal(:normal, b.handle_one_move("+2625FU"))
623     assert_equal(:normal, b.handle_one_move("-8485FU"))
624     assert_equal(:normal, b.handle_one_move("+2524FU"))
625     assert_equal(:normal, b.handle_one_move("-2324FU"))
626     assert_equal(:normal, b.handle_one_move("+2824HI"))
627     assert_equal(:normal, b.handle_one_move("-8586FU"))
628     assert_equal(:normal, b.handle_one_move("+8786FU"))
629     assert_equal(:normal, b.handle_one_move("-0087FU"))
630     assert_equal(:normal, b.handle_one_move("+0023FU"))
631     assert_equal(:normal, b.handle_one_move("-8788TO"))
632     assert_equal(:normal, b.handle_one_move("+2322TO"))
633     assert_equal(:normal, b.handle_one_move("-8879TO"))
634     assert_equal(:normal, b.handle_one_move("+2231TO"))
635     assert_equal(:normal, b.handle_one_move("-7969TO"))
636     assert_equal(:normal, b.handle_one_move("+5969OU"))
637     assert_equal(:normal, b.handle_one_move("-8286HI"))
638     assert_equal(:normal, b.handle_one_move("+3141TO"))
639     assert_equal(:normal, b.handle_one_move("-5141OU"))
640     assert_equal(:normal, b.handle_one_move("+2484HI"))
641     assert_equal(:normal, b.handle_one_move("-8684HI"))
642     assert_equal(:normal, b.handle_one_move("+6978OU"))
643     assert_equal(:normal, b.handle_one_move("-8424HI"))
644     assert_equal(:normal, b.handle_one_move("+7776FU"))
645     assert_equal(:normal, b.handle_one_move("-7374FU"))
646     assert_equal(:normal, b.handle_one_move("+7675FU"))
647     assert_equal(:normal, b.handle_one_move("-7475FU"))
648     assert_equal(:normal, b.handle_one_move("+0079KI"))
649     assert_equal(:normal, b.handle_one_move("-7576FU"))
650     assert_equal(:normal, b.handle_one_move("+7888OU"))
651     assert_equal(:normal, b.handle_one_move("-7677TO"))
652     assert_equal(:normal, b.handle_one_move("+8877OU"))
653     assert_equal(:normal, b.handle_one_move("-2474HI"))
654     assert_equal(:normal, b.handle_one_move("+7788OU"))
655     assert_equal(:normal, b.handle_one_move("-0086KI"))
656     assert_equal(:normal, b.handle_one_move("+9998KY"))
657     assert_equal(:normal, b.handle_one_move("-7424HI"))
658     assert_equal(:normal, b.handle_one_move("+0099GI"))
659     assert_equal(:normal, b.handle_one_move("-0028HI"))
660     assert_equal(:normal, b.handle_one_move("+0078FU"))
661     assert_equal(:uchifuzume, b.handle_one_move("-0087FU"))
662   end
663
664   def test_uchifuzume2
665     # 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
666     b = ShogiServer::Board.new
667     b.set_from_str(<<EOM)
668 P1-KY-KE * -KI * -OU * -KE-KY
669 P2 *  * +TO *  *  *  *  *  *
670 P3 *  *  *  * -KI-KI * -FU *
671 P4 *  * -FU * -FU-FU-FU *  *
672 P5 * -RY+GI+OU *  *  * +FU+FU
673 P6-FU * +FU+KI+GI+FU+FU * +KY
674 P7 *  *  * -RY *  *  *  *  *
675 P8 *  *  *  *  *  *  *  *  *
676 P9+KY+KE *  *  *  *  *  *  *
677 P+00FU00FU00FU00FU00FU00GI00GI00KA00KE
678 P-00FU00KA
679 -
680 EOM
681     assert_equal(:uchifuzume, b.handle_one_move("-0064FU"))
682   end
683
684   def test_uchifuzume3
685     # 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
686     b = ShogiServer::Board.new
687     b.set_from_str(<<EOM)
688 P1 * -GI * -KI-OU * -GI * -KY
689 P2 *  *  *  * -FU *  *  *  *
690 P3+OU * -FU-FU * -FU-KI-FU *
691 P4+KI+KE-RY * -GI *  *  * -FU
692 P5 *  *  *  *  *  *  *  *  *
693 P6+FU *  *  * +KY *  *  * +FU
694 P7 * +FU *  * +FU *  *  *  *
695 P8 * +GI *  *  *  *  *  *  *
696 P9+KY+KE *  *  *  *  *  * -UM
697 P+00KA00KI00KE00KY00FU00FU00FU
698 P-00HI00KE00FU00FU00FU00FU00FU
699 -
700 EOM
701     assert_equal(:normal, b.handle_one_move("-0092FU"))
702   end
703
704   def test_ou
705     b = ShogiServer::Board.new
706     b.set_from_str(<<EOM)
707 P1-OU * +OU
708 P2+FU *  *
709 P3 * +HI *
710 EOM
711     assert_equal(false, b.uchifuzume?(true))
712
713     b = ShogiServer::Board.new
714     b.set_from_str(<<EOM)
715 P1-OU * +OU
716 P2+FU *  *
717 P3 * +RY *
718 EOM
719     assert_equal(true, b.uchifuzume?(true))
720
721     b = ShogiServer::Board.new
722     b.set_from_str(<<EOM)
723 P2-OU * +OU
724 P3+FU *  *
725 P4 * +RY *
726 EOM
727     assert_equal(false, b.uchifuzume?(true)) # ou can move backward
728
729     b = ShogiServer::Board.new
730     b.set_from_str(<<EOM)
731 P2-OU * +OU
732 P3+FU * +KA
733 P4 * +RY *
734 EOM
735     assert_equal(true, b.uchifuzume?(true)) # ou can move backward and kaku can capture it
736  end                   
737
738
739   def test_friend
740     b = ShogiServer::Board.new
741     b.set_from_str(<<EOM)
742 P1-OU * +OU
743 P2+FU * -HI
744 P3 * +RY *
745 EOM
746     assert_equal(false, b.uchifuzume?(true))
747
748     b = ShogiServer::Board.new
749     b.set_from_str(<<EOM)
750 P1-OU * +OU
751 P2+FU * +FU-HI
752 P3 * +RY *
753 EOM
754     assert_equal(true, b.uchifuzume?(true)) # hisha blocked by fu
755
756     b = ShogiServer::Board.new
757     b.set_from_str(<<EOM)
758 P1-OU * +OU
759 P2+FU *  *
760 P3-GI+RY *
761 EOM
762     assert_equal(true, b.uchifuzume?(true))
763
764     b = ShogiServer::Board.new
765     b.set_from_str(<<EOM)
766 P1-OU * +OU
767 P2+FU *  *
768 P3-KI+RY *
769 EOM
770     assert_equal(false, b.uchifuzume?(true))
771
772     b = ShogiServer::Board.new
773     b.set_from_str(<<EOM)
774 P1-OU * +OU
775 P2+FU *  *
776 P3-NG+RY *
777 EOM
778     assert_equal(false, b.uchifuzume?(true))
779  end
780 end