Debian Codegraph @

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

raw
1 /* oracleProverTest.cc
2 */
3 #include "osl/checkmate/oracleProver.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 OracleProverTest : public CppUnit::TestFixture
18 {
19 CPPUNIT_TEST_SUITE(OracleProverTest);
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(OracleProverTest);
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 OracleProver<CheckHashTable> prover(table);
69 Move best_move;
70 const bool result = prover.proofWin(e_state2, path, record, best_move);
71 if (! result)
72 {
73 CheckTableAnalyzer analyzer(table.getTwinTable());
74 std::ofstream os("oracleProverTest.log");
75 analyzer.showProofTree(record,e_state.getHash(),path,true,os);
76
77 CheckmateRecorder::DepthTracer::maxVerboseLogDepth = 20;
78 prover.proofWin(e_state2, path, record, best_move);
79 CheckmateRecorder::DepthTracer::maxVerboseLogDepth = 1;
80 }
81
82 return result;
83 }
84
85 void OracleProverTest::testCheckLong()
86 {
87 CPPUNIT_ASSERT(! isCheckmate(
88 "P1 * * * * * * * -KE-OU\n"
89 "P2 * * * * * * * -KA-KY\n"
90 "P3 * * * * * * * * * \n"
91 "P4 * * * * * * * * * \n"
92 "P5 * * * * * * * * * \n"
93 "P6 * * * * * * * * * \n"
94 "P7 * * * * * * * * * \n"
95 "P8 * * * * * * * * * \n"
96 "P9 * * +OU * * * * * * \n"
97 "P+00KE\n"
98 "P-00AL\n"
99 "+\n",
100 "P1 * * * * * * * -KE-OU\n"
101 "P2 * * * * * * * -FU-KY\n"
102 "P3 * * * * * * * * * \n"
103 "P4 * * * * * * * * * \n"
104 "P5 * * * * * * * * * \n"
105 "P6 * * * * * * * * * \n"
106 "P7 * * * * * * * * * \n"
107 "P8 * * * * * * * * * \n"
108 "P9 * * +OU * * * * * * \n"
109 "P+00KE\n"
110 "P-00AL\n"
111 "+\n"));
112 CPPUNIT_ASSERT(isCheckmate(
113 "P1-KY-KE+GI * * * * * -KY\n"
114 "P2-OU-KE-GI * -KI * * * * \n"
115 "P3 * -FU * * * * +TO * -FU\n"
116 "P4+FU * -FU-GI * * * * * \n"
117 "P5 * * * * * * -KA * * \n"
118 "P6 * * +FU+FU-FU * * * * \n"
119 "P7+KE+FU+KA+KI * * * * +FU\n"
120 "P8 * * +OU * * +KI * * * \n"
121 "P9+KY * * * +FU * * +KE+KY\n"
122 "P+00FU00FU00FU00FU00FU00GI00HI\n"
123 "P-00FU00FU00KI00HI\n"
124 "+\n",
125 "P1-KY-KE+GI * * * * * -KY\n"
126 "P2-OU-KE-GI * -KI * * * * \n"
127 "P3 * -FU * * * * +TO * -FU\n"
128 "P4+FU * -FU-GI * * * * * \n"
129 "P5 * * * * * * -KA * * \n"
130 "P6 * * +FU+FU-FU * * * * \n"
131 "P7+KE+FU+KA+KI * * * * +FU\n"
132 "P8 * * +OU * +FU+KI * * * \n"
133 "P9+KY * * * * * * +KE+KY\n"
134 "P+00FU00FU00FU00FU00FU00GI00HI\n"
135 "P-00FU00FU00KI00HI\n"
136 "+\n"));
137 CPPUNIT_ASSERT(isCheckmate(
138 "P1 * * * * -KI * * * -KY\n"
139 "P2 * +NG+TO * * -KI-OU * * \n"
140 "P3 * * * -FU-FU-FU-KE+FU-FU\n"
141 "P4-FU * * * -KE * -KY-GI * \n"
142 "P5 * * * +UM * * * * * \n"
143 "P6+FU * * * +HI * * * * \n"
144 "P7 * +FU * +FU+FU+FU+FU * +FU\n"
145 "P8 * * * * * +OU * * * \n"
146 "P9+KY+KE-TO * * +KI * +GI+KY\n"
147 "P-00FU00FU00FU00KE00GI00KI00KA\n"
148 "P+00HI\n"
149 "-\n",
150 "P1 * * * * -KI * * * -KY\n"
151 "P2 * +NG+TO * * -KI-OU * * \n"
152 "P3 * * * -FU-FU-FU-KE+FU-FU\n"
153 "P4-FU * * * -KE * -KY-GI * \n"
154 "P5 * * * +UM * * * * * \n"
155 "P6+FU * * * +HI * * * * \n"
156 "P7 * +FU * +FU+FU+FU+FU * +FU\n"
157 "P8+KY * * * * +OU * * * \n"
158 "P9 * +KE-TO * * +KI * +GI+KY\n"
159 "P-00FU00FU00FU00KE00GI00KI00KA\n"
160 "P+00HI\n"
161 "-\n"));
162 CPPUNIT_ASSERT(isCheckmate(
163 "P1 * * * -KI * * * * -KY\n"
164 "P2 * +KI-GI * * * * * * \n"
165 "P3+GI-FU-FU * * * * * -FU\n"
166 "P4-FU * -KE * * -FU-FU-FU * \n"
167 "P5 * * +KA * * * * * * \n"
168 "P6 * * * * -FU * * * * \n"
169 "P7 * +FU+KE+FU * +KA-OU * +FU\n"
170 "P8 * * +OU+GI-NG * * * * \n"
171 "P9 * * +KI * * +KY * * +KY\n"
172 "P-00FU00FU00FU00FU00FU00FU00KE00KI00KY00HI\n"
173 "P+00FU00KE00HI\n"
174 "+\n",
175 "P1 * * * -KI * * * * -KY\n"
176 "P2 * +KI-GI * * * * * * \n"
177 "P3+GI-FU-FU * * * * * -FU\n"
178 "P4-FU * -KE * * -FU-FU-FU * \n"
179 "P5 * * +KA * * * * * * \n"
180 "P6 * * * * -FU * * * * \n"
181 "P7+FU * +KE+FU * +KA-OU * +FU\n"
182 "P8 * * +OU+GI-NG * * * * \n"
183 "P9 * * +KI * * +KY * * +KY\n"
184 "P-00FU00FU00FU00FU00FU00FU00KE00KI00KY00HI\n"
185 "P+00FU00KE00HI\n"
186 "+\n"));
187 CPPUNIT_ASSERT(isCheckmate(
188 "P1-KY * * +TO-KI-OU * * * \n"
189 "P2 * * +RY-GI * * -KI * * \n"
190 "P3-FU * -FU-FU * * * -GI-FU\n"
191 "P4 * * * * -KE-FU * * * \n"
192 "P5 * * * * * -KE-FU * * \n"
193 "P6 * +FU+FU * * * * * +FU\n"
194 "P7+FU+OU * +FU * +FU+GI * * \n"
195 "P8 * * * * -RY * * * * \n"
196 "P9+KY+KE+KE * * * -UM * +KY\n"
197 "P+00FU00FU00FU00GI00KY00KA\n"
198 "P-00FU00FU00KI00KI\n"
199 "-\n",
200 "P1-KY * * +TO-KI-OU * * * \n"
201 "P2 * * +RY-GI * -KI * * * \n"
202 "P3-FU * -FU-FU * * * -GI-FU\n"
203 "P4 * * * * -KE-FU * * * \n"
204 "P5 * * * * * -KE-FU * * \n"
205 "P6 * +FU+FU * * * * * +FU\n"
206 "P7+FU+OU * +FU * +FU+GI * * \n"
207 "P8 * * * * -RY * * * * \n"
208 "P9+KY+KE+KE * * * -UM * +KY\n"
209 "P+00FU00FU00FU00GI00KY00KA\n"
210 "P-00FU00FU00KI00KI\n"
211 "-\n"));
212 CPPUNIT_ASSERT(isCheckmate(
213 "P1-KY * -OU-KI * * * * -KY\n"
214 "P2 * * * * * * -KI * * \n"
215 "P3-FU-FU+NK * -FU-FU * * * \n"
216 "P4 * * * * * -HI * * -FU\n"
217 "P5+FU+FU * -GI * -KE-KE * * \n"
218 "P6+HI * * * +FU+KI * -FU+FU\n"
219 "P7 * * -FU+FU+KA * +FU * * \n"
220 "P8 * * +KI * * * * +FU * \n"
221 "P9+KY * * +OU * * * +KE+KY\n"
222 "P-00FU00FU00FU00GI00GI\n"
223 "P+00FU00GI00KA\n"
224 "-\n",
225 "P1-KY * -OU-KI * * * * -KY\n"
226 "P2 * * * * * * -KI * * \n"
227 "P3-FU-FU+NK * -FU-FU * * * \n"
228 "P4 * * * * * -HI * * -FU\n"
229 "P5+FU+FU * -GI * -KE-KE * * \n"
230 "P6+HI * * * +FU+KI * -FU+FU\n"
231 "P7 * * -FU+FU+KA * +FU * * \n"
232 "P8 * * +KI * * * * +FU * \n"
233 "P9+KY * * +OU * * * +KE+KY\n"
234 "P-00FU00FU00FU00GI00GI\n"
235 "P+00FU00GI00KA\n"
236 "-\n"));
237
238 }
239
240 void OracleProverTest::testInvalidDefense()
241 {
242 SimpleState state=CsaString(
243 "P1 * * * * * * * -KE-OU\n"
244 "P2 * * * * * * * -KA-KY\n"
245 "P3 * * * * * * * * * \n"
246 "P4 * * * * * * * * * \n"
247 "P5 * * * * * * * * * \n"
248 "P6 * * * * * * * * * \n"
249 "P7 * * * * * * * * * \n"
250 "P8 * * * * * * * * * \n"
251 "P9 * * +OU * * * * * * \n"
252 "P+00KE\n"
253 "P-00AL\n"
254 "+\n").getInitialState();
255 HashEffectState e_state(state);
256 DualCheckmateSearcher<> checker(limit);
257 checker.setVerbose(!isShortTest);
258 Move check_move;
259 PathEncoding path(BLACK);
260 AttackOracleAges oracle_age;
261 const bool checkmate = checker.isWinningState<BLACK>
262 (10, e_state, e_state.getHash(), path, check_move, oracle_age);
263 CPPUNIT_ASSERT_EQUAL(true, checkmate);
264 const Move m23KE = Move(Square(2,3),KNIGHT,BLACK);
265 CPPUNIT_ASSERT_EQUAL(m23KE, check_move);
266
267 SimpleState state2=CsaString(
268 "P1 * * * * * * * -KE-OU\n"
269 "P2 * * * * * * -FU-KA-KY\n"
270 "P3 * * * * * * * * * \n"
271 "P4 * * * * * * * * * \n"
272 "P5 * * * * * * * * * \n"
273 "P6 * * * * * * * * * \n"
274 "P7 * * * * * * * * * \n"
275 "P8 * * * * * * * * * \n"
276 "P9 * * +OU * * * * * * \n"
277 "P+00KE\n"
278 "P-00AL\n"
279 "+\n").getInitialState();
280 HashEffectState e_state2(state2);
281 CheckHashTable& table = checker.getTable(BLACK);
282 OracleProver<CheckHashTable> prover(table);
283 const CheckHashRecord *oracle = table.find(e_state.getHash());
284
285 const bool result2 = prover.proofWin(e_state2, path, oracle, check_move);
286 CPPUNIT_ASSERT_EQUAL(true, result2);
287 CPPUNIT_ASSERT_EQUAL(m23KE, check_move);
288 #if 0
289 // ImmediateCheckmate が対応していないので難しい
290 // さっきと違って -2223FU と取れるが無効. SafeMoveFilter が入れば解決?
291 SimpleState state3=CsaString(
292 "P1 * * * * * * * -KE-OU\n"
293 "P2 * * * * * * -FU-FU-KY\n"
294 "P3 * * * * * * * * * \n"
295 "P4 * * * * * +KA * * * \n"
296 "P5 * * * * * * * * * \n"
297 "P6 * * * * * * * * * \n"
298 "P7 * * * * * * * * * \n"
299 "P8 * * * * * * * * * \n"
300 "P9 * * +OU * * * * * * \n"
301 "P+00KE\n"
302 "P-00AL\n"
303 "+\n").getInitialState();
304 HashEffectState e_state3(state3);
305
306 const bool result3 = prover.proofWin(e_state3, path, oracle, check_move);
307 CPPUNIT_ASSERT_EQUAL(true, result3);
308 CPPUNIT_ASSERT_EQUAL(m23KE, check_move);
309 #endif
310 SimpleState state4=CsaString(
311 "P1 * * * * * * * -KE-OU\n"
312 "P2 * * * * * * -FU-FU-KY\n"
313 "P3 * * * * * * * * * \n"
314 "P4 * * * * * +HI * * * \n"
315 "P5 * * * * * * * * * \n"
316 "P6 * * * * * * * * * \n"
317 "P7 * * * * * * * * * \n"
318 "P8 * * * * * * * * * \n"
319 "P9 * * +OU * * * * * * \n"
320 "P+00KE\n"
321 "P-00AL\n"
322 "+\n").getInitialState();
323 HashEffectState e_state4(state4);
324
325 const bool result4 = prover.proofWin(e_state4, path, oracle, check_move);
326 CPPUNIT_ASSERT_EQUAL(false, result4);
327 }
328
329 void OracleProverTest::testInvalidDefenseLose()
330 {
331 SimpleState state=CsaString(
332 "P1 * * * * * * * -KE-OU\n"
333 "P2 * * * * * * * -KA-KY\n"
334 "P3 * * * * * * * +KE * \n"
335 "P4 * * * * * * * * * \n"
336 "P5 * * * * * * * * * \n"
337 "P6 * * * * * * * * * \n"
338 "P7 * * * * * * * * * \n"
339 "P8 * * * * * * * * * \n"
340 "P9 * * +OU * * * * * * \n"
341 "P-00AL\n"
342 "-\n").getInitialState();
343 HashEffectState e_state(state);
344 DualCheckmateSearcher<> checker(limit);
345 checker.setVerbose(!isShortTest);
346 PathEncoding path(WHITE);
347 const Move last_move = Move(Square(2,3),KNIGHT,BLACK);
348 const bool checkmate = checker.isLosingState<WHITE>
349 (10, e_state, e_state.getHash(), path, last_move);
350 CPPUNIT_ASSERT_EQUAL(true, checkmate);
351
352 SimpleState state2=CsaString(
353 "P1 * * * * * * * -KE-OU\n"
354 "P2 * * * * * * -FU-KA-KY\n"
355 "P3 * * * * * * * +KE * \n"
356 "P4 * * * * * * * * * \n"
357 "P5 * * * * * * * * * \n"
358 "P6 * * * * * * * * * \n"
359 "P7 * * * * * * * * * \n"
360 "P8 * * * * * * * * * \n"
361 "P9 * * +OU * * * * * * \n"
362 "P-00AL\n"
363 "-\n").getInitialState();
364 HashEffectState e_state2(state2);
365 CheckHashTable& table = checker.getTable(BLACK);
366 OracleProver<CheckHashTable> prover(table);
367 const CheckHashRecord *oracle = table.find(e_state.getHash());
368
369 const bool result2 = prover.proofLose(e_state2, path, 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, path, 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, path, oracle, last_move);
405 CPPUNIT_ASSERT_EQUAL(false, result4);
406 }
407
408 void OracleProverTest::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 OracleProver<CheckHashTable> prover(table);
453 const CheckHashRecord *oracle = table.find(e_state.getHash());
454
455 Move checkmate_move2 = Move::INVALID();
456 const int distanceSimilar = 30;
457 const PathEncoding pathSimilar(BLACK, distanceSimilar);
458 const bool result = prover.proofWin(e_stateSimilar, pathSimilar,
459 oracle, checkmate_move2);
460 CPPUNIT_ASSERT_EQUAL(check_move, checkmate_move2);
461 CPPUNIT_ASSERT_EQUAL(true, result);
462
463 const CheckHashRecord *recordSimilar = table.find(e_stateSimilar.getHash());
464 CPPUNIT_ASSERT_EQUAL(distance, static_cast<int>(oracle->distance));
465 CPPUNIT_ASSERT_EQUAL(distanceSimilar, static_cast<int>(recordSimilar->distance));
466 }
467
468 void OracleProverTest::testAdjustMove()
469 {
470 const Move m1 =
471 Move(Square(8,9),Square(7,9),KING,PTYPE_EMPTY,false,BLACK);
472 Move m2 = m1;
473 m2.setCapturePtype(PAWN);
474 CPPUNIT_ASSERT(m1 != m2);
475 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
476 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
477 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
478 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
479 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
480
481 m2.setCapturePtype(ROOK);
482 CPPUNIT_ASSERT(m1 != m2);
483 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
484 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
485 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
486 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
487 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
488
489 m2.setCapturePtype(PPAWN);
490 CPPUNIT_ASSERT(m1 != m2);
491 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
492 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
493 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
494 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
495 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
496
497 m2.setCapturePtype(PTYPE_EMPTY);
498 CPPUNIT_ASSERT(m1 == m2);
499 CPPUNIT_ASSERT_EQUAL(m1.from(), m2.from());
500 CPPUNIT_ASSERT_EQUAL(m1.to(), m2.to());
501 CPPUNIT_ASSERT_EQUAL(m1.ptype(), m2.ptype());
502 CPPUNIT_ASSERT_EQUAL(m1.promoteMask(), m2.promoteMask());
503 CPPUNIT_ASSERT_EQUAL(m1.player(), m2.player());
504
505 SimpleState state=CsaString(
506 "P1 * * * * * * * -OU * \n"
507 "P2 * * * * * * * * * \n"
508 "P3 * * * * * * * * * \n"
509 "P4 * * * * * * +KE * +KE\n"
510 "P5 * * * * * * * * * \n"
511 "P6 * * * * * * * * * \n"
512 "P7 * * * * * * * * * \n"
513 "P8 * * * * * * * * * \n"
514 "P9 * +OU * * * * * * * \n"
515 "P-00AL\n"
516 "+\n").getInitialState();
517 HashEffectState e_state(state);
518
519 DualCheckmateSearcher<> checker(limit);
520 checker.setVerbose(!isShortTest);
521 CheckHashTable& table = checker.getTable(BLACK);
522 Move checkmate_move = Move::INVALID();
523 PathEncoding path(BLACK);
524 AttackOracleAges oracle_age;
525 const bool win = checker.isWinningState<BLACK>
526 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
527 CPPUNIT_ASSERT(win);
528 CPPUNIT_ASSERT(checkmate_move != Move::INVALID());
529 const CheckHashRecord *oracle = table.find(e_state.getHash());
530 CPPUNIT_ASSERT(oracle);
531
532 // 取る駒が違っても詰む
533 SimpleState state2=CsaString(
534 "P1 * * * * * * * -OU * \n"
535 "P2 * * * * * * * -KI * \n"
536 "P3 * * * * * * * * * \n"
537 "P4 * * * * * * +KE * +KE\n"
538 "P5 * * * * * * * * * \n"
539 "P6 * * * * * * * * * \n"
540 "P7 * * * * * * * * * \n"
541 "P8 * * * * * * * * * \n"
542 "P9 * +OU * * * * * * * \n"
543 "P-00AL\n"
544 "+\n").getInitialState();
545 HashEffectState e_state2(state2);
546
547 Move checkmate_move2 = Move::INVALID();
548 OracleProver<CheckHashTable> prover(table);
549 const bool result = prover.proofWin(e_state2, path, oracle, checkmate_move2);
550 CPPUNIT_ASSERT_EQUAL(true, result);
551 CPPUNIT_ASSERT_EQUAL(checkmate_move.from(), checkmate_move2.from());
552 CPPUNIT_ASSERT_EQUAL(checkmate_move.to(), checkmate_move2.to());
553 CPPUNIT_ASSERT_EQUAL(checkmate_move.ptype(), checkmate_move2.ptype());
554
555 {
556 // 初期位置が違っても詰む
557 HashEffectState state(CsaString(
558 "P1 * * * * +HI * -KI-OU * \n"
559 "P2 * * * * * * * -FU-FU\n"
560 "P3 * * * * * * +FU * * \n"
561 "P4 * * * * * * * * * \n"
562 "P5 * * * * * * * * * \n"
563 "P6 * * * * * * * * * \n"
564 "P7 * * * * * * * * * \n"
565 "P8 * * * * * * * * * \n"
566 "P9 * +OU * * * * * * * \n"
567 "P-00AL\n"
568 "+\n").getInitialState());
569
570 DualCheckmateSearcher<> checker(limit);
571 checker.setVerbose(!isShortTest);
572 CheckHashTable& table = checker.getTable(BLACK);
573 Move checkmate_move;
574 PathEncoding path(BLACK);
575 AttackOracleAges oracle_age;
576 const bool win = checker.isWinningState<BLACK>
577 (500, state, state.getHash(), path, checkmate_move, oracle_age);
578 CPPUNIT_ASSERT(win);
579 CPPUNIT_ASSERT(checkmate_move.isNormal());
580 const CheckHashRecord *oracle = table.find(state.getHash());
581 CPPUNIT_ASSERT(oracle);
582
583 // 飛車移動
584 HashEffectState state2(CsaString(
585 "P1 * * * +HI * * -KI-OU * \n"
586 "P2 * * * * * * * -FU-FU\n"
587 "P3 * * * * * * +FU * * \n"
588 "P4 * * * * * * * * * \n"
589 "P5 * * * * * * * * * \n"
590 "P6 * * * * * * * * * \n"
591 "P7 * * * * * * * * * \n"
592 "P8 * * * * * * * * * \n"
593 "P9 * +OU * * * * * * * \n"
594 "P-00AL\n"
595 "+\n").getInitialState());
596
597 Move checkmate_move2;
598 OracleProver<CheckHashTable> prover(table);
599 const bool result = prover.proofWin(state2, path, oracle, checkmate_move2);
600 CPPUNIT_ASSERT_EQUAL(true, result);
601 CPPUNIT_ASSERT_EQUAL(checkmate_move.to(), checkmate_move2.to());
602 CPPUNIT_ASSERT_EQUAL(checkmate_move.ptype(), checkmate_move2.ptype());
603 CPPUNIT_ASSERT_EQUAL(checkmate_move.capturePtype(), checkmate_move2.capturePtype());
604 CPPUNIT_ASSERT(checkmate_move.from() != checkmate_move2.from());
605 CPPUNIT_ASSERT_EQUAL(Square(6,1), checkmate_move2.from());
606 }
607 }
608
609 void OracleProverTest::testNoCheck()
610 {
611 SimpleState state=CsaString(
612 "P1 * * * * * * * -OU * \n"
613 "P2 * * * * * * * * * \n"
614 "P3 * * * * * * * * * \n"
615 "P4 * * * * * * +KE * +KE\n"
616 "P5 * * * * * * * * * \n"
617 "P6 * * * * * * * * * \n"
618 "P7 * * * * * * * * * \n"
619 "P8 * * * * * * * * * \n"
620 "P9 * +OU * * * * * * * \n"
621 "P-00AL\n"
622 "+\n").getInitialState();
623 HashEffectState e_state(state);
624
625 DualCheckmateSearcher<> checker(limit);
626 checker.setVerbose(!isShortTest);
627 CheckHashTable& table = checker.getTable(BLACK);
628 Move checkmate_move = Move::INVALID();
629 PathEncoding path(BLACK);
630 AttackOracleAges oracle_age;
631 const bool win = checker.isWinningState<BLACK>
632 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
633 CPPUNIT_ASSERT(win);
634 CPPUNIT_ASSERT(checkmate_move != Move::INVALID());
635 const CheckHashRecord *record = table.find(e_state.getHash());
636 CPPUNIT_ASSERT(record);
637
638 // 玉が移動していると王手にならない
639 SimpleState state2=CsaString(
640 "P1 * * -OU * * * * * * \n"
641 "P2 * * * * * * * * * \n"
642 "P3 * * * * * * * * * \n"
643 "P4 * * * * * * +KE * +KE\n"
644 "P5 * * * * * * * * * \n"
645 "P6 * * * * * * * * * \n"
646 "P7 * * * * * * * * * \n"
647 "P8 * * * * * * * * * \n"
648 "P9 * +OU * * * * * * * \n"
649 "P-00AL\n"
650 "+\n").getInitialState();
651 HashEffectState e_state2(state2);
652
653 Move checkmate_move2 = Move::INVALID();
654 OracleProver<CheckHashTable> prover(checker.getTable(BLACK));
655 ProofOracleAttack<BLACK> oracle(record);
656 const bool result = prover.proofWin(e_state2, path, record, checkmate_move2);
657 CPPUNIT_ASSERT_EQUAL(false, result);
658 }
659
660
661
662 void OracleProverTest::testCheck()
663 {
664 HashEffectState e_state
665 (CsaString(
666 "P1+NY+TO * * * * -OU-KE-KY\n"
667 "P2+FU * * * * -GI-KI * *\n"
668 "P3 * +RY * * +UM * -KI-FU-FU\n"
669 "P4 * * +FU-FU * * * * *\n"
670 "P5 * * -KE * +FU * * +FU *\n"
671 "P6-KE * * +FU+GI-FU * * +FU\n"
672 "P7 * * -UM+GI * * * * *\n"
673 "P8 * * * * * * * * * \n"
674 "P9 * +OU * * * * * * -NG\n"
675 "P+00HI00KI00KE00KY00FU00FU00FU00FU00FU\n"
676 "P-00KI00KY00FU00FU\n"
677 "P-00AL\n"
678 "-\n").getInitialState());
679 DualCheckmateSearcher<> checker(limit);
680 checker.setVerbose(!isShortTest);
681 CheckHashTable& table = checker.getTable(WHITE);
682 Move checkmate_move = Move::INVALID();
683 const PathEncoding path(e_state.turn());
684 AttackOracleAges oracle_age;
685 const bool win = checker.isWinningState<WHITE>
686 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
687 CPPUNIT_ASSERT(win);
688 const CheckHashRecord *oracle = table.find(e_state.getHash());
689 CPPUNIT_ASSERT(oracle);
690
691 // 詰む局面は詰む
692 Move best_move = Move::INVALID();
693 OracleProver<CheckHashTable> prover(table);
694 const bool result = prover.proofWin(e_state, path, oracle, best_move);
695 CPPUNIT_ASSERT_EQUAL(true, result);
696 CPPUNIT_ASSERT_EQUAL(best_move, oracle->bestMove->move);
697
698 // 類似局面も詰む
699 best_move = Move::INVALID();
700 ApplyMoveOfTurn::doMove(e_state, Move(Square(3,7),PAWN,WHITE));
701 ApplyMoveOfTurn::doMove(e_state, Move(Square(8,2),PAWN,BLACK));
702 CPPUNIT_ASSERT_EQUAL(true, prover.proofWin(e_state, path, oracle, best_move));
703 CPPUNIT_ASSERT_EQUAL(best_move, oracle->bestMove->move);
704
705 // 駒が足りないと詰まない
706 {
707 HashEffectState h2 = e_state;
708 ApplyMoveOfTurn::doMove(h2, Move(Square(3,8),GOLD,WHITE));
709 ApplyMoveOfTurn::doMove(h2, Move(Square(4,3),PAWN,BLACK));
710 CPPUNIT_ASSERT_EQUAL(false, prover.proofWin(h2, path, oracle, best_move));
711 }
712
713 // 玉が動いても詰まない
714 {
715 HashEffectState h2 = e_state;
716 ApplyMoveOfTurn::doMove(h2, Move(Square(9,3),PAWN,WHITE));
717 ApplyMoveOfTurn::doMove(h2, Move(Square(8,9),Square(7,9),KING,PTYPE_EMPTY,false,BLACK));
718 const bool result = prover.proofWin(h2, path, oracle, best_move);
719 CPPUNIT_ASSERT_EQUAL(false, result);
720 }
721 // 王手でも詰まない
722 {
723 HashEffectState h2 = e_state;
724 ApplyMoveOfTurn::doMove(h2, Move(Square(9,3),PAWN,WHITE));
725 ApplyMoveOfTurn::doMove(h2, Move(Square(4,1),GOLD,BLACK));
726 const bool result = prover.proofWin(h2, path, oracle, best_move);
727 CPPUNIT_ASSERT_EQUAL(false, result);
728 }
729 }
730
731 void OracleProverTest::testPawnCheckmate()
732 {
733 SimpleState state=CsaString(
734 "P1+NY+TO * * * * -OU-KE-KY\n"
735 "P2 * * * * * -GI-KI * *\n"
736 "P3 * +RY * * +UM * -KI-FU-FU\n"
737 "P4 * * -KE-FU * * * * *\n"
738 "P5 * * * * +FU * * +FU *\n"
739 "P6 * -KE * +FU+GI-FU * * +FU\n"
740 "P7 * +FU-UM * * * * * *\n"
741 "P8 * * * * * * * * * \n"
742 "P9 * +OU+FU * -GI * * * -NG\n"
743 "P+00KI00HI00KI00KE00KY00FU00FU00FU00FU00FU\n"
744 "P-00KY00FU00FU\n"
745 "P+00AL\n"
746 "-\n").getInitialState();
747 HashEffectState e_state((HashEffectState(state)));
748
749 const PathEncoding path(WHITE);
750 DualCheckmateSearcher<> checker(limit);
751 checker.setVerbose(!isShortTest);
752 CheckHashTable& table = checker.getTable(WHITE);
753 Move checkmate_move = Move::INVALID();
754 AttackOracleAges oracle_age;
755 const bool win = checker.isWinningState<WHITE>
756 (500, e_state, e_state.getHash(), path, checkmate_move, oracle_age);
757 CPPUNIT_ASSERT(win);
758 const CheckHashRecord *oracle = table.find(e_state.getHash());
759
760 // 詰む局面は詰む 0088FU, 0098KY
761 Move best_move = Move::INVALID();
762 OracleProver<CheckHashTable> prover(table);
763 CPPUNIT_ASSERT_EQUAL(true, prover.proofWin(e_state, path, oracle, best_move));
764 CPPUNIT_ASSERT_EQUAL(best_move, oracle->bestMove->move);
765
766 // 類似局面も詰む
767 best_move = Move::INVALID();
768 ApplyMoveOfTurn::doMove(e_state, Move(Square(3,7),PAWN,WHITE));
769 ApplyMoveOfTurn::doMove(e_state, Move(Square(4,3),PAWN,BLACK));
770 CPPUNIT_ASSERT_EQUAL(true, prover.proofWin(e_state, path, oracle, best_move));
771 CPPUNIT_ASSERT_EQUAL(best_move, oracle->bestMove->move);
772
773 // 同じ手順では打歩詰になる
774 best_move = Move::INVALID();
775 ApplyMoveOfTurn::doMove(e_state, Move(Square(7,3),LANCE,WHITE));
776 ApplyMoveOfTurn::doMove(e_state, Move(Square(9,9),PAWN,BLACK));
777 CPPUNIT_ASSERT_EQUAL(false, prover.proofWin(e_state, path, oracle, best_move));
778 }
779
780 void OracleProverTest::testPawnCheckmateLose()
781 {
782 SimpleState state=CsaString(
783 "P1 * * * * * * * -KE * \n"
784 "P2 * * * * * * * -OU * \n"
785 "P3 * * * * * * * +FU-FU\n"
786 "P4 * * * * * * +RY * * \n"
787 "P5 * * * * * * * * * \n"
788 "P6 * * * * * * * * * \n"
789 "P7 * * * * * * * * * \n"
790 "P8 * * * * * * * * * \n"
791 "P9 * * +OU * * * * * * \n"
792 "P+00KI\n"
793 "P-00AL\n"
794 "-\n").getInitialState();
795 HashEffectState e_state(state);
796 const PathEncoding path(WHITE);
797 DualCheckmateSearcher<> checker(limit);
798 checker.setVerbose(!isShortTest);
799 CheckHashTable& table = checker.getTable(BLACK);
800 const Move last_move = Move(Square(2,3), PAWN, BLACK);
801 const bool win = checker.isLosingState<WHITE>(500, e_state, e_state.getHash(), path);
802 CPPUNIT_ASSERT(win);
803
804 const CheckHashRecord *oracle = table.find(e_state.getHash());
805 OracleProver<CheckHashTable> prover(table);
806 CPPUNIT_ASSERT_EQUAL(true, prover.proofLose(e_state, path, 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, path, oracle, last_move));
824 }
825
826 void OracleProverTest::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 *oracle = table.find(e_state.getHash());
851
852 // 詰む局面は詰む
853 OracleProver<CheckHashTable> prover(table);
854 CPPUNIT_ASSERT_EQUAL(true, prover.proofLose(e_state, path, oracle));
855
856 // 類似局面も詰む
857 ApplyMoveOfTurn::doMove(e_state, Move(Square(4,3),PAWN,BLACK));
858 ApplyMoveOfTurn::doMove(e_state, Move(Square(3,7),PAWN,WHITE));
859 CPPUNIT_ASSERT_EQUAL(true, prover.proofLose(e_state, path, oracle));
860 CPPUNIT_ASSERT_EQUAL(true, checker.isLosingState<BLACK>(1, e_state, e_state.getHash(), path));
861
862 // 玉が動くと同じ手順では詰まない
863 {
864 HashEffectState h2 = e_state;
865 ApplyMoveOfTurn::doMove(h2, Move(Square(8,9),Square(7,9),KING,PTYPE_EMPTY,false,BLACK));
866 ApplyMoveOfTurn::doMove(h2, Move(Square(9,9),Square(8,9),GOLD,PTYPE_EMPTY,false,WHITE));
867 CPPUNIT_ASSERT_EQUAL(false, prover.proofLose(h2, path ,oracle));
868 }
869 }
870
871 /* ------------------------------------------------------------------------- */
872 // ;;; Local Variables:
873 // ;;; mode:c++
874 // ;;; c-basic-offset:2
875 // ;;; End: