Debian Codegraph @

old-sample/legacy-checkmate/lib/blockingSimulation.tcc

raw
1 /* blockingSimulation.tcc
2 */
3 #ifndef _BLOCKING_SIMULATION_TCC
4 #define _BLOCKING_SIMULATION_TCC
5
6 #include "blockingSimulation.h"
7 #include "checkHashRecord.h"
8 #include "oracleProver.h"
9 #include "checkTableUtil.h"
10 #include "osl/apply_move/applyMoveWithPath.h"
11 #ifdef CHECKMATE_DEBUG
12 # include "osl/stat/ratio.h"
13 #endif
14
15 namespace osl
16 {
17 namespace checkmate
18 {
19 template <class Prover, Player P>
20 struct CallProver
21 {
22 typedef typename Prover::state_t state_t;
23 bool *result;
24 Prover *prover;
25 state_t *state;
26 const HashKey& key;
27 const PathEncoding& path;
28 ProofOracleAttack<P> oracle;
29 CallProver(bool *r, Prover *p, state_t *s, const HashKey& k,
30 const PathEncoding& pa, ProofOracleAttack<P> o)
31 : result(r), prover(p), state(s), key(k), path(pa), oracle(o)
32 {
33 }
34 void operator()(Square)
35 {
36 Move check_move; // 本当は不要 interface 増やす?
37 *result = (*prover).template proofWin<P>
38 (*state, key, path, oracle, check_move);
39 }
40 };
41 } // namespace checkmate
42 } // namespace osl
43
44 template <osl::Player P>
45 template <class Table>
46 inline
47 bool osl::checkmate::BlockingSimulation<P>::
48 proof(NumEffectState& state, const HashKey& new_key,
49 const PathEncoding& new_path, Table& table,
50 const CheckMove& target, const CheckHashRecord *guide,
51 size_t& node_count)
52 {
53 assert(state.turn() == alt(P));
54 ProofOracleAttack<P> oracle(guide);
55 assert(oracle.isValid());
56 typedef OracleProver<Table> prover_t;
57 prover_t prover(table);
58 bool result;
59 CallProver<prover_t,P> helper(&result, &prover, &state, new_key, new_path, oracle);
60 ApplyMove<PlayerTraits<P>::opponent>::doUndoMove(state, target.move, helper);
61 node_count += prover.nodeCount();
62 return result;
63 }
64
65 template <osl::Player P>
66 template <class Table>
67 bool osl::checkmate::BlockingSimulation<P>::
68 proof(NumEffectState& state, const HashKey& new_key, const PathEncoding& new_path,
69 const CheckHashRecord *record, Table& table, const CheckMove& move,
70 size_t& node_count)
71 {
72 #ifdef CHECKMATE_DEBUG
73 static stat::Ratio oracle_found("blocking proof:oracle found"),
74 proven("blocking proof:sim success");
75 #endif
76 CheckMoveList::const_iterator p=record->moves.begin();
77 for (; p!=record->moves.end(); ++p)
78 {
79 if (p->flags.isSet(MoveFlags::Solved)
80 && (p->move.to() == move.move.to()))
81 break;
82 }
83 #ifdef CHECKMATE_DEBUG
84 oracle_found.add(p != record->moves.end());
85 #endif
86 if (p == record->moves.end())
87 return false;
88
89 const bool result = proof(state, new_key, new_path,
90 table, move, p->record, node_count);
91 #ifdef CHECKMATE_DEBUG
92 proven.add(result);
93 #endif
94 return result;
95 }
96
97 template <osl::Player P>
98 template <class Table>
99 void osl::checkmate::BlockingSimulation<P>::
100 proofSibling(NumEffectState& state,
101 const HashKey& key, const PathEncoding& path,
102 CheckHashRecord *record,
103 Table& table, const CheckMove& guide, size_t& node_count)
104 {
105 #ifdef CHECKMATE_DEBUG
106 static stat::Ratio proven("blocking proof:sibling");
107 #endif
108 const Square to = guide.move.to();
109 for (CheckMoveList::iterator p=record->moves.begin();
110 p!=record->moves.end(); ++p)
111 {
112 if (! record->filter.isTarget(p->flags))
113 continue;
114 if (p->move.to() == to)
115 {
116 const HashKey new_key = key.newHashWithMove(p->move);
117 const PathEncoding new_path(path, p->move);
118 if (! p->record)
119 {
120 CheckTableUtil::allocate
121 (p->move, p->record, table, new_key, new_path, record);
122 }
123 const bool result =
124 proof(state, new_key, new_path, table, *p, guide.record, node_count);
125 if (result)
126 record->addToSolvedInDefense(*p, p->record->proofDisproof());
127 #ifdef CHECKMATE_DEBUG
128 proven.add(result);
129 #endif
130 }
131 }
132 }
133
134
135 #endif /* _BLOCKING_SIMULATION_TCC */
136 // ;;; Local Variables:
137 // ;;; mode:c++
138 // ;;; c-basic-offset:2
139 // ;;; End: