Debian Codegraph @

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

raw
1 /* defenseSimulation.h
2 */
3 #ifndef _DEFENSE_SIMULATION_TCC
4 #define _DEFENSE_SIMULATION_TCC
5
6 #include "defenseSimulation.h"
7 #include "checkHashRecord.h"
8 #include "oracleDisprover.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 Disprover, Player P>
20 struct CallDisprover
21 {
22 typedef typename Disprover::state_t state_t;
23 bool *result;
24 Disprover *prover;
25 state_t *state;
26 const HashKey& new_key;
27 const PathEncoding& new_path;
28 DisproofOracleDefense<P> oracle;
29 Move last_move;
30 CallDisprover(bool *r, Disprover *p, state_t *s,
31 const HashKey& k, const PathEncoding& pa,
32 DisproofOracleDefense<P> o, Move last)
33 : result(r), prover(p), state(s), new_key(k), new_path(pa),
34 oracle(o), last_move(last)
35 {
36 }
37 void operator()(Square)
38 {
39 Move escape_move; // 本当は不要 interface 増やす?
40 *result = (*prover).template proofEscape<P>
41 (*state, new_key, new_path, oracle, escape_move, last_move);
42 }
43 };
44 } // namespace checkmate
45 } // namespace osl
46
47 template <osl::Player P>
48 template <class Table>
49 inline
50 bool osl::checkmate::DefenseSimulation<P>::
51 disproof(NumEffectState& state,
52 const HashKey& new_key, const PathEncoding& new_path, Table& table,
53 const CheckMove& target, CheckHashRecord *guide, size_t& node_count)
54 {
55 check_assert(target.record);
56 check_assert(state.turn() == P);
57 DisproofOracleDefense<P> oracle(guide);
58 check_assert(oracle.isValid());
59 typedef OracleDisprover<Table> disprover_t;
60 disprover_t disprover(table);
61 bool result;
62 CallDisprover<disprover_t,P>
63 helper(&result, &disprover, &state, new_key, new_path, oracle, target.move);
64 ApplyMove<P>::doUndoMove(state, target.move, helper);
65 node_count += disprover.nodeCount();
66 return result;
67 }
68
69 template <osl::Player P>
70 template <class Table>
71 bool osl::checkmate::DefenseSimulation<P>::
72 disproofNoPromote(NumEffectState& state,
73 const HashKey& new_key,
74 const PathEncoding& new_path,
75 CheckHashRecord *record, Table& table,
76 CheckMove& target, const CheckMove& guide,
77 size_t& node_count)
78 {
79 #ifdef CHECKMATE_DEBUG
80 static stat::Ratio disproven("defense disproof:nopromote");
81 #endif
82 check_assert(guide.move.unpromote() == target.move);
83 const bool result = disproof(state, new_key, new_path, table, target,
84 guide.record, node_count);
85 if (result)
86 {
87 if (target.record->proofDisproof().isCheckmateFail())
88 record->addToSolvedInAttack(target, target.record->proofDisproof());
89 else
90 check_assert(target.record->findLoop(new_path, table.getTwinTable()));
91 }
92 #ifdef CHECKMATE_DEBUG
93 disproven.add(result);
94 #endif
95 return result;
96 }
97
98 template <osl::Player P>
99 template <class Table>
100 void osl::checkmate::DefenseSimulation<P>::
101 disproofDropSibling(NumEffectState& state,
102 const HashKey& key, const PathEncoding& path,
103 CheckHashRecord *record,
104 Table& table, const CheckMove& guide, size_t& node_count)
105 {
106 #ifdef CHECKMATE_DEBUG
107 static stat::Ratio disproven("defense disproof:drop sibling");
108 #endif
109 check_assert(guide.move.isDrop());
110 const Ptype ptype = guide.move.ptype();
111 for (CheckMoveList::iterator p=record->moves.begin();
112 p!=record->moves.end(); ++p)
113 {
114 if (! record->filter.isTarget(p->flags))
115 continue;
116 if (! p->move.isDrop())
117 continue;
118
119 const HashKey new_key = key.newHashWithMove(p->move);
120 const PathEncoding new_path(path, p->move);
121 if (p->move.ptype() == ptype)
122 {
123 if (! p->record)
124 {
125 CheckTableUtil::allocate
126 (p->move, p->record, table, new_key, new_path, record);
127 }
128 const bool result =
129 disproof(state, new_key, new_path, table, *p, guide.record, node_count);
130 if (result)
131 {
132 if (p->record->proofDisproof().isCheckmateFail())
133 record->addToSolvedInAttack(*p, p->record->proofDisproof());
134 else
135 check_assert(p->findLoop(path, table.getTwinTable()));
136 }
137 #ifdef CHECKMATE_DEBUG
138 disproven.add(result);
139 #endif
140 }
141 }
142 }
143
144
145 #endif /* _DEFENSE_SIMULATION_TCC */
146 // ;;; Local Variables:
147 // ;;; mode:c++
148 // ;;; c-basic-offset:2
149 // ;;; End: