Debian Codegraph @

old-sample/legacy-checkmate/test/oracleProverLight.t.cc

raw
1 /* oracleProverLightTest.cc
2 */
3 #include "osl/checkmate/oracleProverLight.h"
4 #include "osl/checkmate/dualCheckmateSearcher.h"
5 #include "osl/checkmate/analyzer/checkTableAnalyzer.h"
6 #include "osl/checkmate/checkmateRecorder.h"
7 #include "osl/record/csaString.h"
8 #include "osl/state/hashEffectState.h"
9 #include "osl/apply_move/applyMove.h"
10
11 #include <cppunit/TestCase.h>
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <fstream>
14 using namespace osl;
15 using namespace osl::checkmate;
16
17 class OracleProverLightTest : public CppUnit::TestFixture
18 {
19 CPPUNIT_TEST_SUITE(OracleProverLightTest);
20 CPPUNIT_TEST(testCheckLong);
21 CPPUNIT_TEST(testDistance);
22 CPPUNIT_TEST(testNoCheck);
23 CPPUNIT_TEST(testCheck);
24 CPPUNIT_TEST(testCheckAfterAttack);
25 CPPUNIT_TEST(testPawnCheckmate);
26 CPPUNIT_TEST(testPawnCheckmateLose);
27 CPPUNIT_TEST(testAdjustMove);
28 CPPUNIT_TEST(testInvalidDefense);
29 CPPUNIT_TEST(testInvalidDefenseLose);
30 CPPUNIT_TEST_SUITE_END();
31 public:
32 void testCheckLong();
33 void testInvalidDefense();
34 void testInvalidDefenseLose();
35 void testDistance();
36 void testAdjustMove();
37 void testNoCheck();
38 void testCheck();
39 void testPawnCheckmate();
40 void testPawnCheckmateLose();
41 void testCheckAfterAttack();
42 };
43 CPPUNIT_TEST_SUITE_REGISTRATION(OracleProverLightTest);
44
45 extern bool isShortTest;
46
47 const size_t limit = 400000;
48 static bool isCheckmate(const char *org, const char *similar)
49 {
50 SimpleState state=CsaString(org).getInitialState();
51 HashEffectState e_state(state);
52
53 DualCheckmateSearcher<> checker(limit);
54 checker.setVerbose(! isShortTest);
55 CheckHashTable& table = checker.getTable(state.turn());
56 Move check_move;
57 PathEncoding path(state.turn());
58 AttackOracleAges oracle_age;
59 const bool win = checker.isWinningState(limit, e_state, e_state.getHash(),
60 path, check_move, oracle_age);
61 CPPUNIT_ASSERT(win);
62 const CheckHashRecord *record = table.find(e_state.getHash());
63 CPPUNIT_ASSERT(record);
64 CPPUNIT_ASSERT(record->proofDisproof().isCheckmateSuccess());
65
66 SimpleState state2=CsaString(similar).getInitialState();
67 HashEffectState e_state2(state2);
68 Move best_move;
69 if (e_state2.turn() == BLACK)
70 {
71 OracleProverLight<BLACK> prover;
72 ProofOracleAttack<BLACK> oracle(record);
73 return prover.proofWin(e_state2, oracle, best_move);
74 }
75 else
76 {
77 OracleProverLight<WHITE> prover;
78 ProofOracleAttack<WHITE> oracle(record);
79 return prover.proofWin(e_state2, oracle, best_move);
80 }
81 }
82
83 void OracleProverLightTest::testCheckLong()
84 {
85 CPPUNIT_ASSERT(! isCheckmate(
86 "P1 * * * * * * * -KE-OU\n"
87 "P2 * * * * * * * -KA-KY\n"
88 "P3 * * * * * * * * * \n"
89 "P4 * * * * * * * * * \n"
90 "P5 * * * * * * * * * \n"
91 "P6 * * * * * * * * * \n"
92 "P7 * * * * * * * * * \n"
93 "P8 * * * * * * * * * \n"
94 "P9 * * +OU * * * * * * \n"
95 "P+00KE\n"
96 "P-00AL\n"
97 "+\n",
98 "P1 * * * * * * * -KE-OU\n"
99 "P2 * * * * * * * -FU-KY\n"
100 "P3 * * * * * * * * * \n"
101 "P4 * * * * * * * * * \n"
102 "P5 * * * * * * * * * \n"
103 "P6 * * * * * * * * * \n"
104 "P7 * * * * * * * * * \n"
105 "P8 * * * * * * * * * \n"
106 "P9 * * +OU * * * * * * \n"
107 "P+00KE\n"
108 "P-00AL\n"
109 "+\n"));
110 CPPUNIT_ASSERT(isCheckmate(
111 "P1-KY-KE+GI * * * * * -KY\n"
112 "P2-OU-KE-GI * -KI * * * * \n"
113 "P3 * -FU * * * * +TO * -FU\n"
114 "P4+FU * -FU-GI * * * * * \n"
115 "P5 * * * * * * -KA * * \n"
116 "P6 * * +FU+FU-FU * * * * \n"
117 "P7+KE+FU+KA+KI * * * * +FU\n"
118 "P8 * * +OU * * +KI * * * \n"
119 "P9+KY * * * +FU * * +KE+KY\n"
120 "P+00FU00FU00FU00FU00FU00GI00HI\n"
121 "P-00FU00FU00KI00HI\n"
122 "+\n",
123 "P1-KY-KE+GI * * * * * -KY\n"
124 "P2-OU-KE-GI * -KI * * * * \n"
125 "P3 * -FU * * * * +TO * -FU\n"
126 "P4+FU * -FU-GI * * * * * \n"
127 "P5 * * * * * * -KA * * \n"
128 "P6 * * +FU+FU-FU * * * * \n"
129 "P7+KE+FU+KA+KI * * * * +FU\n"
130 "P8 * * +OU * +FU+KI * * * \n"
131 "P9+KY * * * * * * +KE+KY\n"
132 "P+00FU00FU00FU00FU00FU00GI00HI\n"
133 "P-00FU00FU00KI00HI\n"
134 "+\n"));
135 CPPUNIT_ASSERT(isCheckmate(
136 "P1 * * * * -KI * * * -KY\n"
137 "P2 * +NG+TO * * -KI-OU * * \n"
138 "P3 * * * -FU-FU-FU-KE+FU-FU\n"
139 "P4-FU * * * -KE * -KY-GI * \n"
140 "P5 * * * +UM * * * * * \n"
141 "P6+FU * * * +HI * * * * \n"
142 "P7 * +FU * +FU+FU+FU+FU * +FU\n"
143 "P8 * * * * * +OU * * * \n"
144 "P9+KY+KE-TO * * +KI * +GI+KY\n"
145 "P-00FU00FU00FU00KE00GI00KI00KA\n"
146 "P+00HI\n"
147 "-\n",
148 "P1 * * * * -KI * * * -KY\n"
149 "P2 * +NG+TO * * -KI-OU * * \n"
150 "P3 * * * -FU-FU-FU-KE+FU-FU\n"
151 "P4-FU * * * -KE * -KY-GI * \n"
152 "P5 * * * +UM * * * * * \n"
153 "P6+FU * * * +HI * * * * \n"
154 "P7 * +FU * +FU+FU+FU+FU * +FU\n"
155 "P8+KY * * * * +OU * * * \n"
156 "P9 * +KE-TO * * +KI * +GI+KY\n"
157 "P-00FU00FU00FU00KE00GI00KI00KA\n"
158 "P+00HI\n"
159 "-\n"));
160 CPPUNIT_ASSERT(isCheckmate(
161 "P1 * * * -KI * * * * -KY\n"
162 "P2 * +KI-GI * * * * * * \n"
163 "P3+GI-FU-FU * * * * * -FU\n"
164 "P4-FU * -KE * * -FU-FU-FU * \n"
165 "P5 * * +KA * * * * * * \n"
166 "P6 * * * * -FU * * * * \n"
167 "P7 * +FU+KE+FU * +KA-OU * +FU\n"
168 "P8 * * +OU+GI-NG * * * * \n"
169 "P9 * * +KI * * +KY * * +KY\n"
170 "P-00FU00FU00FU00FU00FU00FU00KE00KI00KY00HI\n"
171 "P+00FU00KE00HI\n"
172 "+\n",
173 "P1 * * * -KI * * * * -KY\n"
174 "P2 * +KI-GI * * * * * * \n"
175 "P3+GI-FU-FU * * * * * -FU\n"
176 "P4-FU * -KE * * -FU-FU-FU * \n"
177 "P5 * * +KA * * * * * * \n"
178 "P6 * * * * -FU * * * * \n"
179 "P7+FU * +KE+FU * +KA-OU * +FU\n"
180 "P8 * * +OU+GI-NG * * * * \n"
181 "P9 * * +KI * * +KY * * +KY\n"
182 "P-00FU00FU00FU00FU00FU00FU00KE00KI00KY00HI\n"
183 "P+00FU00KE00HI\n"
184 "+\n"));
185 CPPUNIT_ASSERT(isCheckmate(
186 "P1-KY * * +TO-KI-OU * * * \n"
187 "P2 * * +RY-GI * * -KI * * \n"
188 "P3-FU * -FU-FU * * * -GI-FU\n"
189 "P4 * * * * -KE-FU * * * \n"
190 "P5 * * * * * -KE-FU * * \n"
191 "P6 * +FU+FU * * * * * +FU\n"
192 "P7+FU+OU * +FU * +FU+GI * * \n"
193 "P8 * * * * -RY * * * * \n"
194 "P9+KY+KE+KE * * * -UM * +KY\n"
195 "P+00FU00FU00FU00GI00KY00KA\n"
196 "P-00FU00FU00KI00KI\n"
197 "-\n",
198 "P1-KY * * +TO-KI-OU * * * \n"
199 "P2 * * +RY-GI * -KI * * * \n"
200 "P3-FU * -FU-FU * * * -GI-FU\n"
201 "P4 * * * * -KE-FU * * * \n"
202 "P5 * * * * * -KE-FU * * \n"
203 "P6 * +FU+FU * * * * * +FU\n"
204 "P7+FU+OU * +FU * +FU+GI * * \n"
205 "P8 * * * * -RY * * * * \n"
206 "P9+KY+KE+KE * * * -UM * +KY\n"
207 "P+00FU00FU00FU00GI00KY00KA\n"
208 "P-00FU00FU00KI00KI\n"
209 "-\n"));
210 CPPUNIT_ASSERT(isCheckmate(
211 "P1-KY * -OU-KI * * * * -KY\n"
212 "P2 * * * * * * -KI * * \n"
213 "P3-FU-FU+NK * -FU-FU * * * \n"
214 "P4 * * * * * -HI * * -FU\n"
215 "P5+FU+FU * -GI * -KE-KE * * \n"
216 "P6+HI * * * +FU+KI * -FU+FU\n"
217 "P7 * * -FU+FU+KA * +FU * * \n"
218 "P8 * * +KI * * * * +FU * \n"
219 "P9+KY * * +OU * * * +KE+KY\n"
220 "P-00FU00FU00FU00GI00GI\n"
221 "P+00FU00GI00KA\n"
222 "-\n",
223 "P1-KY * -OU-KI * * * * -KY\n"
224 "P2 * * * * * * -KI * * \n"
225 "P3-FU-FU+NK * -FU-FU * * * \n"
226 "P4 * * * * * -HI * * -FU\n"
227 "P5+FU+FU * -GI * -KE-KE * * \n"
228 "P6+HI * * * +FU+KI * -FU+FU\n"
229 "P7 * * -FU+FU+KA * +FU * * \n"
230 "P8 * * +KI * * * * +FU * \n"
231 "P9+KY * * +OU * * * +KE+KY\n"
232 "P-00FU00FU00FU00GI00GI\n"
233 "P+00FU00GI00KA\n"
234 "-\n"));
235
236 }
237
238 void OracleProverLightTest::testInvalidDefense()
239 {
240 SimpleState state=CsaString(
241 "P1 * * * * * * * -KE-OU\n"
242 "P2 * * * * * * * -KA-KY\n"
243 "P3 * * * * * * * * * \n"
244 "P4 * * * * * * * * * \n"
245 "P5 * * * * * * * * * \n"
246 "P6 * * * * * * * * * \n"
247 "P7 * * * * * * * * * \n"
248 "P8 * * * * * * * * * \n"
249 "P9 * * +OU * * * * * * \n"
250 "P+00KE\n"
251 "P-00AL\n"
252 "+\n").getInitialState();
253 HashEffectState e_state(state);
254 DualCheckmateSearcher<> checker(limit);
255 checker.setVerbose(!isShortTest);
256 Move check_move;
257 PathEncoding path(BLACK);
258 AttackOracleAges oracle_age;
259 const bool checkmate = checker.isWinningState<BLACK>
260 (10, e_state, e_state.getHash(), path, check_move, oracle_age);
261 CPPUNIT_ASSERT_EQUAL(true, checkmate);
262 const Move m23KE = Move(Square(2,3),KNIGHT,BLACK);
263 CPPUNIT_ASSERT_EQUAL(m23KE, check_move);
264
265 SimpleState state2=CsaString(
266 "P1 * * * * * * * -KE-OU\n"
267 "P2 * * * * * * -FU-KA-KY\n"
268 "P3 * * * * * * * * * \n"
269 "P4 * * * * * * * * * \n"
270 "P5 * * * * * * * * * \n"
271 "P6 * * * * * * * * * \n"
272 "P7 * * * * * * * * * \n"
273 "P8 * * * * * * * * * \n"
274 "P9 * * +OU * * * * * * \n"
275 "P+00KE\n"
276 "P-00AL\n"
277 "+\n").getInitialState();
278 HashEffectState e_state2(state2);
279 CheckHashTable& table = checker.getTable(BLACK);
280 OracleProverLight<BLACK> prover;
281 const CheckHashRecord *record = table.find(e_state.getHash());
282 ProofOracleAttack<BLACK> oracle(record);
283
284 const bool result2 = prover.proofWin(e_state2, oracle, check_move);
285 CPPUNIT_ASSERT_EQUAL(true, result2);
286 CPPUNIT_ASSERT_EQUAL(m23KE, check_move);
287 #if 0
288 // ImmediateCheckmate では無理
289 // さっきと違って -2223FU と取れるが無効. SafeMoveFilter が入れば解決?
290 SimpleState state3=CsaString(
291 "P1 * * * * * * * -KE-OU\n"
292 "P2 * * * * * * -FU-FU-KY\n"
293 "P3 * * * * * * * * * \n"
294 "P4 * * * * * +KA * * * \n"
295 "P5 * * * * * * * * * \n"
296 "P6 * * * * * * * * * \n"
297 "P7 * * * * * * * * * \n"
298 "P8 * * * * * * * * * \n"
299 "P9 * * +OU * * * * * * \n"
300 "P+00KE\n"
301 "P-00AL\n"
302 "+\n").getInitialState();
303 HashEffectState e_state3(state3);
304
305 const bool result3 = prover.proofWin(e_state3, oracle, check_move);
306 CPPUNIT_ASSERT_EQUAL(true, result3);
307 CPPUNIT_ASSERT_EQUAL(m23KE, check_move);
308 #endif
309 SimpleState state4=CsaString(
310 "P1 * * * * * * * -KE-OU\n"
311 "P2 * * * * * * -FU-FU-KY\n"
312 "P3 * * * * * * * * * \n"
313 "P4 * * * * * +HI * * * \n"
314 "P5 * * * * * * * * * \n"
315 "P6 * * * * * * * * * \n"
316 "P7 * * * * * * * * * \n"
317 "P8 * * * * * * * * * \n"
318 "P9 * * +OU * * * * * * \n"
319 "P+00KE\n"
320 "P-00AL\n"
321 "+\n").getInitialState();
322 HashEffectState e_state4(state4);
323
324 const bool result4 = prover.proofWin(e_state4, oracle, check_move);
325 CPPUNIT_ASSERT_EQUAL(false, result4);
326 }
327
328 void OracleProverLightTest::testInvalidDefenseLose()
329 {
330 SimpleState state=CsaString(
331 "P1 * * * * * * * -KE-OU\n"
332 "P2 * * * * * * * -KA-KY\n"
333 "P3 * * * * * * * +KE * \n"
334 "P4 * * * * * * * * * \n"
335 "P5 * * * * * * * * * \n"
336 "P6 * * * * * * * * * \n"
337 "P7 * * * * * * * * * \n"
338 "P8 * * * * * * * * * \n"
339 "P9 * * +OU * * * * * * \n"
340 "P-00AL\n"
341 "-\n").getInitialState();
342 HashEffectState e_state(state);
343 DualCheckmateSearcher<> checker(limit);
344 checker.setVerbose(!isShortTest);
345 PathEncoding path(WHITE);
346 const Move last_move = Move(Square(2,3),KNIGHT,BLACK);
347 const bool checkmate = checker.isLosingState<WHITE>
348 (10, e_state, e_state.getHash(), path, last_move);
349 CPPUNIT_ASSERT_EQUAL(true, checkmate);
350
351 SimpleState state2=CsaString(
352 "P1 * * * * * * * -KE-OU\n"
353 "P2 * * * * * * -FU-KA-KY\n"
354 "P3 * * * * * * * +KE * \n"
355 "P4 * * * * * * * * * \n"
356 "P5 * * * * * * * * * \n"
357 "P6 * * * * * * * * * \n"
358 "P7 * * * * * * * * * \n"
359 "P8 * * * * * * * * * \n"
360 "P9 * * +OU * * * * * * \n"
361 "P-00AL\n"
362 "-\n").getInitialState();
363 HashEffectState e_state2(state2);
364 CheckHashTable& table = checker.getTable(BLACK);
365 OracleProverLight<BLACK> prover;
366 const CheckHashRecord *record = table.find(e_state.getHash());
367 ProofOracleDefense<BLACK> oracle(record);
368
369 const bool result2 = prover.proofLose(e_state2, oracle, last_move);
370 CPPUNIT_ASSERT_EQUAL(true, result2);
371
372 // さっきと違って -2223FU と取れるが無効. SafeMoveFilter が入れば解決?
373 SimpleState state3=CsaString(
374 "P1 * * * * * * * -KE-OU\n"
375 "P2 * * * * * * -FU-FU-KY\n"
376 "P3 * * * * * * * +KE * \n"
377 "P4 * * * * * +KA * * * \n"
378 "P5 * * * * * * * * * \n"
379 "P6 * * * * * * * * * \n"
380 "P7 * * * * * * * * * \n"
381 "P8 * * * * * * * * * \n"
382 "P9 * * +OU * * * * * * \n"
383 "P-00AL\n"
384 "-\n").getInitialState();
385 HashEffectState e_state3(state3);
386
387 const bool result3 = prover.proofLose(e_state3, oracle, last_move);
388 CPPUNIT_ASSERT_EQUAL(true, result3);
389
390 SimpleState state4=CsaString(
391 "P1 * * * * * * * -KE-OU\n"
392 "P2 * * * * * * -FU-FU-KY\n"
393 "P3 * * * * * * * +KE * \n"
394 "P4 * * * * * +HI * * * \n"
395 "P5 * * * * * * * * * \n"
396 "P6 * * * * * * * * * \n"
397 "P7 * * * * * * * * * \n"
398 "P8 * * * * * * * * * \n"
399 "P9 * * +OU * * * * * * \n"
400 "P-00AL\n"
401 "-\n").getInitialState();
402 HashEffectState e_state4(state4);
403
404 const bool result4 = prover.proofLose(e_state4, oracle, last_move);
405 CPPUNIT_ASSERT_EQUAL(false, result4);
406 }
407
408 void OracleProverLightTest::testDistance()
409 {
410 // bug 35
411 SimpleState state=CsaString(
412 "P1 * * * * * * * -OU * \n"
413 "P2 * * * * * * * * * \n"
414 "P3 * * * * * * * +FU * \n"
415 "P4 * * * * * * * * * \n"
416 "P5 * * * * * * * * * \n"
417 "P6 * * * * * * * * * \n"
418 "P7 * * * * * * * * * \n"
419 "P8 * * * * * * * * * \n"
420 "P9 * * +OU * * * * * * \n"
421 "P+00KI\n"
422 "P-00AL\n"
423 "+\n").getInitialState();
424 HashEffectState e_state(state);
425 DualCheckmateSearcher<> checker(limit);
426 checker.setVerbose(!isShortTest);
427 Move check_move = Move::INVALID();
428 const int distance = 3;
429 const PathEncoding path(BLACK, distance);
430 AttackOracleAges oracle_age;
431 const bool win = checker.isWinningState<BLACK>(10, e_state, e_state.getHash(), path, check_move,
432 oracle_age);
433 CPPUNIT_ASSERT_EQUAL(true, win);
434 CPPUNIT_ASSERT_EQUAL(Move(Square(2,2),GOLD,BLACK), check_move);
435
436 SimpleState stateSimilar=CsaString(
437 "P1 * * * * * * * -OU * \n"
438 "P2 * * * * * * * * * \n"
439 "P3 * * * * * * -FU+FU * \n"
440 "P4 * * * * * * * * * \n"
441 "P5 * * * * * * * * * \n"
442 "P6 * * * * * * * * * \n"
443 "P7 * * * * * * * * * \n"
444 "P8 * * * * * * * * * \n"
445 "P9 * * +OU * * * * * * \n"
446 "P+00KI\n"
447 "P-00AL\n"
448 "+\n").getInitialState();
449 HashEffectState e_stateSimilar(stateSimilar);
450
451 CheckHashTable& table = checker.getTable(BLACK);
452 OracleProverLight<BLACK> prover;
453 const CheckHashRecord *record = table.find(e_state.getHash());
454 ProofOracleAttack<BLACK> oracle(record);
455
456 Move checkmate_move2 = Move::INVALID();
457 const int distanceSimilar = 30;
458 const PathEncoding pathSimilar(BLACK, distanceSimilar);
459 const bool result = prover.proofWin(e_stateSimilar, oracle, checkmate_move2);
460 CPPUNIT_ASSERT_EQUAL(check_move, checkmate_move2);
461 CPPUNIT_ASSERT_EQUAL(true, result);
462 }
463
464 void OracleProverLightTest::testAdjustMove()
465 {
466 const Move m1 =
467 Move(Square(8,9),Square(7,9),KING,PTYPE_EMPTY,false,BLACK);
468 Move m2 = m1;
469 m2.setCapturePtype(PAWN);
470 CPPUNIT_ASSERT(m1 != m2);
471 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
472 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
473 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
474 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
475 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
476
477 m2.setCapturePtype(ROOK);
478 CPPUNIT_ASSERT(m1 != m2);
479 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
480 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
481 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
482 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
483 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
484
485 m2.setCapturePtype(PPAWN);
486 CPPUNIT_ASSERT(m1 != m2);
487 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
488 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
489 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
490 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
491 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
492
493 m2.setCapturePtype(PTYPE_EMPTY);
494 CPPUNIT_ASSERT(m1 == m2);
495 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
496 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
497 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
498 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
499 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
500
501 SimpleState state=CsaString(
502 "P1 * * * * * * * -OU * \n"
503 "P2 * * * * * * * * * \n"
504 "P3 * * * * * * * * * \n"
505 "P4 * * * * * * +KE * +KE\n"
506 "P5 * * * * * * * * * \n"
507 "P6 * * * * * * * * * \n"
508 "P7 * * * * * * * * * \n"
509 "P8 * * * * * * * * * \n"
510 "P9 * +OU * * * * * * * \n"
511 "P-00AL\n"
512 "+\n").getInitialState();
513 HashEffectState e_state(state);
514
515 DualCheckmateSearcher<> checker(limit);
516 checker.setVerbose(!isShortTest);
517 CheckHashTable& table = checker.getTable(BLACK);
518 Move checkmate_move = Move::INVALID();
519 PathEncoding path(BLACK);
520 AttackOracleAges oracle_age;
521 const bool win = checker.isWinningState<BLACK>
522 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
523 CPPUNIT_ASSERT(win);
524 CPPUNIT_ASSERT(checkmate_move != Move::INVALID());
525 const CheckHashRecord *record = table.find(e_state.getHash());
526 CPPUNIT_ASSERT(record);
527
528 // 取る駒が違っても詰む
529 SimpleState state2=CsaString(
530 "P1 * * * * * * * -OU * \n"
531 "P2 * * * * * * * -KI * \n"
532 "P3 * * * * * * * * * \n"
533 "P4 * * * * * * +KE * +KE\n"
534 "P5 * * * * * * * * * \n"
535 "P6 * * * * * * * * * \n"
536 "P7 * * * * * * * * * \n"
537 "P8 * * * * * * * * * \n"
538 "P9 * +OU * * * * * * * \n"
539 "P-00AL\n"
540 "+\n").getInitialState();
541 HashEffectState e_state2(state2);
542
543 Move checkmate_move2 = Move::INVALID();
544 OracleProverLight<BLACK> prover;
545 ProofOracleAttack<BLACK> oracle(record);
546 const bool result = prover.proofWin(e_state2, oracle, checkmate_move2);
547 CPPUNIT_ASSERT_EQUAL(true, result);
548 CPPUNIT_ASSERT_EQUAL(checkmate_move.from(), checkmate_move2.from());
549 CPPUNIT_ASSERT_EQUAL(checkmate_move.to(), checkmate_move2.to());
550 CPPUNIT_ASSERT_EQUAL(checkmate_move.ptype(), checkmate_move2.ptype());
551
552 {
553 // 初期位置が違っても詰む
554 HashEffectState state(CsaString(
555 "P1 * * * * +HI * * -KI-OU\n"
556 "P2 * * * * * * * -FU-FU\n"
557 "P3 * * * * * +KI+FU * * \n"
558 "P4 * * * * * * * * * \n"
559 "P5 * * * * * * * * * \n"
560 "P6 * * * * * * * * * \n"
561 "P7 * * * * * * * * * \n"
562 "P8 * * * * * * * * * \n"
563 "P9 * +OU * * * * * * * \n"
564 "P-00AL\n"
565 "+\n").getInitialState());
566
567 DualCheckmateSearcher<> checker(limit);
568 checker.setVerbose(!isShortTest);
569 CheckHashTable& table = checker.getTable(BLACK);
570 Move checkmate_move;
571 PathEncoding path(BLACK);
572 AttackOracleAges oracle_age;
573 const bool win = checker.isWinningState<BLACK>
574 (500, state, state.getHash(), path, checkmate_move, oracle_age);
575 CPPUNIT_ASSERT(win);
576 CPPUNIT_ASSERT(checkmate_move.isNormal());
577 const CheckHashRecord *oracle = table.find(state.getHash());
578 CPPUNIT_ASSERT(oracle);
579
580 // 飛車移動
581 HashEffectState state2(CsaString(
582 "P1 * * * +HI * * * -KI-OU\n"
583 "P2 * * * * * * * -FU-FU\n"
584 "P3 * * * * * +KI+FU * * \n"
585 "P4 * * * * * * * * * \n"
586 "P5 * * * * * * * * * \n"
587 "P6 * * * * * * * * * \n"
588 "P7 * * * * * * * * * \n"
589 "P8 * * * * * * * * * \n"
590 "P9 * +OU * * * * * * * \n"
591 "P-00AL\n"
592 "+\n").getInitialState());
593
594 Move checkmate_move2;
595 OracleProverLight<BLACK> prover;
596 const bool result = prover.proofWin(state2, ProofOracleAttack<BLACK>(oracle), checkmate_move2);
597 CPPUNIT_ASSERT_EQUAL(true, result);
598 CPPUNIT_ASSERT_EQUAL(checkmate_move.to(), checkmate_move2.to());
599 CPPUNIT_ASSERT_EQUAL(checkmate_move.ptype(), checkmate_move2.ptype());
600 CPPUNIT_ASSERT_EQUAL(checkmate_move.capturePtype(), checkmate_move2.capturePtype());
601 CPPUNIT_ASSERT(checkmate_move.from() != checkmate_move2.from());
602 CPPUNIT_ASSERT_EQUAL(Square(6,1), checkmate_move2.from());
603 }
604 }
605
606 void OracleProverLightTest::testNoCheck()
607 {
608 SimpleState state=CsaString(
609 "P1 * * * * * * * -OU * \n"
610 "P2 * * * * * * * * * \n"
611 "P3 * * * * * * * * * \n"
612 "P4 * * * * * * +KE * +KE\n"
613 "P5 * * * * * * * * * \n"
614 "P6 * * * * * * * * * \n"
615 "P7 * * * * * * * * * \n"
616 "P8 * * * * * * * * * \n"
617 "P9 * +OU * * * * * * * \n"
618 "P-00AL\n"
619 "+\n").getInitialState();
620 HashEffectState e_state(state);
621
622 DualCheckmateSearcher<> checker(limit);
623 checker.setVerbose(!isShortTest);
624 CheckHashTable& table = checker.getTable(BLACK);
625 Move checkmate_move = Move::INVALID();
626 PathEncoding path(BLACK);
627 AttackOracleAges oracle_age;
628 const bool win = checker.isWinningState<BLACK>
629 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
630 CPPUNIT_ASSERT(win);
631 CPPUNIT_ASSERT(checkmate_move != Move::INVALID());
632 const CheckHashRecord *record = table.find(e_state.getHash());
633 CPPUNIT_ASSERT(record);
634
635 // 玉が移動していると王手にならない
636 SimpleState state2=CsaString(
637 "P1 * * -OU * * * * * * \n"
638 "P2 * * * * * * * * * \n"
639 "P3 * * * * * * * * * \n"
640 "P4 * * * * * * +KE * +KE\n"
641 "P5 * * * * * * * * * \n"
642 "P6 * * * * * * * * * \n"
643 "P7 * * * * * * * * * \n"
644 "P8 * * * * * * * * * \n"
645 "P9 * +OU * * * * * * * \n"
646 "P-00AL\n"
647 "+\n").getInitialState();
648 HashEffectState e_state2(state2);
649
650 Move checkmate_move2 = Move::INVALID();
651 OracleProverLight<BLACK> prover;
652 ProofOracleAttack<BLACK> oracle(record);
653 const bool result = prover.proofWin(e_state2, oracle, checkmate_move2);
654 CPPUNIT_ASSERT_EQUAL(false, result);
655 }
656
657
658
659 void OracleProverLightTest::testCheck()
660 {
661 HashEffectState e_state
662 (CsaString(
663 "P1+NY+TO * * * * -OU-KE-KY\n"
664 "P2+FU * * * * -GI-KI * *\n"
665 "P3 * +RY * * +UM * -KI-FU-FU\n"
666 "P4 * * +FU-FU * * * * *\n"
667 "P5 * * * -KE+FU * * +FU *\n"
668 "P6-KE * * +FU+GI-FU * * +FU\n"
669 "P7 * * -UM+GI * * * * *\n"
670 "P8 * * * * * * * * * \n"
671 "P9 * +OU * * * * * * -NG\n"
672 "P+00HI00KI00KE00KY00FU00FU00FU00FU00FU\n"
673 "P-00KI00KY00FU00FU\n"
674 "P-00AL\n"
675 "-\n").getInitialState());
676 DualCheckmateSearcher<> checker(limit);
677 checker.setVerbose(!isShortTest);
678 CheckHashTable& table = checker.getTable(WHITE);
679 Move checkmate_move = Move::INVALID();
680 const PathEncoding path(e_state.turn());
681 AttackOracleAges oracle_age;
682 const bool win = checker.isWinningState<WHITE>
683 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
684 CPPUNIT_ASSERT(win);
685 const CheckHashRecord *record = table.find(e_state.getHash());
686 CPPUNIT_ASSERT(record);
687
688 // 詰む局面は詰む
689 Move best_move = Move::INVALID();
690 OracleProverLight<WHITE> prover;
691 ProofOracleAttack<WHITE> oracle(record);
692 const bool result = prover.proofWin(e_state, oracle, best_move);
693 CPPUNIT_ASSERT_EQUAL(true, result);
694 CPPUNIT_ASSERT_EQUAL(record->bestMove->move, best_move);
695 // 類似局面も詰む
696 best_move = Move::INVALID();
697 ApplyMoveOfTurn::doMove(e_state, Move(Square(3,7),PAWN,WHITE));
698 ApplyMoveOfTurn::doMove(e_state, Move(Square(8,2),PAWN,BLACK));
699 CPPUNIT_ASSERT_EQUAL(true, prover.proofWin(e_state, oracle, best_move));
700 CPPUNIT_ASSERT_EQUAL(best_move, record->bestMove->move);
701
702 // 駒が足りないと詰まない
703 {
704 HashEffectState h2 = e_state;
705 ApplyMoveOfTurn::doMove(h2, Move(Square(3,8),GOLD,WHITE));
706 ApplyMoveOfTurn::doMove(h2, Move(Square(4,3),PAWN,BLACK));
707 CPPUNIT_ASSERT_EQUAL(false, prover.proofWin(h2, oracle, best_move));
708 }
709
710 // 玉が動いても詰まない
711 {
712 HashEffectState h2 = e_state;
713 ApplyMoveOfTurn::doMove(h2, Move(Square(9,3),PAWN,WHITE));
714 ApplyMoveOfTurn::doMove(h2, Move(Square(8,9),Square(7,9),KING,PTYPE_EMPTY,false,BLACK));
715 const bool result = prover.proofWin(h2, oracle, best_move);
716 CPPUNIT_ASSERT_EQUAL(false, result);
717 }
718
719 // 王手でも詰まない
720 {
721 HashEffectState h2 = e_state;
722 ApplyMoveOfTurn::doMove(h2, Move(Square(9,3),PAWN,WHITE));
723 ApplyMoveOfTurn::doMove(h2, Move(Square(4,1),GOLD,BLACK));
724 const bool result = prover.proofWin(h2, oracle, best_move);
725 CPPUNIT_ASSERT_EQUAL(false, result);
726 }
727 }
728
729 void OracleProverLightTest::testPawnCheckmate()
730 {
731 SimpleState state=CsaString(
732 "P1+NY+TO * * * * -OU-KE-KY\n"
733 "P2 * * * * * -GI-KI * *\n"
734 "P3 * +RY * * +UM * -KI-FU-FU\n"
735 "P4 * * -KE-FU * * * * *\n"
736 "P5 * * * * +FU * * +FU *\n"
737 "P6 * -KE * +FU+GI-FU * * +FU\n"
738 "P7 * +FU-UM * * * * * *\n"
739 "P8 * * * * * * * * * \n"
740 "P9 * +OU+FU * -GI * * * -NG\n"
741 "P+00KI00HI00KI00KE00KY00FU00FU00FU00FU00FU\n"
742 "P-00KY00FU00FU\n"
743 "P+00AL\n"
744 "-\n").getInitialState();
745 HashEffectState e_state((HashEffectState(state)));
746
747 const PathEncoding path(WHITE);
748 DualCheckmateSearcher<> checker(limit);
749 checker.setVerbose(!isShortTest);
750 CheckHashTable& table = checker.getTable(WHITE);
751 Move checkmate_move = Move::INVALID();
752 AttackOracleAges oracle_age;
753 const bool win = checker.isWinningState<WHITE>
754 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
755 CPPUNIT_ASSERT(win);
756 const CheckHashRecord *record = table.find(e_state.getHash());
757
758 // 詰む局面は詰む 0088FU, 0098KY
759 Move best_move = Move::INVALID();
760 OracleProverLight<WHITE> prover;
761 ProofOracleAttack<WHITE> oracle(record);
762 CPPUNIT_ASSERT_EQUAL(true, prover.proofWin(e_state, oracle, best_move));
763 CPPUNIT_ASSERT_EQUAL(best_move, record->bestMove->move);
764
765 // 類似局面も詰む
766 best_move = Move::INVALID();
767 ApplyMoveOfTurn::doMove(e_state, Move(Square(3,7),PAWN,WHITE));
768 ApplyMoveOfTurn::doMove(e_state, Move(Square(4,3),PAWN,BLACK));
769 CPPUNIT_ASSERT_EQUAL(true, prover.proofWin(e_state, oracle, best_move));
770 CPPUNIT_ASSERT_EQUAL(best_move, record->bestMove->move);
771
772 // 同じ手順では打歩詰になる
773 best_move = Move::INVALID();
774 ApplyMoveOfTurn::doMove(e_state, Move(Square(7,3),LANCE,WHITE));
775 ApplyMoveOfTurn::doMove(e_state, Move(Square(9,9),PAWN,BLACK));
776 CPPUNIT_ASSERT_EQUAL(false, prover.proofWin(e_state, oracle, best_move));
777 }
778
779 void OracleProverLightTest::testPawnCheckmateLose()
780 {
781 SimpleState state=CsaString(
782 "P1 * * * * * * * -KE * \n"
783 "P2 * * * * * * * -OU * \n"
784 "P3 * * * * * * * +FU-FU\n"
785 "P4 * * * * * * +RY * * \n"
786 "P5 * * * * * * * * * \n"
787 "P6 * * * * * * * * * \n"
788 "P7 * * * * * * * * * \n"
789 "P8 * * * * * * * * * \n"
790 "P9 * * +OU * * * * * * \n"
791 "P+00KI\n"
792 "P-00AL\n"
793 "-\n").getInitialState();
794 HashEffectState e_state(state);
795 const PathEncoding path(WHITE);
796 DualCheckmateSearcher<> checker(limit);
797 checker.setVerbose(!isShortTest);
798 CheckHashTable& table = checker.getTable(BLACK);
799 const Move last_move = Move(Square(2,3), PAWN, BLACK);
800 const bool win = checker.isLosingState<WHITE>(500, e_state, e_state.getHash(), path);
801 CPPUNIT_ASSERT(win);
802
803 const CheckHashRecord *record = table.find(e_state.getHash());
804 OracleProverLight<BLACK> prover;
805 ProofOracleDefense<BLACK> oracle(record);
806 CPPUNIT_ASSERT_EQUAL(true, prover.proofLose(e_state, oracle, last_move));
807
808 // 今度は逃げられないので打歩詰 (bug 33)
809 SimpleState stateNoEscape=CsaString(
810 "P1 * * * * * * * -KE-KY\n"
811 "P2 * * * * * * * -OU-KE\n"
812 "P3 * * * * * * * +FU-FU\n"
813 "P4 * * * * * * +RY * * \n"
814 "P5 * * * * * * * * * \n"
815 "P6 * * * * * * * * * \n"
816 "P7 * * * * * * * * * \n"
817 "P8 * * * * * * * * * \n"
818 "P9 * * +OU * * * * * * \n"
819 "P+00KI\n"
820 "P-00AL\n"
821 "-\n").getInitialState();
822 HashEffectState e_stateNoEscape(stateNoEscape);
823 CPPUNIT_ASSERT_EQUAL(false, prover.proofLose(e_stateNoEscape, oracle, last_move));
824 }
825
826 void OracleProverLightTest::testCheckAfterAttack()
827 {
828 SimpleState state=CsaString(
829 "P1+NY+TO * * * * -OU-KE-KY\n"
830 "P2+FU * * * * -GI-KI * *\n"
831 "P3 * +RY * * +UM * -KI-FU-FU\n"
832 "P4 * * +FU-FU * * * * *\n"
833 "P5 * * -KE * +FU * * +FU *\n"
834 "P6-KE * * +FU+GI-FU * * +FU\n"
835 "P7 * * -UM * * * * * *\n"
836 "P8 * * * * * * * * * \n"
837 "P9-KI+OU * -GI * * * * -NG\n"
838 "P+00HI00KI00KE00KY00FU00FU00FU00FU00FU\n"
839 "P-00KY00FU00FU\n"
840 "P-00AL\n"
841 "+\n").getInitialState();
842 HashEffectState e_state((HashEffectState(state)));
843 const PathEncoding path(BLACK);
844
845 DualCheckmateSearcher<> checker(limit);
846 checker.setVerbose(!isShortTest);
847 CheckHashTable& table = checker.getTable(WHITE);
848 const bool lose = checker.isLosingState<BLACK>(500, e_state, e_state.getHash(), path);
849 CPPUNIT_ASSERT(lose);
850 const CheckHashRecord *record = table.find(e_state.getHash());
851
852 // 詰む局面は詰む
853 OracleProverLight<WHITE> prover;
854 ProofOracleDefense<WHITE> oracle(record);
855 CPPUNIT_ASSERT_EQUAL(true, prover.proofLose(e_state, oracle));
856
857 // 類似局面も詰む
858 ApplyMoveOfTurn::doMove(e_state, Move(Square(4,3),PAWN,BLACK));
859 ApplyMoveOfTurn::doMove(e_state, Move(Square(3,7),PAWN,WHITE));
860 CPPUNIT_ASSERT_EQUAL(true, prover.proofLose(e_state, oracle));
861 CPPUNIT_ASSERT_EQUAL(true, checker.isLosingState<BLACK>(1, e_state, e_state.getHash(), path));
862
863 // 玉が動くと同じ手順では詰まない
864 {
865 HashEffectState h2 = e_state;
866 ApplyMoveOfTurn::doMove(h2, Move(Square(8,9),Square(7,9),KING,PTYPE_EMPTY,false,BLACK));
867 ApplyMoveOfTurn::doMove(h2, Move(Square(9,9),Square(8,9),GOLD,PTYPE_EMPTY,false,WHITE));
868 CPPUNIT_ASSERT_EQUAL(false, prover.proofLose(h2, oracle));
869 }
870 }
871
872 /* ------------------------------------------------------------------------- */
873 // ;;; Local Variables:
874 // ;;; mode:c++
875 // ;;; c-basic-offset:2
876 // ;;; End: